当前位置:Gxlcms > 数据库问题 > SQL 表结构操作

SQL 表结构操作

时间:2021-07-01 10:21:17 帮助过:25人阅读

1 create table Scores               --表名
2 (Id int identity(1,1) primary key,--设置主键,并且行号自增,
identity(1,1)表示Id列从1开始自增,每次增加1 3 Date datetime not null, --设置类型为datetime,不能为空 4 Name nvarchar(50) not null, 5 Score nvarchar(2) --默认状态下,类型为空 6 )
技术分享

2.修改表名Scores为NewScores

 1 exec sp_rename ‘Scores‘,‘NewScores‘ 

3.删除表Scores

 1 drop table Scores --删除表(表的结构、属性以及索引也会被删除) 

4.清空表数据

 1 truncate table Scores --仅仅删除表格中的数据 

5.修改列Score为 not null

1 alter table Scores
2 alter column Score nvarchar(2) not null

6.添加列

1 alter table Scores 
2 add new nvarchar(20) not null

7.修改列名

1 exec sp_rename ‘Scores.Name‘,‘NewName‘,‘column‘

8.删除列

1  alter table Scores 
2  drop column new 

9.修改identity列

 如果说创建表时没有设置自增列。因为自增列不能直接修改,必须降原有Id列删除,然后重新添加一列具有identity属性的Id字段。

1  exec sp_pkeys @table_name=‘Scores‘                               --查询主键名
2  alter table Scores drop constraint PK__Scores__3214EC074E3D66D2  --将主键约束先删去
3  alter table Scores drop column Id                                --将Id列删去
4  alter table Scores add Id int identity(1,1)                      --添加自增列(此时Id列不是主键)
5  alter table Scores add Id int identity(1,1) constraint pk primary key  --添加自增列,设置Id为主键名字为pk

 

SQL 表结构操作

标签:表格   创建表   结构   code   null   lte   ble   exec   com   

人气教程排行