语法:CREATE DATABASE <数据库名称>
复制
CREATE DATABASE dbname -- 创建名为 dbname 的数据库
复制
语法: 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 面积<=1800) ) create table 职工表 ( 职工编号 int identity (1,1) primary key, 职工号 varchar(50) unique, 仓库号 varchar(50), 工资 int check(基本工资>=800 and 基本工资<=2100), ) create table 订单表 ( 订单编号 int identity(1,1) primary key, 订单号 varchar(50) unique, 职工号 varchar(50) references 职工表(职工号),--references两张表通过“职工号”关联-- 订购日期 datetime, 销售金额 int ) create table 阳光工资表 ( 职工编号 int identity (1,1) primary key, 职工号 varchar(50) unique, 仓库号 varchar(50), 基本工资 int check(基本工资>=800 and 基本工资<=2100), 加班工资 int, 奖金 int, 扣率 int, 应发工资 as (基本工资+加班工资+奖金-扣率) --as为自动计算字段,不能输入值-- )
复制
下面的例子向表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)
注:这只适用于刚建完表的情况,如果此时主键已经使用过了,表中存在许多数据,不能使用该方法删除主键,会导致数据丢失。(可行的方法,建一张相同的表来存储数据,在修改,插入)。 
复制
create table 表名( 列名1 参数, 列名2 参数, foreign key(列名) references 目标表名(目标列名) );
复制
比如stuInfo(学生信息表)表是主表。他的主键是stuID, 另外还有一个stuExam表(学生考试成绩表)。在这个表中也有个列是stuID,但是要引用主表中的stuID. 那么在创建约束的时候: alter table stuExam add constraint fk_stuID foreign key(stuID) references stuInfo(stuID) go
复制
 primary key   		主键
 not null, 			不能为空not null
 unique, 			唯一索引关键字unique
 check (面积>=300 and 面积<=1800)    check 约束
约束
非空约束 --NN,ont null constraint
必须填写数据不能为空
--指定表 Student 添加名为NN_Student_sClassId非空约束(指定列名sClassId),括号输入表达式
alter table Student add constraint NN_Student_sClassId check(sClassId is not null)
 
主键约束 --PK,primary key constraint
唯一且不为空
--指定表 Student 添加名为PK_Student_sId主键约束(指定列名sId)
alter table Student add constraint PK_Student_sId primary key(sId)
 
唯一约束 --UQ,unique constraint
唯一,允许为空,但是同样的数据只能出现一次
--指定表 Student 添加名为UQ_Student_sName唯一约束(指定列名sName)
alter table Student add constraint UQ_Student_sName unique(sName)
    
默认约束 --DF,default constraint
设置默认值
--指定表 Student 添加名为DF_Student_sName默认约束(指定列名sBirthday),获取当前日期
alter table Student add constraint DF_Student_sName default(getdate()) for sBirthday
 
--指定表 Student 添加名为DF_Student_sName默认约束(指定列名sBirthday),指定日期  
alter table Student add constraint DF_Student_sName default('1995-12-12') for sBirthday  
--指定表 Student 添加名为DF_Student_sName默认约束(指定列名sSex),指定性别
alter table Student add constraint DF_Student_sSex default('男') for sSex     
 
检查约束 --CK,check constraint
设置范围以及格式限制
--指定表 Student 添加名为 CK_Student_sSex检查约束(指定列名sSex),限制为'男'或者'女'
alter table Student add constraint CK_Student_sSex check(sSex='男' or sSex='女')   
--指定表 Student 添加名为 CK_Student_sSex检查约束(指定列名sAge),限制为0-100之间的数字
alter table Student add constraint CK_Student_sAge check(sAge>=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    --删除指定表中的约束 
复制
use db_sqlserver go create table #db_local_table ( id int, name varchar(50), age int, area int ) 创建的临时表不能与其他会话共享,当会话结束时,行和表的定义都将被删除
复制
use db_sqlserver go create table ##db_local_table ( id int, name varchar(50), age int, area int ) 全局临时表对所有用户都是可见的,在每个访问该表的用户都断开服务器连接时,全局临时表才会被删除
复制
use db_sqlserver; go create table db_table7 ( 仓库编号 int primary key, 职工号 varchar(50) unique, 仓库号 varchar(50), 工资 int, 面积 int check(面积>=600 and 面积<=1800) )
复制
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 (基本工资 + 加班工资 + 奖金 - 扣率) )
复制
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)
) 
复制
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) )
复制
use db_sqlserver;
go
if(Exists(select * from sys.sysobjects where id=OBJECT_ID('db_table9')))
  print '数据库表名已经存在'
  
else 
  print '该数据库表名不存在,可以利用该名创建表' 
复制
use db_sqlserver; go execute sp_help db_table9;
复制
use db_sqlserver; go select * from sysobjects where type='U'
复制
use db_sqlserver; go execute sp_rename "db_table9", "db_renametable"
复制
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') 
复制
use db_sqlserver;
go
alter table db_table1 alter column 电子邮件 varchar(200)
 
 
select name 字段名, xusertype 类型编号, length 长度 from syscolumns where id = object_id('db_table1') 
复制
use db_sqlserver;
go
alter table db_table1 drop column 电子邮件 
 
 
select name 字段名, xusertype 类型编号, length 长度 from syscolumns where id = object_id('db_table1') 
复制
use db_sqlserver; go drop table db_table1 drop table db_table1, db_table2