2数据库的创建与管理,数据操纵

学生表、教师表以及上课表的建立
一、数据库建立
1.创建teaching数据库
2.利用SQL Server Management Studio创建表student,表结构为
2数据库的创建与管理,数据操纵_第1张图片

create table student(studentno nchar(11) not null primary key,sname nchar(8) null,sex nchar(2) null default '男' check(sex='男' or sex='女'),birthday datetime null,classno nchar(6) null,point smallint null,phone nchar(12) null,email nvarchar(20) null)

3.利用Transact-SQL语句在数据库teaching中创建表如下:
(1)课程表

create table course(courseno nchar(6) not null ,cname nchar(20) null,type nchar(8) null,period tinyint null,credit numeric(4,1) null,constraint pk_course primary key(courseno))

(2)分数表

create table score(studentno nchar(11) not null ,courseno nchar(6) not null,usually numeric(6,2) null,final numeric(6,2) null,constraint pk_score primary key(studentno,courseno))

(3)教师表

create table teacher(teachno nchar(6) not null primary key,tname nchar(8) null ,major nchar(10) null, prof nchar(10) null,department nchar(12) null)

(4)班级表

create table class(classno nchar(7) not null primary key,classname nchar(12) null,department nchar(12) null,monitor nchar(8) null)

(5)教课表

create table teach_class(teacherno nchar(6) not null ,classno nchar(7) not null,courseno nchar(6) not null,constraint pk_teach_class primary key(teacherno,classno,courseno))

4.为表输入数据。
执行命令:

use teaching  
 insert into student(studentno,sname,sex,birthday,classno,point,phone,email)values('16122210009','许海兵','男','1996/11/05','160501','789','13623456778','[email protected]...')
 insert into student(studentno,sname,sex,birthday,classno,point,phone,email)values('16122221324','何影',  '女','1997/12/04','160501','879','13178978999','[email protected]...')
 insert into student(studentno,sname,sex,birthday,classno,point,phone,email)values('16125111109','敬秉承','男','1998/03/01','160801','789','15678945623','[email protected]...')
 insert into student(studentno,sname,sex,birthday,classno,point,phone,email)values('16125121107','梁欣',  '女','1996/09/03','160502','777','13145678921','[email protected]...')
 insert into student(studentno,sname,sex,birthday,classno,point,phone,email)values('16135222201','夏文斐','女','1997/10/06','160502','867','15978945645','[email protected]...')
 insert into student(studentno,sname,sex,birthday,classno,point,phone,email)values('16137221508','赵忘舒','女','1997/02/13','160802','789','12367823453','ping163.com...')
 insert into student(studentno,sname,sex,birthday,classno,point,phone,email)values('17111133071','崔岩坚','男','1998/06/06','170601','787','15556845645','[email protected]...')
 insert into student(studentno,sname,sex,birthday,classno,point,phone,email)values('17112100072','宿致远','男','1999/02/04','170501','658','12545678998','[email protected]...')
 insert into student(studentno,sname,sex,birthday,classno,point,phone,email)values('17112111208','韩吟秋','男','1997/02/14','170601','666','15878945612','[email protected]...')
 insert into student(studentno,sname,sex,birthday,classno,point,phone,email)values('17122203567','封澈',  '女','1999/09/09','170601','898','13245674564','[email protected]...')
 insert into student(studentno,sname,sex,birthday,classno,point,phone,email)values('17123567897','赵毓欣','女','1999/08/04','170501','999','13175689345','[email protected]...')
 insert into student(studentno,sname,sex,birthday,classno,point,phone,email)values('17126113307','竹云泽','女','2000/09/07','170601','787','13245678543','[email protected]...')
 insert into student(studentno,sname,sex,birthday,classno,point,phone,email)values('17127893457','风格'  ,'男','2000/09/09','170601','789','15134576788','[email protected]...')
 insert into student(studentno,sname,sex,birthday,classno,point,phone,email)values('1828261001', '孙释远','女','1999/09/09','090502','777','1328909876', '[email protected]...')
