设置
注意: 设置完成之后 重新启动
创建数据库
注意: 这个目标路径必须要有该文件名的文件夹
-- 指向 master 数据库,告诉它我们要创建一个新的数据库操作了
use master
go-- 创建数据库
create database StudentManageDB
on primary
(-- 以下四个组成部分缺一不可(注意在数据库文件中字符串要用单引号'',没有双引号""):-- 数据库文件的逻辑名(数据库服务内部使用的,外部是看不见的,叫什么名字不重要,只要不重复进行)name='StudentManageDB_data',-- 数据库物理文件名(绝对路径,SQLServe_DB 文件夹需要提前创建好),通常命名:逻辑名.mdffilename='F:\SQLServe_DB\StudentManageDB_data.mdf',-- 数据库文件初始大小(依据企业的具体项目而定)size=10MB,-- 数据文件增常量(当size大小不够用时,以一定的值来扩充,增常量的大小依据size而定)filegrowth=5MB
)
-- 创建日志文件
log on
(-- 理解同上name='StudentManageDB_log',filename='F:\SQLServe_DB\StudentManageDB_log.ldf',,size=5MB,filegrowth=1MB
)
-- 最后需要增加一个 go,代表结束了(不能加在 log on 上面,因为上面是一个整体)
go-- 然后点击 执行 按钮(如果报错,双击错误信息,可以快速定位到错误的地方)
-- 执行正常会提示:命令已成功完成。
删除
-- 指向 master 数据库,告诉它我们要创建一个新的数据库操作了
use master
go-- 判断当前数据库是否存在(在 master 里面有一个 sysdatabases 数据表,存放的是数据库信息)
-- 如果能从 sysdatabases 中找到 StudentManageDB 数据库,会返回一个结果集,那么 exists 发现有结果集的话,就会返回true
-- 在 SQL 文件中,你选中某一段代码,点击执行,可以只执行某一段代码,比如选中:select * from sysdatabases
if exists (select * from sysdatabases where name='StudentManageDB')
-- 删除数据库
drop database StudentManageDB
go
附加
SQLServer数据类型
文本类型
文本类型:字符数据包含任意字母、符号或数字字符的组合
-
char:固定长度的非 Unicode 字符数据,最大长度为8000个字符
-
比说说定义性别 Gender char(2),一个汉字占2个字符,所以定义 char(2)就行避免造成空间浪费(当然定义成 nchar(1)也行)
-
-
varchar:可变长度的非 Unicode 数据,最大长度为8000个字符
-
比说说定义姓名 Name varchar(20),因为姓名的长度不固定,可能2个汉字也可能4个汉字甚至更多,所以我们定义为可变长度。占的空间少了,可以节约出来一部分空间,占多了顶多也就20个字符了(这里名字最多10个汉字)。
-
-
text:存储长文本信息,最大长度为2^31-1 (2147483647)个字符
-
存储的字符范围就比较多了。如果定义的字符超过 char的8000个字符,那么就需要定义为 text 长文本类型的了。
-
-
nchar:固定长度的 Unicode 数据,最大长度为4000个字符
-
nvarchar:可变长度的 Unicode 数据,最大长度为4000个字符
-
ntext:存储可变长度的长文本,2^30-1 (1073741823)个字符
Unicode:是国际组织制定的可以容纳世界上所有文字和符号的字符编码方案 注意:带n的数据类型长度是不带n的两倍。比如nchar(1)和char(2)长度相同(1个字节 nchar 等于 声明2个字节的char,所以带 n 的最大长度就降低了)
整数类型
整数类型:
-
bigint:占用8个字节,可表示范围:-2^63 ~ 2^63-1之间的整数
-
int:占用4个字节,可表示范围:-2^31 ~ 2^31-1之间的整数
-
smallint:占用2个字节,可表示范围:-2^15 ~ 2^15-1之间的整数
-
tinyint:占用1个字节,可表示范围:0 ~ 255之间的整数
精确数字类型
精确数字类型:
-
decimal:-10^38 ~ 10^38-1之间的固定精度和小数位的数字
-
numeric:功能等同于decimal
-
写法:decimal(整数,小数)和numeric(整数,小数)
-
默认:如果不指定位数,默认18位整数,0位小数,比如身份证号:numeric(18,0),当然身份证号一般不用 numeric,而是用 char,因为身份证号有的还含有 X 字符
近似数字(浮点)类型
近似数字(浮点)类型:
-
float[(n)]:表示范围:-1.79E+308 ~ 1.79E+308 (1.79乘以10的308次幂)
-
n表示精度,在1-53之间取值,当n在1-24之间时,精度为7位有效数字,占用4个字节;当n在25-53之间时,精度为15位有效数字,占用8个字节
-
real:表示范围: -3.40E+38 ~ 3.40E+38占用4个字节存储空间,相当于float(24)
日期类型
日期类型:
-
datetime:允许的范围1753-1-1至9999-1-1
-
smalldatetime:允许的范围1900-1-1至2079-6-6
-
时间精度不同:datetime精确到3/100秒;smalldatetime精确到1分钟
-
格式说明:
-
分隔符数字方式:2023-04-24或04/24/2023
-
纯数字方式:04242023
-
英文数字方式:Apr 24,2023
-
-
注意问题:日期在使用的时候需要使用单引号''括起来
货币类型
货币类型:
-
money:货币数值介于 -2^63 ~ 2^63-1之间,精确到货币单位的千分之一。
-
smallmoney:货币数据介于 -214748.3648 ~ -214748.3648之间,精确到货币单位的千分之十。
位类型
位类型:
-
bit:表示“是/否”类型的数据。(0、1 / true、false)
二进制类型
二进制类型:
-
binary:固定长度的二进制数据,最大长度为8000个字节。
-
vbinary:可变长度的二进制数据,其最大长度为8000个字节。
-
image:可变长度的二进制数据,其最大长度为2^31个字节。应用场合:可存储图片。
数据表的创建
列的特征包含的内容
- 是否为空(NULL):在输入数据时,数据库的列允许为空时,可以不输入数据,否则必须输入。列是否为空要根据数据库设计的具体要求决定,对于关键列必须禁止为空。
- 是否是标识列(自动编号)
- 是否有默认值:如果数据表的某列在用户不输入数据的时候,希望提供一个默认的内容,比如:用户如果不输入学员地址,则默认:'地址不详'。
- 是否为主键:identity(开始位,增量)主键是实体的唯一标识,保证实体不被重复。一个数据表必须有主键才有意义,否则更新和删除实体都可能会出现异常。
新建查询创建数据表
-- 创建学员信息数据表-- 不能再使用 use master 否则创建的数据表就会出现在 master 数据库中了(会出现有些学生创建完数据表,找不到了的情况)
-- 需要指向 StudentManageDB 数据库,执行完就会发现左上角的下拉框“可用数据库”发生改变了
use StudentManageDB
go-- 判断数据表是否存在
-- 数据表在某个数据库中的信息存在 sysobjects 这个数据表中
-- 选中小括号中的代码执行,可查看 sysobjects 表中的信息,其中 sys 开头的都是系统表
if exists(select * from sysobjects where name='Students')
drop table Students
go-- 创建表
create table Students
(-- 学号-- 假设学号是5位的,我们从10000开始,每次递增1StudentId int identity(10000,1),-- 姓名StudentName varchar(20) not null,-- 性别Gender char(2) not null,-- 出生日期Birthday datetime not null,-- 身份证号StudentIdNo numeric(18,0) not null,-- 年龄-- 后期可通过出生日期动态推算出来,这里只作为练习Age int not null,-- 电话号码PhoneNumber varchar(50),-- 地址StudentAddress varchar(500),-- 所属班级(外键)ClassId int not null
)
go
主键约束与唯一约束
主键(primary key)约束:如 pk_StudentId
唯一(unique)约束:如 uq_StudentIdNo
新建查询
use StudentManageDB
go-- 添加相关约束:
-- 创建主键约束(单独选中执行可查看括号中 select 的结果)
if exists (select * from sysobjects where name='pk_StudentId')
alter table Students drop constraint pk_StudentId
alter table Students add constraint pk_StudentId primary key(StudentId)
go
-- 添加完成后,刷新,打开 Students 表的列,可以看到 StudentId 是主键(钥匙了)
-- 创建唯一约束
if exists (select * from sysobjects where name='uq_StudentIdNo')
alter table Students drop constraint uq_StudentIdNo
alter table Students add constraint uq_StudentIdNo unique(StudentIdNo)
go
-- 添加完成后,也可以通过表右键 -> 设计 -> 右键:索引/键 -> 找到添加约束
检查约束与默认约束
检查(check)约束:如 ck_Age
默认(default key)约束:如 df_StudentAddress
use StudentManageDB
go-- 创建检查约束
if exists (select * from sysobjects where name='ck_Age')
alter table Students drop constraint ck_Age
alter table Students add constraint ck_Age check(Age between 18 and 30)
go
if exists (select * from sysobjects where name='ck_PhoneNumber')
alter table Students drop constraint ck_PhoneNumber
-- 范围约束:len(PhoneNumber) >= 6 and len(PhoneNumber) <= 11、len(PhoneNumber) between 6 and 11
alter table Students add constraint ck_PhoneNumber check(len(PhoneNumber) = 11)
go
-- 添加失败,与现有的数据发生冲突(通常是创建完数据表立马添加约束)
-- 如果有非法数据,要么清空,要么修改成符合要求的数据
-- select * from Students
-- go-- 创建默认约束
if exists (select * from sysobjects where name='df_StudentAddress')
alter table Students drop constraint df_StudentAddress
alter table Students add constraint df_StudentAddress default('地址不详') for StudentAddress
go-- insert into Students (StudentName,Gender,Birthday,Age,StudentIdNo,PhoneNumber,StudentAddress,ClassId) values ('测试','男','1998-05-22',25,941548445145,'19552145822',default,1)
-- 或者
-- insert into Students (StudentName,Gender,Birthday,Age,StudentIdNo,PhoneNumber,ClassId) values ('沈月','女','1998-05-22',25,941548445161,'19552145874',1)
-- go
-- select * from Students
手动操作
模糊查询
Like , between, in
`select StudentName, StudentAddress from Students where StudentAddress like '河南%'``select StudentName, StudentAddress from Students where StudentName like '%浩%'``select StudentName, Age, StudentAddress from Students where Age between 24 and 26``select StudentName, Birthday from Students where Birthday between '1999-01-01' and '2001-05-05'``select StudentName, Age, StudentAddress from Students where Age in (20,26)``select StudentName, Age, StudentAddress from Students where StudentName in ('王可','沈月')`