SQL数据库的基础知识及使用!
- 2019 年 10 月 31 日
- 笔记
作者 | 寂寞黑鲨
来源 | CSDN
1. 约束作用
数据库的约束:避免垃圾数据的产生,禁止非法的数据加入数据库中,保证数据库的结构良好
数据库中的数据在C#中就是一个对象,一条记录存储的是一个对象的属性(例如:姓名,学号,班级等属性),存储到数据库中就是一列列的字段
2. 约束的类型
实体完整性约束:保证存储的记录在数据库中唯一。常见约束类型:
- a.主键约束约束(primary key)
- b.唯一键约束(unique)等
域完整性约束:对字段进行约束。常见约束类型有:
- a.数据类型约束(int或者char(2))等约束)
- b.非空约束(not null)
- c.默认约束(default)
- d.检查约束(check)等
引用完整性约束:保证数据库中的多张数据表数据的一致性和完整性。常见约束类型:外键约束(foreign key)
3. 外键约束的使用
外键约束的使用:当一张表依赖于另外一张表的某个或某些字段时使用,创建外键约束时,先建被引用的表(主键表),再建有外键约束的表(外键表)
删除表中的数据时,如果当前表(主键表)被其他表引用,删除主键表中的数据时有两种方法:第一种:则应该先删除引用的表(外键表)中的数据,再删当前表(主键表)中的数据,例如:A表(主键表)中的a1字段被B表(外键表)中的a1字段引用,这时如果要删除A表中的a1时,要先删除B中的a1再删A中的a1;第二种:通过级联的方式删除,但不提倡使用。
建议:数据表中的主键值不能随便修改。
4. 创建数据库、数据表和表的约束的示例代码
--指向当前要使用的数据库 use master go --判断当前数据库是否存在 if exists (select * from sysdatabases where name='SMDB') drop database SMDB --删除数据库 go --创建数据库 create database SMDB on primary ( --数据库文件的逻辑名 name='SMDB_data', --数据库物理文件名(绝对路径) filename='D:DBSMDB_data.mdf', --数据库文件初始大小 size=10MB, --数据文件增长量 filegrowth=1MB ) --创建日志文件 log on ( name='SMDB_log', filename='D:DBSMDB_log.ldf', size=2MB, filegrowth=1MB ) go --创建学员信息数据表 use SMDB go if exists (select * from sysobjects where name='Students') drop table Students go create table Students ( StudentId int identity(100000,1) , StudentName varchar(20) not null, Gender char(2) not null, Birthday smalldatetime not null, StudentIdNo numeric(18,0) not null,--身份证号 StuImage text null,--学员照片 Age int not null, PhoneNumber varchar(50), StudentAddress varchar(500), ClassId int not null --班级外键 ) go --创建班级表 if exists(select * from sysobjects where name='StudentClass') drop table StudentClass go create table StudentClass ( ClassId int primary key, ClassName varchar(20) not null ) go --创建成绩表 if exists(select * from sysobjects where name='ScoreList') drop table ScoreList go create table ScoreList ( Id int identity(1,1) primary key, StudentId int not null, CSharp int null, SQLServerDB int null, UpdateTime smalldatetime not null ) go --创建管理员用户表 if exists(select * from sysobjects where name='Admins') drop table Admins create table Admins ( LoginId int identity(1000,1) primary key, LoginPwd varchar(20) not null, AdminName varchar(20) not null ) go --创建数据表的各种约束 use SMDB go --创建“主键”约束primary key 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) --创建检查约束check 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 35) --创建唯一约束unique 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) --创建身份证的长度检查约束 if exists(select * from sysobjects where name='ck_StudentIdNo') alter table Students drop constraint ck_StudentIdNo alter table Students add constraint ck_StudentIdNo check (len(StudentIdNo)=18) --创建默认约束 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 if exists(select * from sysobjects where name='df_UpdateTime') alter table ScoreList drop constraint df_UpdateTime alter table ScoreList add constraint df_UpdateTime default (getdate() ) for UpdateTime --创建外键约束 if exists(select * from sysobjects where name='fk_classId') alter table Students drop constraint fk_classId alter table Students add constraint fk_classId foreign key (ClassId) references StudentClass(ClassId) if exists(select * from sysobjects where name='fk_StudentId') alter table ScoreList drop constraint fk_StudentId alter table ScoreList add constraint fk_StudentId foreign key(StudentId) references Students(StudentId)
5. 插入测试数据
一些使用经验:
- 插入数据时,先插主键表再插外键表,否则会出错
- 先把表结构和约束创建完再添加数据,这样可以有效的避免出错
use SMDB go --插入班级数据 insert into StudentClass(ClassId,ClassName) values(1,'网监1区') insert into StudentClass(ClassId,ClassName) values(2,'网监2区') insert into StudentClass(ClassId,ClassName) values(3,'信息安全') insert into StudentClass(ClassId,ClassName) values(4,'计算机科学与技术') --插入学员信息 insert into Students (StudentName,Gender,Birthday,Age,StudentIdNo,PhoneNumber,StudentAddress,ClassId) values('苏家辉','男','1989-08-07',22,120223198908071111,'022-22222222','天津市南开区红磡公寓5-5-102',1), ('小美女','女','1989-05-06',22,120223198905062426,'022-33333333','天津市河北区王串场58号',2), ('小帅哥','男','1990-02-07',21,120223199002078915,'022-44444444','天津市红桥区丁字沽曙光路79号',4) --插入成绩信息 insert into ScoreList (StudentId,CSharp,SQLServerDB)values(100000,60,78),(100001,55,88),(100002,90,58) --插入管理员信息 insert into Admins (LoginPwd,AdminName) values(123456,'杨家贵') insert into Admins (LoginPwd,AdminName) values(123456,'陈皮') insert into Admins (LoginPwd,AdminName) values(123456,'刘懿蝉')
7. 因为添加约束出现的问题解决
若后期,想要在表中添加约束,但加不进去,这是因为一旦创建了约束,数据库系统就要对执行约束,因为已经存在了垃圾数据,执行约束没有通过,所以添加不成功。
解决这种问题的办法:找到垃圾数据然后对垃圾数据进行修改或者删除没用的数据,然后再添加约束
找到垃圾数据的办法:子查询 not in进行查找
8.identity的使用
如果数据库已经创建并且已经做了部署,但是我们希望identity从头开始,可以使用truncate,但是使用前有个前提那就是必须没有引用关系,如果有引用关系,先删除引用关系
9. 数据库查询及对NULL的处理
以后在数据库中添加数据时,尽量不适用null空值,因为在程序中容易出错,可以使用空字符串代替
在数据表中查找null值:使用is null方法
对于null 值,可以把null替换掉,或者把null数据插入一个临时表中,在临时表中做数据检索
10. 简单的几个查询
- select top 3 from 表 ,查询表 中的前三条记录(在程序的分页中使用)
- select top 20 percent from 表 ,查询表中20%的数据并显示
- order by 排序asc升序 desc 降序
- select 名字,班级名称 from 学生表 inner join 班级表 on 班级表.classid=学生表 .classid inner join …on…,需要特别 注意的是:使用连接查询时,相同的字段在两个表中出现,需要在前面加上表明,例如:班级表.classid=学生表 .classid
常用模糊查询:
- like配合%通配符使用,示例:杨%——查询杨开头的所有数据
- between….and…,示例:between 70 and 100——查询70到100的数据
常用函数:
- AVG——求平均值;max——求最大值;min——求最小值;sum——求和;count(*)——求记录数目