当前位置: 移动技术网 > IT编程>数据库>MSSQL > sql server 表结构修改方法

sql server 表结构修改方法

2017年12月12日  | 移动技术网IT编程  | 我要评论

如果我们需要修改sql server表结构,应该怎么做呢?下面就将教您如何修改sql server表结构的方法,希望对您学习sql server表结构方面能够有所帮助。

向sql server表中增加一个varchar列
alter table distributors add column address varchar(30);
从sql server表中删除一个字段:
alter table distributors drop column address restrict;
在一个操作中修改两个现有字段的类型:
alter table distributors
alter column address type varchar(80),
alter column name type varchar(100);
使用一个 using 子句, 把一个包含 unix 时间戳的 integer 字段转化成 timestamp with time zone:
alter table foo
alter column foo_timestamp type timestamp with time zone
using
timestamp with time zone 'epoch' + foo_timestamp * interval '1 second';

对现存字段改名
alter table distributors rename column address to city;
更改现存sql server表的名字:
alter table distributors rename to suppliers;
给一个字段增加一个非空约束:
alter table distributors alter column street set not null;
从一个字段里删除一个非空约束:
alter table distributors alter column street drop not null;
给一个表增加一个检查约束:
alter table distributors add constraint zipchk check (char_length(zipcode) = 5);
删除一个表和它的所有子表的监查约束:
alter table distributors drop constraint zipchk;

向表中增加一个外键约束
alter table distributors add constraint distfk foreign key (address) references addresses(address) match full;
给表增加一个(多字段)唯一约束:
alter table distributors add constraint dist_id_zipcode_key unique (dist_id, zipcode);
给一个表增加一个自动命名的主键约束,要注意的是一个表只能有一个主键:
alter table distributors add primary key (dist_id);
把表移动到另外一个表空间:
alter table distributors set tablespace fasttablespace;

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网