一、建库 

--创建数据库
create database DBTEST1
on --数据文件
(
	name = 'DBTEST1', --逻辑名称
	filename = 'D:\SQLdata\DBTEST1.mdf', --物理路径和名称
	size = 5MB, --文件的初始大小
	filegrowth = 2MB --文件的增长
)
log on --日志文件
(
	name = 'DBTEST1_log', --逻辑名称
	filename = 'D:\SQLdata\DBTEST1_log.ldf', --物理路径和名称
	size = 5MB, --文件的初始大小
	filegrowth = 2MB --文件的增长
)

create database DBTEST2

 二、建表

--切换数据库
use DBTEST
--创建表的基本语法----------------------------------------------------------
--create table 表名
--(
--	字段名1 数据类型
--	字段名2 数据类型
--)

--判断表是否存在
if exists(select*from sys.objects where name='Department' and type='U')
	drop table Department
--建表(部门,职级,员工)
create table Department
(
  --部门编号,primary key:主键,identity(1,1):自动增长,初始值1,增长步长1
  DepartmentId int primary key identity(1,1),
  --部门名称
  DepartmentIdName nvarchar(50) not null,
  --部门描述
  DepartmentRemark text
)
--char:定长,char(10),无论存储数据是否真的到了10个字节,都要占用10字节。
--varchar:变长,varchar(10),最多占用10个字节。
--text:长文本
--char,varchar,text前面加n:存储unicode字符,对中文友好。
--varchar(100):存储100个字母或者50个汉字。
--nvarchar(100):存储100个字母或者100个汉字。


create table [Rank]
(
  --职级编号,primary key:主键,identity(1,1):自动增长,初始值1,增长步长1
  RankId int primary key identity(1,1),
  --职级名称
  RankName nvarchar(50) not null,
  --职级描述
  RankRemark text
)

--员工
create table People
(
	PeopleId int primary key identity(1,1),--员工编号
	DepartmentId int references Department(DepartmentId) not null,--部门(引用外键)
	RankId int references [Rank](RankId) not null,--职级(引用外键)
	PeopleName nvarchar(50) not null,--姓名
	PeopleSex nvarchar(1) default('男') check(PeopleSex='男'or PeopleSex='女'),--性别
	PeopleBirth datetime not null,--生日
	PeopleSalary decimal(12,2) check(PeopleSalary>=1000 or PeopleSalary<=1000000) not null,--月薪
	PeoplePhone varchar(20) unique not null,--电话
	PeopleAddress varchar(300),--地址
	PeopleAddTime date default(getdate())--添加时间
)

--修改表结构--------------------------------------------------------------
--(1)添加列
--alter table 表名 add 新列表 数据类型
--给员工表添加一列邮箱
alter table People add PeopleMail varchar(200)

--(2)删除列
--alter table 表名 drop column 列名
--删除邮箱这一列
alter table People drop column PeopleMail

--(3)修改列
--alter table 表名 alter column 列名 数据类型
--修改地址varchar(300)为varchar(200)
alter table People alter column PeopleAddress varchar(200)

--维护约束(删除,添加)

--删除约束
--alter table 表名 drop constraint 约束名
--删除月薪的约束
alter table People drop constraint CK__People__PeopleSa__49C3F6B7

--添加约束(check约束)
--alter table 表名 add constraint 约束名 check(表达式)
--添加工资字段约束,在1000-1000000之间
alter table People add constraint CK__People__PeopleSa_1
check(PeopleSalary>=1000 or PeopleSalary<=1000000)

--添加约束(check约束)
alter table 表名 add constraint 约束名 check(表达式)
--添加约束(主键)
alter table 表名 add constraint 约束名 primary key(列名)
--添加约束(唯一)
alter table 表名 add constraint 约束名 unique(列名)
--添加约束(默认值)
alter table 表名 add constraint 约束名 default 默认值 for 列名
--添加约束(外键)
alter table 表名 add constraint 约束名 foreign key(列名)
references 关联表名(列名)

三、插入数据 

--向部门表插入数据
insert into Department(DepartmentIdName,DepartmentRemark)
values('市场部','…………')
insert into Department(DepartmentIdName,DepartmentRemark)
values('软件部','…………')
insert into Department(DepartmentIdName,DepartmentRemark)
values('企划部','…………')
--简写
insert into Department values('硬件部','…………')
--一次性插入多行数据
insert into Department(DepartmentIdName,DepartmentRemark)
select '测试部','…………' union
select '实践部','…………' union
select '产品部','…………'

--向职级表插入数据---------------------------------------
insert into [Rank](RankName,RankRemark)
values('初级','……')
insert into [Rank](RankName,RankRemark)
values('中级','……')
insert into [Rank](RankName,RankRemark)
values('高级','……')

--向员工表插入数据---------------------------------------
insert into People values(7,1,'刘备','男','1987-8-8',12000,'13888888888','荆州',getdate())
insert into People values(2,2,'关羽','男','1988-8-8',50000,'13345675345','荆州',getdate())
insert into People values(1,1,'孙尚香','女','1987-7-9',2000,'13834545678','成都',getdate())
insert into People values(2,3,'小乔','女','1998-2-6',15000,'13842532343','上海',getdate())
insert into People values(5,2,'周瑜','男','1965-5-12',20000,'13835434500','广州',getdate())
insert into People values(3,1,'鲁肃','男','1967-3-11',5300,'13833233233','深圳',getdate())
insert into People values(3,2,'吕蒙','男','1998-8-1',8000,'13987653432','天津',getdate())
insert into People values(5,3,'陆逊','男','1923-1-4',9000,'13426787653','上海',getdate())
insert into People values(4,2,'太史慈','男','1932-4-22',2000,'13234567654','佛山',getdate())
insert into People values(6,1,'黄盖','男','1956-10-11',10000,'11234564435','广州',getdate())
insert into People values(1,3,'貂蝉','女','1934-11-8',4000,'16767686532','湛江',getdate())
insert into People values(2,1,'典韦','男','1947-3-23',9000,'13345265234','杭州',getdate())
insert into People values(3,3,'曹操','男','1904-2-10',15000,'17675437865','北京',getdate())
insert into People values(4,2,'许褚','男','1923-6-13',12000,'17876543087','上海',getdate())
insert into People values(5,1,'曹仁','男','1956-2-11',35000,'15657859098','石家庄',getdate())
insert into People values(6,2,'孙策','男','1928-3-19',50000,'13345648955','郑州',getdate())
insert into People values(7,3,'孙权','男','1978-1-30',50000,'15634765678','哈尔滨',getdate())
insert into People values(6,2,'大乔','女','1913-4-16',21000,'19876546754','长沙',getdate())
insert into People values(7,2,'aaaa','男','1947-2-22',25000,'15678765464','重庆',getdate())
insert into People values(1,1,'张飞','男','1911-9-30',11000,'13245645656','北京',getdate())
insert into People values(4,1,'马超','男','1994-3-6',8000,'13450987907','海南',getdate())