这篇文章是看b站上数据库基础入门学习视频的笔记,指路:【SQL SERVER数据库_D丝学编程】 https://www.bilibili.com/video/BV1XV411C7TP/?share_source=copy_web&vd_source=312df205f0bcaec87ec6255f76082f05
目录
一、切换数据库
use DBTSET
DBTEST是自己定义的数据库,根据需求使用 USE 语句可以切换当前使用的数据库。
二、创建表(含判断是否已存在)
创建表基本语法
create table 表名
(
字段名1 数据类型字段名2 数据类型
)
使用系统视图 sys.objects 判断表是否存在,避免重复创建:
--数据库找databases,表找objects
if not exists(select*from sys.objects where name='Department' and type='U')
begin
create table Department
(
--部门编号,一般会找一个主键primary key,主键里不能重复
DepartmentId int primary key identity(1,1),
DepartmentName nvarchar(50) not null,
DepartmentRemark text
);
end
数据类型说明:
-
char(n):定长字符串,char(10),占用固定空间10个字节,但不能超10个字节 -
varchar(n):变长字符串,varchar(10),最多占用10个字节。varchar(100)存储100个字母或者50个汉字。 -
nvarchar(100):可以存储100个字母和100个汉字。
-
char,varchar,text前面加n:存储unicode字符,对中文友好。 -
not null :不能为空
-
type='U':U表示用户定义的表
-
text:超长文本。 -
decimal(12,2):用于存储精确数值,如工资。 -
identity(1,1):自动增长,初始值1,增长的步长为1
三、多个表创建示例
部门表 Department
create table Department
(
DepartmentId int primary key identity(1,1),
DepartmentName nvarchar(50) not null,
DepartmentRemark text
)
职级表 [Rank]
注意:Rank 是 SQL 保留关键字,加上 [] 可用于命名。
create table [Rank]
(
--职级编号
RankId int primary key identity(1,1),
--职级名称
RankName nvarchar(50) not null,
--职级描述
RankRemark text
)
员工表 People
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='女') not null,
PeopleBirth smalldatetime not null,--生日
PeopleSalary decimal(12,2) check(PeopleSalary>=1000 and PeopleSalary<=1000000) not null,--月薪
PeoplePhone varchar(20) unique not null,--电话
PeopleAddress varchar(300),--地址
PeopleAddTime smalldatetime default(getdate())--添加时间
)
- date:可以存储年月日,datetime类型:可以存储更多,smalldatetime:占用空间更小,可以用的时间是近期
- references:引用外键,防止加的东西在关联表里不存在
- check:约束
- default:默认值
- decimal:(总长度,小数点后面位数)
- unique:表示数据必须唯一
- getdate():获得当前时间
四、修改表结构
添加列
alter table 表名 add 新列名 数据类型
--给员工表添加一列邮箱
alter table People add PeopleMail varchar(200)
删除列
alter table 表名 drop column 列名
--删除邮箱这一列
alter table People drop column PeopleMail
修改列
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__74AE54BC
其中约束名查询 方法之一:



添加约束
Check 约束
alter table 表名 add constraint 约束名 check(表达式)
--添加工资字段约束,工资必须在1000-1000000之间
alter table People add constraint CK_People_PeopleSal
check(PeopleSalary>=1000 and PeopleSalary <= 1000000)
主键约束
alter table 表名 add constraint 约束名 primary key(列名)
唯一约束
alter table 表名 add constraint 约束名 unique(列名)
默认值约束
alter table 表名 add constraint 约束名 default 默认值 for 列名
外键约束
alter table 表名 add constraint 约束名 foreign key(列名)
references 关联表名(列名(主键))
六、完整代码:
--切换数据库
use DBTSET
--创建表基本语法
--create table 表名
--(
-- 字段名1 数据类型
-- 字段名2 数据类型
--)
--注释快捷键 ctrl+k+c
--数据库找databases,表找objects,type='U':U表示用户定义的表
if not exists(select*from sys.objects where name='Department' and type='U')
--drop table Department
--建表(部门,职级,员工)
begin
create table Department
(
--部门编号,一般会找一个主键primary key,主键里不能重复,identity(1,1):自动增长,初始值1,增长的步长为1
DepartmentId int primary key identity(1,1),
--nvarchar:字符串,not null:不能为空
DepartmentName nvarchar(50) not null,
--text:长文本
DepartmentRemark text
);
end
--char:定长,char(10),char(10),占用固定空间10个字节,但不能超10个字节
--varchar:变长,varchar(10),最多占用10个字节
--text:长文本
--char,varchar,text前面加n:存储unicode字符,对中文友好。
--varchar(100):存储100个字母或者50个汉字,
--nvarchar(100):可以存储100个字母和100个汉字
--加个方括号,是因为Rank本身是关键字,加个方括号就可以用来命名了
if not exists(select*from sys.objects where name='Rank' and type='U')
begin
create table [Rank]
(
--职级编号
RankId int primary key identity(1,1),
--职级名称
RankName nvarchar(50) not null,
--职级描述
RankRemark text
);
end
--员工
if not exists(select*from sys.objects where name='People' and type='U')
begin
create table People
(
PeopleId int primary key identity(1,1),--员工编号
--部门,添加部门表的主键就可以获得那个表的所有信息
--references:引用外键,防止加的东西在关联表里不存在
DepartmentId int references Department(DepartmentId) not null,
--职级
RankId int references [Rank](RankId) not null,
PeopleName nvarchar(50) not null,
--check:约束,default:默认值
PeopleSex nvarchar(1) default('男') check(PeopleSex='男'or PeopleSex='女') not null,
--date:可以存储年月日,datetime类型:可以存储更多,smalldatetime:占用空间更小,可以用的时间是近期
PeopleBirth smalldatetime not null,--生日
--decimal:(总长度,小数点后面位数)
PeopleSalary decimal(12,2) check(PeopleSalary>=1000 and PeopleSalary<=1000000) not null,--月薪
--unique:表示数据必须唯一
PeoplePhone varchar(20) unique not null,--电话
PeopleAddress varchar(300),--地址
--getdate():获得当前时间
PeopleAddTime smalldatetime default(getdate())--添加时间
);
end
--修改表结构
--(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__74AE54BC
--添加约束(check约束)
--alter table 表名 add constraint 约束名 check(表达式)
--添加工资字段约束,工资必须在1000-1000000之间
alter table People add constraint CK_People_PeopleSal
check(PeopleSalary>=1000 and PeopleSalary <= 1000000)
--添加约束(主键)
alter table 表名 add constraint 约束名 primary key(列名)
--添加约束(唯一)
alter table 表名 add constraint 约束名 unique(列名)
--添加约束(默认值)
alter table 表名 add constraint 约束名 default 默认值 for 列名
--添加约束(外键)
alter table 表名 add constraint 约束名 foreign key(列名)
references 关联表名(列名(主键))
5903

被折叠的 条评论
为什么被折叠?