select * from student 
insert into course(courseno,cname,type,period,credit)values('c05103','高等数学','必修','64','4.0')
insert into course(courseno,cname,type,period,credit)values('c05109','C语言','必修','48','3.0')
insert into course(courseno,cname,type,period,credit)values('c05127','数据结构','必修','64','4.0')
insert into course(courseno,cname,type,period,credit)values('c05129','数据库原理与应用','必修','64','4.0')
insert into course(courseno,cname,type,period,credit)values('c05138','软件工程','选修','48','3.0')
insert into course(courseno,cname,type,period,credit)values('c05139','统一建模语言U','选修','48','3.0')
insert into course(courseno,cname,type,period,credit)values('c05141','WIN程序设计','选修','64','4.0')
insert into course(courseno,cname,type,period,credit)values('c05142','WEB程序设计','选修','64','4.0')
insert into course(courseno,cname,type,period,credit)values('c05151','管理信息系统','选修','48','3.0')
insert into course(courseno,cname,type,period,credit)values('c05152','电子商务','选修','48','3.0')
insert into course(courseno,cname,type,period,credit)values('c06108','机械制图','必修','60','3.5')
insert into course(courseno,cname,type,period,credit)values('c06127','机械设计','必修','64','4.0')
insert into course(courseno,cname,type,period,credit)values('c06172','铸造工艺','选修','42','2.5')
insert into course(courseno,cname,type,period,credit)values('c08106','经济法','必修','48','3.0')
insert into course(courseno,cname,type,period,credit)values('c08123','金融学','必修','40','2.5')
insert into course(courseno,cname,type,period,credit)values('c08171','会计软件','选修','32','2.0')
insert into course(courseno,cname,type,period,credit)values('c11222','数据库原理','必修','48','3.0')
insert into course(courseno,cname,type,period,credit)values('c22222','测试课程','必修','48','3.0')

执行命令:

insert into score(studentno,courseno,usually,final)values('16122210009','c05103','87.00','82.00')
insert into score(studentno,courseno,usually,final)values('16122210009','c05109','77.00','91.00')
insert into score(studentno,courseno,usually,final)values('16122221324','c05103','88.00','62.00')
insert into score(studentno,courseno,usually,final)values('16125111109','c05109','91.00','77.00')
insert into score(studentno,courseno,usually,final)values('16125111109','c08106','79.00','99.00')
insert into score(studentno,courseno,usually,final)values('16125111109','c08123','85.00','92.00')
insert into score(studentno,courseno,usually,final)values('16125121107','c08171','77.00','92.00')
insert into score(studentno,courseno,usually,final)values('16125121107','c05103','74.00','91.00')
insert into score(studentno,courseno,usually,final)values('16135222201','c05109','89.00','60.00')
insert into score(studentno,courseno,usually,final)values('16135222201','c08109','99.00','92.00')
insert into score(studentno,courseno,usually,final)values('16122210009','c08171','95.00','82.00')
insert into score(studentno,courseno,usually,final)values('16137221508','c05109','79.00','91.00')
insert into score(studentno,courseno,usually,final)values('16137221508','c08106','78.00','95.00')
insert into score(studentno,courseno,usually,final)values('16137221508','c08123','78.00','89.00')
insert into score(studentno,courseno,usually,final)values('16137221508','c08171','88.00','98.00')
insert into score(studentno,courseno,usually,final)values('17111133071','c05103','82.00','69.00')
insert into score(studentno,courseno,usually,final)values('17111133071','c05109','77.00','82.00')
insert into score(studentno,courseno,usually,final)values('17112100072','c05109','87.00','86.00')
select * from score

执行命令:

insert into teacher(teachno,tname,major,prof,department)values('t05001','韩晋升','软件工程','教授','计算机学院')
insert into teacher(teachno,tname,major,prof,department)values('t05002','张衣新','金融','助教','农机学院')
insert into teacher(teachno,tname,major,prof,department)values('t05003','刘元朝','网络技术','教授','计算机学院')
insert into teacher(teachno,tname,major,prof,department)values('t05011','海封','计算机技术','副教授','计算机学院')
insert into teacher(teachno,tname,major,prof,department)values('t05017','卢明欣','软件测试','讲师','计算机学院')
insert into teacher(teachno,tname,major,prof,department)values('t06011','胡海悦','机械制造','教授','机械学院')
insert into teacher(teachno,tname,major,prof,department)values('t06023','姚思远','铸造工艺','副教授','机械机学院')
insert into teacher(teachno,tname,major,prof,department)values('t07017','刘丽萍','物联网','教授','计算机学院')
insert into teacher(teachno,tname,major,prof,department)values('t07019','马爱芬','经济管理','讲师','管理学院')
insert into teacher(teachno,tname,major,prof,department)values('t08017','田有余','金融管理','副教授','管理机学院')
select * from teacher

执行命令:

