目录
前言:
让我们荡起双桨,使用SQL语句来实现建库,建表,各种增删改查操作吧!很详细的解析,一步步操作。工具:SQL Server 2019中SQL Server Management Studio(简称SSMS)。
一、创建数据库
新建查询,在里面输入建立数据库的SQL语句,这里创建的数据库的名字为EDUC
create database EDUC
on
(name=EDUC_data,--数据文件的逻辑名称,注意不能与日志逻辑同名
filename='D:\SQL_database\EDUC_data1.mdf',--物理名称,注意路径必须存在。
--这里D:\Database是我D盘上的文件夹,一定要保证路径是存在的,不然创建失败
size=5,--数据初始长度为5M
maxsize=10,--最大长度为10M
filegrowth=1)--数据文件每次增长1M
log on
(name=EDUC_log,
filename='D:\SQL_database\EDUC_data1.ldf',--物理名称,注意路径必须存在。
size=2,
maxsize=5,
filegrowth=1)
如果你路径出现错误,就有可能会这样报错:
刷新一下,我们就可以看到:
二、创建几张表
根据上面的表格,我们可以这里展示一个例子:
比如创建student表
use EDUC
go
create table student
(
sno char(8) primary key,--学号是主键,它默认是not null的
sname char(8) not null,--姓名
sex char(2),--性别
native char(20),--籍贯
birthday smalldatetime,--出生日期
dno char(6),--所在院系
spno char(8),--专业班级
classno char(4),--班级号
entime smalldatetime,--入校时间
home varchar(40),--家庭住址
tel varchar(40)--联系电话
)
后面的一起有:
use EDUC
go
create table student--学生表
(
sno char(8) primary key,--学号是主键,它默认是not null的
sname char(8) not null,--姓名
sex char(2),--性别
native char(20),--籍贯
birthday smalldatetime,--出生日期
dno char(6),--所在院系
spno char(8),--专业班级
classno char(4),--班级号
entime smalldatetime,--入校时间
home varchar(40),--家庭住址
tel varchar(40)--联系电话
)
create table course--课程信息表
(cno char(10) primary key,--课程编号
spno char(8),--专业代码(外键)
cname char(20) not null,--课程名称
ctno tinyint,--课程类型编号(外键)
experiment tinyint,--实验时数
lecture tinyint,--授课学时
semester tinyint,--开课学期
credit tinyint)--课程学分
create table student_course--学生选课成绩表
(sno char(8) not null,--学生学号
tcid smallint not null,--上课编号
score tinyint--学生成绩
)
create table teacher--教师信息表
(tno char(8) primary key,--教师编号
tname char(8) not null,--教师姓名
sex char(2),--教师性别
birthday smalldatetime,--教师出生日期
dno char(6),--教师所在院系
pno tinyint,--教师职务或职称编号
home varchar(4), --教师家庭住宅
zipcode char(6),--邮政编码
tel varchar(40),--联系电话
email varchar(40)--电子邮件
)
create table teacher_course--教师上课课表
(tcid smallint primary key,--上课编号
tno char(8),--教师编号(外键)
spno char(8),--专业代码(外键)
classno char(4),--班级号
cno char(10),--课程编号(外键)
semester char(6),--学期
schoolyear char(10),--学年
classtime varchar(40),--上课时间
classroom varchar(4),--上课地点
weektime tinyint --每周课时数
)
当表太多的时候,其实你可以用资源管理器来创建,这样不需要写那么多代码,但是多敲一敲还是有好处的。
完成如下:
三、修改数据库基本表的定义
那假如你现在需要修改表中定义的类型、属性等怎么办呢,当然我们这里只介绍SQL语句
1、修改列属性
(1)将student表中的birthday设置为not null。
use EDUC
go
alter table student
alter column birthday smalldatetime not null
我们刷新之后观察一下列属性如下,完成了修改:
(2)用 SQL 语句将 Student 表中的属性 sno char(8)改成 varchar(20)类型。
use EDUC
go
alter table student
alter column sno varchar(20)
当我们这样输入的时候报错了
所以你知道,当我们要修改有约束的列属性时,要先删除约束,所以我们有:
<--先删除
alter table 表名 drop CONSTRAINT 主键约束名
--后新增
alter table 表名 add CONSTRAINT 主键约束名 primary key (字段1,字段2...)
use EDUC
go
alter table student
drop constraint PK__student__DDDF644698DA0F41
alter table student
alter column sno varchar(20)
alter table student
add constraint PK__student__DDDF644698DA0F41 primary key (sno)
但是出错了对吧
所以属性那里我们设置为not null才行哦
例如这样的:
use EDUC
go
alter table student
drop constraint PK__student__DDDF644698DA0F41
alter table student
alter column sno varchar(20) not null
alter table student
add constraint PK__student__DDDF644698DA0F41 primary key (sno)
(3)修改属性名称
将student表中的tel变为te
use EDUC
go
execute sp_rename 'student.tel','te';
完成之后它会提示你可能会破坏脚本和存储过程
我们查看一下,的确修改了
2、添加列
use EDUC
go
alter table student
add year varchar(4)
然后:
use EDUC
go
alter table student
add check(year between 2004 and 2008)
3、删除列
(1)删除之前添加的year列
如果我们不先删除依赖约束,那么肯定又会这样报错
所以我们要这样做:
use EDUC
go
alter table student
drop constraint CK__student__year__2C3393D0
alter table student
drop column year
那么观察一下,果然删除了:
四、创建和删除索引
1、分别建立以下索引
use EDUC
go
create index stusname
on student(sname desc);
在索引那里查看就有:
use EDUC
go
create unique index coucname
on course(cname);
(3) 在 student_course 表的 sno 列上建立聚集索引。
use EDUC
go
create clustered index stucourse
on student_course(sno)
(4) 在 student_course 表的 sno(升序), tid(升序)和 score(降序)三列上建立一个普通索引。
use EDUC
go
create index scno
on student_course(sno,tcid,score desc)
总结:
create [unique] [clustered] [nonclustered] index index_name
on {tabel/view} (column[dese/asc][....n])
2、 删除索引
use EDUC
go
drop index stusname on student;
后记:
之后会讲解插入、查询、视图、更新、流控制语句、游标的使用和数据库约束,如有误请指出,谢谢。