-
修改表名
格式:sp_rename tablename,newtablename
sp_rename tablename,newtablename
-
修改字段名
格式:sp_rename 'tablename.colname',newcolname,'column'
sp_rename 'tablename.colname',newcolname,'column'
-
添加字段
格式:alter table table_name add new_column data_type [interality_codition]
示例1
ALTER TABLE student Add nationality varchar(20) --示例2 添加int类型的列,默认值为 0alter table student add studentName int default 0
--示例3 添加int类型的列,默认值为0,主键
alter table student add studentId int primary key default 0
--示例4 判断student中是否存在name字段且删除字段
if exists(select * from syscolumns where id=object_id('student') and name='name') begin
alter table student DROP COLUMN name
end -
更改字段
格式:alter table table_name alter column column_name
ALTER TABLE student ALTER COLUMN name VARCHAR(200)
-
删除字段
格式:alter table table_name drop column column_name
ALTER TABLE student DROP COLUMN nationality;
-
查看字段约束
格式: select * from information_schema.constraint_column_usage where TABLE_NAME = table_name
SELECT TABLE_NAME,COLUMN_NAME,CONSTRAINT_NAME FROM information_schema.CONSTRAINT_COLUMN_USAGE
WHERE TABLE_NAME = 'student' -
查看字段缺省约束表达式 (即默认值等)
格式:select * from information_schema.columns where TABLE_NAME = table_name
SELECT TABLE_NAME, COLUMN_NAME, COLUMN_DEFAULT FROM information_schema.COLUMNS
WHERE TABLE_NAME='student' -
查看字段缺省约束名
格式:select name from sysobjects where object_id(table_name)=parent_obj and xtype=’D’
select name from sysobjects
where object_id('表?名?')=parent_obj and xtype='D' -
删除字段约束
格式:alter table tablename drop constraint constraintname
ALTER TABLE student DROP CONSTRAINT PK__student__2F36BC5B772B9A0B
-
添加字段约束
格式:alter table tablename add constraint constraintname primary key (column_name)
--示例1
ALTER TABLE stuInfo ADD CONSTRAINT PK_stuNo PRIMARY KEY (stuNo)
--示例2 添加主键约束(Primary Key)
-- 存在主键约束PK_stuNO,则删除IF EXISTS(SELECT * FROM sysobjects WHERE name='PK_stuNo' and xtype='PK')
Alter TABLE stuInfo
Drop Constraint PK_stuNo
Go-- 重新添加主键约束PK_stuNO
ALTER TABLE stuInfo ADD CONSTRAINT PK_stuNo PRIMARY KEY (stuNo)
Go--示例3 添加 唯一UQ约束(Unique Constraint)
-- 存在唯一约束UQ_stuNO,则删除IF EXISTS(SELECT * FROM sysobjects WHERE name='UQ_stuID' and xtype='UQ')
Alter TABLE stuInfo
Drop Constraint UQ_stuID
Go-- 重新添加唯一约束UQ_stuID
ALTER TABLE stuInfo ADD CONSTRAINT UQ_stuID UNIQUE (stuID)
--示例4 添加默认DF约束(Default Constraint)
-- 存在默认约束UQ_stuNO,则删除IF EXISTS(SELECT * FROM sysobjects WHERE name='DF_stuAddress' and xtype='D')
Alter TABLE stuInfo Drop Constraint DF_stuAddress
Go-- 重新添加默认约束DF_stuAddress
ALTER TABLE stuInfo ADD CONSTRAINT DF_stuAddress DEFAULT ('地址不详') FOR stuAddress
--示例5 检查CK约束(Check Constraint)
-- 存在检查约束UQ_stuNO,则删除IF EXISTS(SELECT * FROM sysobjects WHERE name='CK_stuAge' and xtype='C')
Alter TABLE stuInfo Drop Cons
相关文章
- 01-03SQL实现表名更改,列名更改,约束更改
- 01-03python-Django;如何更改django.contrib.sessions使用的表名?
- 01-03Sql从一张表中更改另一张表数据
- 01-03sql 如何更改表的字段属性
- 01-03【SQL Server】sql server更改了数据表的字段/新增数据表的字段 无法保存
- 01-03【转载,备忘】SQL Server 更改跟踪(Chang Tracking)监控表数据
- 01-03SQL SERVER增加、删除、更改表中的字段名 (详询请加qq:2085920154)
- 01-03sql server 2008 创建新数据库报错、创建表报错、更改表的设计报错
- 01-03SQL Server更改排序规则的实现过程
- 01-03sql server更改机器名后更改数据库机器名