sql server创建数据表的完整语法

1.创建数据库

语法:CREATE DATABASE 

复制

CREATE DATABASE dbname -- 创建名为 dbname 的数据库

复制

2.表的创建

 语法:
USE suntest  
create table 仓库  
(  
仓库编号 int ,   
仓库号 varchar(50) ,   
城市 varchar(50) ,   
面积 int  
)  
create table 仓库1  
(  
仓库编号 int not null ,   
仓库号 varchar(50) not null,   
城市 varchar(50) not null, --不能为空not null--  
面积 int  
)  
create table 仓库2  
(  
仓库编号 int primary key , --主键的关键字primary key--  
仓库号 varchar(50) unique, --唯一索引关键字unique--  
城市 varchar(50) not null, --不能为空not null--  
面积 int  
)  
create table 仓库3  
(  
仓库编号 int primary key , --主键的关键字primary key--  
仓库号 varchar(50) unique, --唯一索引关键字unique--  
城市 varchar(50) default '青岛', --不能为空not null--  
面积 int check (面积>=300 and 面积=800 and 基本工资=800 and 基本工资<=2100),  
加班工资 int,  
奖金 int,  
扣率 int,  
应发工资 as (基本工资+加班工资+奖金-扣率) --as为自动计算字段,不能输入值--  
) 

复制

3.在现有表中添加标识列

下面的例子向表T_test中添加一个名为ID,类型为int,种子为1,递增量为1的标识列
--创建表
CREATE TABLE T_test
(Name varchar(50)
)
--插入数据
INSERT T_test(Name) VALUES('张三')
--增加标识列
ALTER TABLE T_test
ADD ID int IDENTITY(1,1)

注:这只适用于刚建完表的情况,如果此时主键已经使用过了,表中存在许多数据,不能使用该方法删除主键,会导致数据丢失。(可行的方法,建一张相同的表来存储数据,在修改,插入)。

复制

4.创建外键

create table 表名(
列名1 参数,
列名2 参数,
foreign key(列名) references 目标表名(目标列名)
);

复制

5.添加外键

比如stuInfo(学生信息表)表是主表。他的主键是stuID,
另外还有一个stuExam表(学生考试成绩表)。在这个表中也有个列是stuID,但是要引用主表中的stuID.
那么在创建约束的时候:
alter table stuExam
add constraint fk_stuID foreign key(stuID) references stuInfo(stuID)
go

复制

6.约束

 primary key   		主键
 not null, 			不能为空not null
 unique, 			唯一索引关键字unique
 check (面积>=300 and 面积=0 and sAge<=100)   
 
外键约束  --FK,foreign key constraint
表关系
alter table Student add constraint Fk_Student_sClassId foreign key(sClassId) references Class(cId)
--指定表Student添加sClassId外键为Class的主键cId
on delete cascade on update  cascade --级联删除 --级联更新
 
删除约束
alter table Student drop Constraint NN_Student_sClassId    --删除指定表中的约束

复制

7.创建局部临时表

use db_sqlserver
go
create table #db_local_table
(
  id  int,
  name varchar(50),
  age int,
  area int
)
创建的临时表不能与其他会话共享,当会话结束时,行和表的定义都将被删除

复制

8.创建全局临时表

use db_sqlserver
go
create table ##db_local_table
(
  id  int,
  name varchar(50),
  age int,
  area int
)
全局临时表对所有用户都是可见的,在每个访问该表的用户都断开服务器连接时,全局临时表才会被删除

复制

9.创建具有check约束字段的数据库表

use db_sqlserver;
go
create table db_table7
(
  仓库编号 int primary key,
  职工号  varchar(50) unique,
  仓库号  varchar(50),
  工资   int,
  面积  int check(面积>=600 and 面积<=1800)
)

复制

10.创建含有计算字段的数据库表

use db_sqlserver;
go
create table db_table8
(
  职工编号 int primary key,
  职工号 varchar(50) unique,
  仓库号 varchar(50),
  基本工资 int check(基本工资>=800 and 基本工资<=2100),
  加班工资 int,
  奖金 int,
  扣率 int,
  应发工资 as (基本工资 + 加班工资 + 奖金 - 扣率)
)

复制

11.创建含有自动编号字段的数据库表

use db_sqlserver;
go
create table db_table9
(
   仓库编号 int identity(1,1) primary key,
   仓库号 varchar(50) unique,
   城市 varchar(50) default('青岛'),
   面积 int check(面积>=300 and 面积<=1800)
)

复制

12.创建含有排序字段和默认值的数据表

create table db_table10 
(
   仓库编号 int identity(1, 1) primary key,
   仓库号 varchar(50) collate french_CI_AI not null,
   城市 varchar(50) default '青岛',
   面积 int check(面积>=300 and 面积<=1800)
)

复制

13.动态判断数据库表是否存在

use db_sqlserver;
go
if(Exists(select * from sys.sysobjects where id=OBJECT_ID('db_table9')))
  print '数据库表名已经存在'
  
else 
  print '该数据库表名不存在,可以利用该名创建表'

复制

14.查看表的各种信息,可以查看指定数据库表的属性、表中字段属性、各种约束等信息

use db_sqlserver;
go
execute sp_help db_table9;

复制

15.用select语句查看数据库表的属性信息

use db_sqlserver;
go
select * from sysobjects where type='U'

复制

16.重命名数据库表

use db_sqlserver;
go
execute sp_rename "db_table9", "db_renametable"

复制

17.增加数据库表的新字段

use db_sqlserver;
go
alter table db_table1 add 电子邮件 varchar(50)
alter table db_table1 add 联系方式 varchar(50) default '0532-88886396'
 
select name 字段名, xusertype 类型编号, length 长度 from syscolumns where id = object_id('db_table1')

复制

18.修改数据库表的字段

use db_sqlserver;
go
alter table db_table1 alter column 电子邮件 varchar(200)
 
 
select name 字段名, xusertype 类型编号, length 长度 from syscolumns where id = object_id('db_table1')

复制

19.删除数据库表字段

use db_sqlserver;
go
alter table db_table1 drop column 电子邮件 
 
 
select name 字段名, xusertype 类型编号, length 长度 from syscolumns where id = object_id('db_table1')

复制

20.删除数据库表

use db_sqlserver;
go
drop table db_table1
drop table db_table1, db_table2

本文来自网络,不代表协通编程立场,如若转载,请注明出处:https://www.net2asp.com/c19f5cb507.html