数据库知识总结(表结构操作)

发表于:2016-10-26 10:04

字体: | 上一篇 | 下一篇 | 我要投稿

 作者:李玮    来源:51Testing软件测试网采编

分享:
  1、创建表Scores
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
《2023软件测试行业现状调查报告》独家发布~

关注51Testing

联系我们

快捷面板 站点地图 联系我们 广告服务 关于我们 站长统计 发展历程

法律顾问:上海兰迪律师事务所 项棋律师
版权所有 上海博为峰软件技术股份有限公司 Copyright©51testing.com 2003-2024
投诉及意见反馈:webmaster@51testing.com; 业务联系:service@51testing.com 021-64471599-8017

沪ICP备05003035号

沪公网安备 31010102002173号