头歌 数据库系统实验 答案 善用目录
其他作业链接
非盈利文章,谢谢大家的分享和支持,如果大家有想要投稿的答案,也可以点击下面链接联系作者。
选择题加粗为正确答案
头歌java实训答案集
头歌MySQL数据库实训答案 有目录
SQL Server 实验1.1:创建和使用数据库
第1关 走进SQL Server2012
暂无参考代码
第2关 使用 create database 单条语句创建数据库
create database student
go
第3关 创建含自定义数据文件的数据库
create database student
on
(name=studentdata1,
filename='/home/studentdata1.mdf',
maxsize=10MB
),
(name=studentdata2,
filename='/home/studentdata2.mdf',
maxsize=10MB
)
go
第4关 向数据库中新增数据文件和日志文件
create database student
Go
alter database student
??add file
???(
name=studentdata1,
filename= '/home/studentdata1.ndf',
????size=6MB,
maxsize=20MB,
filegrowth=1MB
??)
Go
alter database student
??add log file
???(
???name=studentlog1,
filename= '/home/studentdata1.ldf',
maxsize=20MB,
filegrowth=1MB
??)
Go
第5关 添加一个数据文件到指定文件组
create database student
go
alter database student
??add filegroup stufilegroup
go
alter database student
add file
???(
???name='studentdata',
filename= '/home/studentdata.ndf'
??)
to FILEGROUP stufilegroup
go
alter database student
MODIFY FILEGROUP stufilegroup DEFAULT
go
第6关 删除数据库
create database student
go
drop database student
Go
SQL Server 实验1.2:创建和使用数据表
第1关 创建一个简单的数据表
create database teach
go
use teach
create table c
(
cno varchar(10),
cname varchar(10),
credit float default 2.0
)
go
use teach
exec sp_columns c
go
第2关 向数据表中新增列
drop database teach
create database teach
go
use teach
create table s
(
sno varchar(50),
sname varchar(50),
age int,
dept varchar(50)
)
go
use teach
alter table s
add birthday date
go
use teach
exec sp_columns s
Go
第3关 修改数据表中的列定义
drop database teach
create database teach
create table s
(
sno varchar(10),
sname varchar(10),
age int,
dept varchar(10)
)
go
use teach
create table s
(
sno varchar(10),
sname varchar(10),
age int,
dept varchar(10)
)
go
use teach
alter table s
alter column sname varchar(50)
alter table s
alter column age char(10)
go
use teach
exec sp_columns s
Go
第4关 删除数据表中的列
drop database teach
create database teach
go
use teach
create table s
(
sno varchar(10),
sname varchar(10),
age int,
dept varchar(10)
)
go
use teach
alter table s
drop column age
go
use teach
exec sp_columns s
go
SQL Server 实验1.3:创建和使用数据表中约束
第1关 创建带主键和唯一约束的数据表
create database teach
go
use teach
create table s
(
sno char(6) CONSTRAINT sno_pk PRIMARY KEY,
sname varchar(8) CONSTRAINT sn_uq UNIQUE,
age int ,
dept varchar(20)
)
create table sc
(
sno char(6) NOT NULL,
cno varchar(10) NOT NULL,
grade int ,
CONSTRAINT sno_cno_pk PRIMARY KEY(sno,cno)
)
go
use teach
select b.column_name 主键名
from information_schema.table_constraints a
inner join information_schema.constraint_column_usage b
on a.constraint_name = b.constraint_name
where a.constraint_type = 'PRIMARY KEY' and a.table_name = 's'
go
select b.column_name 主键名
from information_schema.table_constraints a
inner join information_schema.constraint_column_usage b
on a.constraint_name = b.constraint_name
where a.constraint_type = 'PRIMARY KEY' and a.table_name = 'sc'
go
SELECT
idx.name AS 唯一约束名,
col.name AS 列名
FROM
sys.indexes idx
JOIN sys.index_columns idxCol
ON (idx.object_id = idxCol.object_id
AND idx.index_id = idxCol.index_id
AND idx.is_unique_constraint = 1)
JOIN sys.tables tab
ON (idx.object_id = tab.object_id)
JOIN sys.columns col
ON (idx.object_id = col.object_id
AND idxCol.column_id = col.column_id)
where object_name(idx.object_id)='s'
go
第2关 创建带外键、检查约束的数据表
create database teach
go
use teach
CREATE TABLE s
(
sno char(6) PRIMARY KEY,
sname varchar(8) UNIQUE,
age int NOT NULL,
dept varchar(20) NOT NULL
)
create table sc
(
sno char(6) foreign key references s(sno),
cno char(10) NOT NULL,
grade int check (grade>=0 and grade<=100) NOT NULL,
PRIMARY KEY(sno,cno)
)
go
use teach
select
d.name as 外键列,
object_name(b.parent_object_id) as 外键表,
c.name as 主键列,
object_name(b.referenced_object_id) as 主健表
from sys.foreign_keys A
inner join sys.foreign_key_columns B on A.object_id=b.constraint_object_id
inner join sys.columns C on B.parent_object_id=C.object_id and B.parent_column_id=C.column_id
inner join sys.columns D on B.referenced_object_id=d.object_id and B.referenced_column_id=D.column_id
where object_name(B.referenced_object_id)='s'
SELECT
col.name AS check列名,
chk.definition
FROM
sys.check_constraints chk
JOIN sys.tables tab
ON (chk.parent_object_id = tab.object_id)
JOIN sys.columns col
ON (chk.parent_object_id = col.object_id
AND chk.parent_column_id = col.column_id)
where object_name(chk.parent_object_id)='sc'
Go
第3关 向数据表中新增完整性约束
create database teach
go
use teach
CREATE TABLE s
(
sno varchar(50) PRIMARY KEY,
sname varchar(50) ,
age int ,
dept varchar(50) ,
)
go
use teach
ALTER TABLE s
ADD check (age between 19 and 23)
go
use teach
exec sp_columns s
SELECT
col.name AS check列名,
chk.definition
FROM
sys.check_constraints chk
JOIN sys.tables tab
ON (chk.parent_object_id = tab.object_id)
JOIN sys.columns col
ON (chk.parent_object_id = col.object_id
AND chk.parent_column_id = col.column_id)
where object_name(chk.parent_object_id)='s'
Go
第4关 删除数据表中的完整性约束
DROP database teach
create database teach
go
use teach
CREATE TABLE s
(
sno varchar(10) CONSTRAINT sno_pk PRIMARY KEY,
sname varchar(10) ,
age int ,
dept varchar(10)
)
go
use teach
ALTER TABLE s
DROP CONSTRAINT sno_pk
go
use teach
exec sp_columns s
select b.column_name 主键名
from information_schema.table_constraints a
inner join information_schema.constraint_column_usage b
on a.constraint_name = b.constraint_name
where a.constraint_type = 'PRIMARY KEY' and a.table_name = 's'
go
第5关 删除数据表
DROP database teach
create database teach
go
use teach
create table s
(
sno char(6) primary key,
sname varchar(8),
age int,
dept varchar(20)
)
create table sc
(
sno char(6) foreign key references s(sno),
cno varchar(10),
grade int,
primary key(sno,cno)
)
DROP TABLE sc
DROP TABLE s
go
use teach
SELECT * FROM INFORMATION_SCHEMA.TABLES
go
SQL Server 实验2.1 单表查询
第1关 查询单表中若干列
create database teach
go
use teach
create table s
(
sno char(10) primary key,
name varchar(20) unique,
sex char(2) default 'm',
age int check(age between 19 and 23),
dept varchar(20) not null
)
insert into s
values('001','tom','m',20,'cs'),('002','jerry','m',19,'cs'),('003','amy','f',22,'is')
go
use teach
select name,dept
from s
select *
from s
select name as 姓名
from s
select distinct dept
from s
go
第2关 查询单表中若干行
drop database teach
create database teach
go
use teach
create table c
(
cno char(10) primary key,
cname varchar(50) not null,
credit float default 2.0
)
insert into c
values('c01','database system',3.5),('c02','software system design',4),('c03','math',3)
go
use teach
select cname
from c
where credit >3
select cno,credit
from c
where credit not between 2.0 and 3.0
select *
from c
where cno in ('c01','c02')
select cname
from c
where cname like '%system%'
go
第3关 聚集函数
drop database teach
create database teach
go
use teach
create table sc
(
sno char(10),
cno char(10),
grade int check(grade between 0 and 100),
primary key(sno,cno)
)
insert into sc
values('001','c01',90),('001','c02',87),('002','c01',85),('002','c02',92),('003','c01',89)
go
use teach
select max(grade) ,min(grade)
from sc
select sum(grade) as 总分
from sc
where sno = '001'
select count(distinct sno) as 人数
from sc
go
第4关 分组和排序
drop database teach
create database teach
go
use teach
create table sc
(
sno char(10),
cno char(10),
grade int check(grade between 0 and 100),
primary key(sno,cno)
)
insert into sc
values('001','c01',90),('001','c02',87),('002','c01',85),('002','c02',80),('003','c01',89)
go
use teach
select cno,avg(grade) as 平均分
from sc
group by cno
select cno,avg(grade) as 平均分
from sc
group by cno
having avg(grade) > 85
order by avg(grade) ASC
go
SQL Server 实验2.2 连接查询
第1关 等值连接
drop database teach
create database teach
go
use teach
create table s
(
sno char(10) primary key,
name varchar(20) unique,
sex char(2) default 'm',
age int check(age between 19 and 23),
dept varchar(20) not null
)
create table sc
(
sno char(10) foreign key references s(sno),
cno char(10),
grade int check(grade between 0 and 100),
primary key(sno,cno)
)
insert into s
values('001','tom','m',20,'cs'),('002','jerry','m',19,'cs'),('003','amy','f',22,'is')
insert into sc
values('001','c01',90),('001','c02',87),('002','c01',85),('002','c02',92)
go
use teach
select s.*,cno,grade
from s,sc
where s.sno=sc.sno
select s.*,cno,grade
from s left join sc
on s.sno=sc.sno
select name,cno,grade
from s,sc
where s.sno=sc.sno and dept = 'cs'
go
第2关 多表连接
drop database teach
create database teach
go
use teach
create table s
(
sno char(10) primary key,
name varchar(20) unique,
sex char(2) default 'm',
age int check(age between 19 and 23),
dept varchar(20) not null
)
create table c
(
cno char(10) primary key,
cname varchar(50) not null,
credit float default 2.0
)
create table sc
(
sno char(10) foreign key references s(sno),
cno char(10) foreign key references c(cno),
grade int check(grade between 0 and 100),
primary key(sno,cno)
)
insert into s
values('001','tom','m',20,'cs'),('002','jerry','m',19,'cs')
insert into c
values('c01','database system',3.5),('c02','software system design',4)
insert into sc
values('001','c01',90),('001','c02',87),('002','c01',85),('002','c02',92)
go
use teach
SELECT s.sno, name,cname, credit, grade
FROM s, c,sc
WHERE s.sno = sc.sno
AND sc.cno = c.cno
SELECT name,cname, grade
FROM s, c,sc
WHERE (s.sno = sc.sno
AND sc.cno = c.cno)
and s.sno = '001'
go
第3关 自身连接
drop database teach
create database teach
go
use teach
create table s
(
sno char(10) primary key,
name varchar(20) unique,
sex char(2) default 'm',
age int check(age between 19 and 23),
dept varchar(20) not null
)
insert into s
values('001','tom','m',20,'cs'),('002','jerry','m',19,'cs'),('003','amy','f',22,'is')
go
use teach
SELECT X.name, X.age
FROM s AS X ,s AS Y
WHERE X.age> Y.age AND Y.name='jerry'
go
SQL Server 实验2.3 子查询
第1关 返回单值的子查询
create database teach
go
use teach
create table s
(
sno char(10) primary key,
name varchar(20) unique,
sex char(2) default 'm',
age int check(age between 19 and 23),
dept varchar(20) not null
)
create table sc
(
sno char(10) foreign key references s(sno),
cno char(10),
grade int check(grade between 0 and 100),
primary key(sno,cno)
)
insert into s
values('001','tom','m',20,'cs'),('002','jerry','m',19,'cs'),('003','amy','f',22,'is')
insert into sc
values('001','c01',90),('001','c02',87),('002','c01',85),('002','c02',92)
go
use teach
select cno,grade
from sc
where sno =
(
select sno
from s
where name="tom"
)
select sno,name,age
from s
where age >
(
select age
from s
where name = 'jerry'
)
go
第2关 返回一组值的子查询
drop database teach
create database teach
go
use teach
create table c
(
cno char(10) primary key,
cname varchar(50) not null,
credit float default 2.0
)
create table sc
(
sno char(10) ,
cno char(10) foreign key references c(cno),
grade int check(grade between 0 and 100),
primary key(sno,cno)
)
insert into c
values('c01','database system',3.5),('c02','software system design',4)
insert into sc
values('001','c01',90),('001','c02',87),('002','c01',88),('002','c02',92)
go
use teach
select cname,credit
from c
where cno in
(
select cno
from sc
where sno = '001'
)
select cno,grade
from sc
where cno != 'c01' and grade < all
(
select grade
from sc
where cno ='c01'
)
go
SQL Server 实验3 数据操纵与视图、索引
第1关 数据操纵
create database teach
go
use teach
create table s
(
sno char(10) primary key,
name varchar(20) unique,
sex char(2) default 'm',
age int check(age between 19 and 23),
dept varchar(20) not null
)
create table sc
(
sno char(10) foreign key references s(sno),
cno char(10),
grade int check(grade between 0 and 100),
primary key(sno,cno)
)
insert into s
values('001','tom','m',20,'cs'),('002','jerry','m',19,'cs'),('003','amy','f',22,'is')
insert into sc
values('001','c01',90),('001','c02',87),('002','c01',85),('002','c02',92)
go
use teach
create table sc_avg
(
sno char(10),
avg_grade int /*平均分*/
)
insert
into sc_avg(sno,avg_grade)
select sno,avg(grade)
from sc
group by sno
select * from sc_avg
update sc
set grade=grade+5
where grade>=90
select * from sc
delete
from sc
select * from sc
go
第2关 创建和使用视图
drop database teach
create database teach
go
use teach
create table s
(
sno char(10) primary key,
name varchar(20) unique,
sex char(2) default 'm',
age int check(age between 19 and 23),
dept varchar(20) not null
)
insert into s
values('001','tom','m',20,'cs'),('002','jerry','m',19,'cs'),('003','amy','f',22,'is')
go
create view cs_m_s
as
select sno,name,age
from s
where dept='cs' and sex='m'
go
select * from cs_m_s
go
select *
from cs_m_s
where age>=20
go
delete
from cs_m_s
go
select * from cs_m_s
go
第3关 创建和删除索引
drop database teach
create database teach
go
use teach
create table sc
(
sno char(10),
cno char(10),
grade int check(grade between 0 and 100)
)
go
create index sno_cno_index
on sc
(
sno asc,
cno asc
)
go
exec sp_helpindex sc
go
drop index sno_cno_index on sc
go
exec sp_helpindex sc
go
SQL Server 实验4 存储过程和触发器
第1关 创建和使用存储过程
drop database teach
create database teach
go
use teach
create table s
(
sno char(10) primary key,
name char(20) unique,
age int check(age between 19 and 23),
)
insert into s
values('001','tom',20),('002','jerry',19),('003','amy',22)
use teach
go
CREATE PROCEDURE insertrecord
(
@sno char(10),
@name char(20),
@age int
)
AS
INSERT INTO s
VALUES(@sno,@name,@age)
go
EXECUTE insertrecord
@sno='004',@name='tony',@age=19
go
use teach
select * from s
第2关 创建和使用 DML 触发器
drop database teach
create database teach
go
use teach
create table s
(
sno char(10) primary key,
name varchar(20) unique,
sex char(2) default 'm',
age int check(age between 19 and 23),
dept varchar(20) not null
)
create table sc
(
sno char(10),
cno char(10),
grade int check(grade between 0 and 100),
primary key(sno,cno)
)
insert into s
values('001','tom','m',20,'cs'),('002','jerry','m',19,'cs'),('003','amy','f',22,'is')
insert into sc
values('001','c01',90),('001','c02',87),('002','c01',85),('002','c02',92)
go
create trigger delete_s on s
after delete
as
delete from sc
where sno in (select sno from deleted)
go
use teach
delete from s where sno ='002'
go
select * from s
select * from sc
|