Sql Server数据库操作

1.修改表的列字段

新加表字段:
alter table 表名 add 字段名 字段类型(字段类型大小);
例:

	alter table Student add Sage tinyint;
	alter table Student add Sbody varchar(100 );

表字段添加为主键
—添加主键
alter table [表名] add constraint [主键名] primary key(字段名1,字段名2)
例:

	alter table Course add constraint CId primary key(CId);

删除表字段:
alter table 表名 drop column 字段名;
例:

	alter table Student drop column Sbody;

修改表字段的类型或者大小:
alter table 表名 alter column 字段名 新字段类型(新字段类型大小);
例:

	#修改表字段类型为int
    alter table Course alter column CId int;
    #修改表字段不为空
	alter table Course alter column CId int not null;

修改表字段的名称:
EXEC sp_rename '表名.[原字段名]' , '新字段名' , 'COLUMN';
例句:

	exec sp_rename 'Student.[SId]','Sid','COLUMN';
	exec sp_rename 'Student.[name]','Sname','COLUMN';

2.表的创建

create table Course(
	CId int ,
	Cname varchar(50) not null,
	Tid int not null
);

identity包含两个参数,第一个参数表示起始值,第二个参数表示增量。
primary key:设为主键

create table Teacher(
	Tid int identity(1,1) primary key not null,
	Tname varchar(50) not null
);

猜你喜欢

转载自blog.csdn.net/weixin_42119415/article/details/108615721