insert into class(classno,classname,department,monitor)values('160501','计算机','计算机学院','马文斐')
insert into class(classno,classname,department,monitor)values('160502','计算机','计算机学院','章成楠')
insert into class(classno,classname,department,monitor)values('160801','管理','管理学院','党海平')
insert into class(classno,classname,department,monitor)values('160802','管理','管理学院','李红章')
insert into class(classno,classname,department,monitor)values('170501','计算机','计算机学院','刘国平')
insert into class(classno,classname,department,monitor)values('170601','机械','机械学院','王善执')
select * from class

执行命令:

insert into teach_class(teacherno,classno,courseno)values('t05001','170501','c05109')
insert into teach_class(teacherno,classno,courseno)values('t05002','170501','c05127')
insert into teach_class(teacherno,classno,courseno)values('t05003','160501','c05127')
insert into teach_class(teacherno,classno,courseno)values('t05011','160502','c05138')
insert into teach_class(teacherno,classno,courseno)values('t05017','160501','c05127')
insert into teach_class(teacherno,classno,courseno)values('t06011','170601','c06127')
insert into teach_class(teacherno,classno,courseno)values('t06023','170601','c06172')
insert into teach_class(teacherno,classno,courseno)values('t07019','160801','c08123')
insert into teach_class(teacherno,classno,courseno)values('t08017','160801','c08106')
select * from teach_class

5.利用Transact-SQL语句修改表结构。
(1)位student表增加民族nation列,数据类型为nchar(10),允许为空。

alter table student add nation nchar(10) null

(2)修改student表中nation列的数据类型为varchar(10)。

alter table student alter column nation varchar(10)

(3)删除student表中个nation列。

alter table student drop column nation

6.利用Transact-SQL语句修改表中的数据。
(1)向student表中插入一条学生记录(‘16122221326’,‘李杰’,‘男’,‘1999-2-3’,‘160501’,754,‘13456734522’,‘[email protected]’)
执行命令:

insert into student(studentno,sname,sex,birthday,classno,point,phone,email)values('16122221326','李杰','男','1999/02/03','160501','754','13456734522','[email protected]')

(2)向score表中插入一条选课记录(‘16122221326’,‘c05109’)

insert into score(studentno,courseno)values('16122221326','c05109')

(3)将student表中插入的学号为‘16122221326’的学生性别改为‘女’。

update student set sex='女' where studentno='16122221326'

(4)删除学号为‘16122221326’的学生的基本信息和选课信息。

delete from student where studentno='16122221326'

二、

1 向课程表中添加一个列:课程编码 nchar(4) , 要求课程编码的第一个字母必须是C,第2-4个字符必须是数字1-9中一个

alter table 课程表 add 课程编码 nchar(4) check(课程编码 like 'C%[1 to 9][1 to 9][1 to 9]')

2 从学生表中查询出:学号,姓名,年龄

select 学号,姓名,出生日期,year(getdate())-YEAR(出生日期) as 年龄 from 学生表

3 查询出生日为今天的学生信息

select CONVERT(int,getdate()-'1988-12-1')

4 从成绩表中查出有哪些课程被选

select 课程名称 from 课程表 where 课程号 in(select 课程号 from 成绩表)

5 从成绩表中查询已选课程有几门

select COUNT(distinct 课程号) from 成绩表

6 从学生表中,查出1985—1991年之间出生的学生

select * from 学生表 where 出生日期 between 1985/01/01 and 1991/12/31

7 使用between and方法,查出成绩表中成绩在80-95之间的情况

select * from 成绩表 where 成绩 between 80 and 95

9 从成绩表中查出课程101,104课程的情况

select * from 成绩表 where 成绩 between 80 and 95

10 查出家庭住址中含有“村”字的学生情况

select * from 学生表 where 地址 like '%村%'

11 查出所有姓李和杨的学生情况,请使用至少两种方法实现

select * from 学生表 where 姓名 like '李%' or 姓名 like '杨%'
select * from 学生表 where LEFT (姓名,1)in('李','杨')

12 把学生表拆分成男,女生两个独立的表. 再把两个表合并到一个新表”学生总”

select * into 学生男表 from 学生表 where 性别='男'
select * into 学生女表 from 学生表 where 性别='女'
select * into 学生总 from 学生表
insert into 学生总 select * from 学生男表
insert into 学生总 select * from 学生女表
select * from 学生总

13 查出计算机基础和大学英语课程的成绩情况

select * from 成绩表 where 课程号 in(select 课程号 from 课程表 where 课程名称='计算机基础' or 课程名称='大学英语')

你可能感兴趣的:(数据库,mysql,sql,sqlserver)