1.创建带参数的数据库
--我们写的查询语句,一般称为SQL脚本
--带参数的查询语句
create database School
on primary
(
name='SchoolMain',--主数据文件逻辑名称
size=10mb,--初始大小
filegrowth=10%,--文件增长比例
fileName='D:\SchoolPh.mdf',
maxsize=100mb
)
log on
(
name='SchoolLog',--日志文件逻辑名称
size=10mb,--初始大小
filegrowth=10%,--文件增长比例
fileName='D:\SchoolPh_Log.ldf',
maxsize=100mb
)
2.SQL是一种标准,市场上有很多DBMS,所有的关系型数据库都支持标准SQL,但各个厂商也制定了自己独特语句,比如微软的MS SQL
Server,他里面有一种叫做T-SQL的语句,就是对标准SQL做出的扩展
3.SQL语句分类:
①DDL:数据定义语句(创建数据表,创建表)
②DML:数据操作语句(对数据的增删改查)
4.插入数据,高级部分
一条insert语句录入多条记录
--语法:insert into [表名](字段列表)
-- select [值列表] union,
-- select [值列表] union,
-- ...
-- select [值列表] union,
select * from TbClass
insert into TbClass(clsName,clsTeacher,clsNumber)
select '15级IOS班','东哥',40 union
select '15级IOS班','东哥',40 union
select '15级Android班','吕布',40 union all
select '15级Android班','吕布',40 union all
select '15级云计算班','典韦',40 union
select '16级大数据班','马超',40
Union 关键字,本身就有剔除重复记录的意思,所以,录入多条相同记录
会被Union给删除掉,如果,就是要插入重复的记录,需要在union后边添加all关键字使用union all
5.备份:把一个数据表中的记录,备份到另外一个目前不存在的表中
–语法:select [字段列表] into [新表名] from[旧表名]
--把TbClass中的记录,备份到TbClassBak
select stuName,stuNumber into TbClassBak from TbClass
–此用法不能往已经存在的表中备份数据,系统会自动的给用户创建一个新的表
–虽然可以在select语句中,用代替所有的字段列表,但是我们不建议这么做,
–尤其是在做项目的时候,千万不能用代替字段列表
6.go语句
1.go不是SQL语句,他只是MS SQL Server的一个命令
2.go可以把一个SQL脚本文件的众多SQL语句,分为多个批次发送到数据库服务引擎去执行,这样可以让不能同时选中执行的SQL语句能够一次全选中,执行提交
7.插入数据
--insert into [表名]([字段列表]) values(值列表)
insert into TbClass (clsName,clsTeacher,clsNumber)
values('14级数据库班','李俊峰',34)
insert into TbClass (clsTeacher,clsName,clsNumber)
values('东哥','13嵌入式班',65)
--省略某些字段的insert语句,not null的字段不能省略
insert into TbClass(clsName,clsNumber) values('14级.net班',32)
--如果就是想给所有的字段都有值,省略全部字段列表
insert into TbClass values('13.net班','吴亮',48)
8.查询
--基本语法:select [字段列表] from [表名]:查询出该表的所有记录
select clsId,clsName,clsTeacher,clsNumber from TbClass
--只查询若干字段
select clsName,clsTeacher from TbClass
--如果就是想查看所有字段的,可以使用‘*’代替字段列表
select * from TbClass
注:[]的作用:.避免字段名与关键字的冲突,但是绝对不要使用关键字做表名或者字段名
9.修改表结构和追加约束
--1.删除一列
alter table StudentInfo drop column stuPhone
--2.添加一列
alter table StudentInfo add stuPhone char(11)
--3.修改字段的数据类型
alter table StudentInfo alter column stuGender nchar(1)
--4.添加主键约束
alter table StudentInfo add constraint PK_StudentInfo_stuID primary key(stuID)
--5.添加唯一性约束
alter table StudentInfo add constraint UK_StudentInfo_stuName unique(stuName)
--6.添加check约束
alter table StudentInfo add constraint CK_StudentInfo_stuAge
check(stuAge>=18 and stuAge<=35)
--7.添加非空约束,实际上就是对列的数据类型的修改
alter table StudentInfo alter column stuPhone char(11) not null
--8.添加外键约束
alter table StudentInfo add constraint FK_Student_stuClassId
foreign key(stuClassId) references StudentInfo(clsId)
--9.外键的级联删除/更新
--语法:on delete [no action|cascade]
alter table StudentInfo add constraint FK_StudentInfo_stuClassId
foreign key(stuClassId) references StudentInfo(clsId) on delete cascade
--10.删除约束
alter table Studentinfo drop constraint FK_Student_stuClassId
--11.删除多条约束
alter table Studentinfo drop constraint FK_Student_stuClassId,UK_StudentInfo_stuName unique(stuName)