今天主要学习的是数据库的约束
保证数据库的完整性是从建表的时候开始的,完整性的问题大多是由设计引起的,所以创建表的时候,就应当保证数据输入的正确性,错误的数据,不符合要求的数据不允许输入,简单说,保证完整性,就是创建表的时候为他实施完整性的约束
完整性约束:
域完整性--就是对字段的约束
1.数据类型约束
文本数据类型--
char(文本长度):固定长度的非Unicode字符,单字节字符占一个长度单位,双字节字符占两个长度单位,最大长度8000(一个Unicode占16位)
varchar(文本长度):可变长度的非Unicode字符
nchar(文本长度):固定长度Unicode字符,单双字节都占一个长度单位,最大长度4000
nvarchar(文本长度):可变长度Unicode字符
text:非Unicode长文本字符
ntext:Unicode长文本字符
数字数据类型--
整数型:int
浮点数型:float
Create Table [test].dbo.[asd]
([id] int ,
[name] varchar(50),
[age] int,
[sex] char(2)
)
2.非空约束:Not Null
数据内容不允许为空,空指的是没有任何数据,''不代表空
Create Table [test].dbo.[asd]
([id] int Not Null,
[name] varchar(50),
[age] int,
[sex] char(2)
)
3.主键约束:Primary Key/Primary Key(字段名...)(主键约束默认是有排序,默认有聚合索引)
一张表只能有一个主键,主键字段内容非空且不重复,可以将多个字段作为主键(任然是一个主键),多个字段内容不能同时重复。通常情况下用单字段做主键,且主键不能为空。
Create Table [test].dbo.[asd]
([id] int Primary Key,
[name] varchar(50),
[age] int,
[sex] char(2)
)
Create Table [test].dbo.[asd]
([id] int Primary Key,
[name] varchar(50) Primary Key([id]),
[age] int,
[sex] char(2)
)
如果有多个字段主键
Create Table [test].dbo.[asd]
([id] int Primary Key,
[name] varchar(50) Primary Key([id],[name]),
[age] int,
[sex] char(2)
)
索引:方便数据的查询,提高查询效率,降低修改的效率。
聚合索引、将数据本身作为索引项
非聚合索引、在数据库中开辟索引空间
4.唯一键约束:Unique
一张表可以有多个唯一键字段,唯一键字段内容不重复。
Create Table [test].dbo.[asd]
([id] int Unique,
[name] varchar(50) ,
[age] int,
[sex] char(2)
)
5.标识列:Identity(种子,增长量)
非空,只能对整型数据字段设置,标识列不能编辑,会自动增长,增长不会因意外停止。
标识列是由种子(起始量)与增长量组成。
Create Table [test].dbo.[asd]
([id] int Identity(1,2),
[name] varchar(50) ,
[age] int,
[sex] char(2)
)
6.默认值绑定:Default 默认值
不输入数据的时候有一个默认数据值
Create Table [test].dbo.[asd]
([id] int ,
[name] varchar(50) ,
[age] int,
[city] varchar(50) Default '上海',
[sex] char(2)
)
引用完整性--
7.外键约束:
Foreign Key References [库名].架构名.表名
Foreign Key(外键字段名) References [库名].架构名.表名
主表于外键表中相对应的字段,只有出现在主表中的内容,才能出现在外键表的外键字段中。主表关联字段的值必须具有唯一属性(主键、唯一键),外键引用字段的数据类型必须与主键字段一致
Create Table [test].dbo.[asd]
([id] int primary key ,
[name] varchar(50) ,
[age] int,
[city] varchar(50),
[sex] char(2)
)
Create Table [test].dbo.[wer]
([id] int Foreign Key References [test].dbo.[asd]([id]),
[coursee] varchar(50),
)
Create Table [test].dbo.[asd]
([id] int primary key ,
[name] varchar(50) ,
[age] int,
[city] varchar(50),
[sex] char(2)
)
Create Table [test].dbo.[wer]
([id] int,
[coursee] varchar(50) Foreign Key (id) References [test].dbo.[asd]([id]),
)
自定义完整性--
8.检查约束(check约束):Check(表达式)
自定义约束条件来约束字段值的内容。
Create Table [test].dbo.[asd]
([id] int primary key ,
[name] varchar(50) check (sex='男' or sex='女') ,
[age] int,
[city] varchar(50),
[sex] char(2)
)
Create Table [test].dbo.[asd]
([id] int primary key ,
[name] varchar(50) ,
[age] int,
[city] varchar(50),
[sex] char(2) check (sex='男' or sex='女')
)
Create Table [test].dbo.[asd]
([id] int primary key ,
[name] varchar(50) ,
[age] int check(age beteween 20 and 40)),
[city] varchar(50),
[sex] char(2) check (sex='男' or sex='女'),
[email] varchar(50) check(email like '%_@%_.%_')
)
书写方式
sex='男' or sex='女'
性别='男' or 性别='女'
年龄 大于 20岁 小于 40岁
age>20 and age<40
age between 20 and 40
between 值1 and 值2
in (值1,值2..)
通配符:使用的是like
% 0到N个任意字符
_ 1个任意字符
[范围] 范围内的一个任意字符
[^范围] 范围外的一个任意字符
%或者%的意思就是1到N个任意字符
email固定格式写法
email like '%@%.%_'
S001-S009怎么写
id like 's[0-9][0-9][09]' and id not like 's000'
等于 =
大于 >
大于等于 >=
小于 <
小于等于 <=
不等于 != <>
逻辑与 and
逻辑或 or
逻辑非 not
创建数据库
--单行注释
书写模板
Create DataBase [数据库名]
On
(
Name=逻辑名,
FileName='文件路径\文件名',
Size=初始大小MB,
MaxSize=最大大小MB/UnLimited,
FileGrowth=增长方式MB/百分数%
),
(
Name=逻辑名,
FileName='文件路径\文件名',
Size=初始大小MB,
MaxSize=最大大小MB/UnLimited,
FileGrowth=增长方式MB/百分数%
)
Log On
(
Name=逻辑名,
FileName='文件路径\文件名',
Size=初始大小MB,
MaxSize=最大大小MB/UnLimited,
FileGrowth=增长方式MB/百分数%
)
Create DataBase [test]
On
(
Name=test,
FileName='d:/test.mdf',
Size=8MB,
MaxSize=UnLimited,
FileGrowth=64MB
),
(
Name=test_1,
FileName='d:/test_1.ndf',
Size=8MB,
MaxSize=UnLimited,
FileGrowth=64MB
)
Log On
(
Name=test_log,
FileName='d:/test_log.ldf',
Size=8MB,
MaxSize=UnLimited,
FileGrowth=64MB
)
Go 将之前的语句批处理先执行
Use [库名]
创建表
Create Table [库名].架构名.[表名]
([字段名] 字段类型 约束条件,
[字段名] 字段类型 约束条件,
....
[字段名] 字段类型 约束条件
)
Create Table [test].dbo.[asd]
([id] int ,
[name] varchar(50),
[age] int
[sex] char(2)
)