数据库实验二
1、创建数据库
- --**************************
- --*** 1.建立数据库SC ***
- --**************************
- create database SC
- go
- ----------------------------
- --使用、定位数据库SC
- ----------------------------
- use SC
- go
2、创建数据表
- --**************************
- --*** 2.创建数据表 ***
- --**************************
- ----------------------------
- --1)创建表Student
- ----------------------------
- create table Student
- (
- Sno char(9) primary key, /*Sno是主码 列级完整性约束条件 实体完整性*/
- Sname char(10) unique, /*Sname取唯一值,不重复*/
- Ssex char(2),
- Sage smallint, /*类型为smallint*/
- Sdept char(20) /*所在系*/
- ); /*;要加*/
- ----------------------------
- --2)创建表Course
- ----------------------------
- create table Course
- (
- Cno char(4) primary key, /*列级完整性约束条件,Cno是主码*/
- Cname char(20),
- Cpno char(4), /*Cpno的含义是先行课*/
- Ccredit smallint,
- foreign key (Cpno) references Course(Cno)
- /*表级完整性约束条件,Cpno是外码,被参照表是Course,被参照列是Cno*/
- );
- ----------------------------
- ----------------------------
- --3)创建表SC
- ----------------------------
- create table SC
- (
- Sno char(9),
- Cno char(4),
- Grade smallint,
- primary key (Sno,Cno),
- /*主码有两个属性构成,必须作为表级完整性进行定义*/
- foreign key (Sno) references Student(Sno),
- /*表级完整性约束条件,Sno是外码,被参照表是Student*/
- foreign key (Cno) references Course(Cno)
- /*表级完整性约束条件,Cno是外码,被参照表示Course*/
- );
- ----------------------------
- go
3、插入数据表
- --**************************
- --*** 3.插入数据 ***
- --**************************
- ----------------------------
- ---1)插入Student数据
- ----------------------------
- insert into Student values('200215121','李勇','男',20,'CS') /*向表Student中插入一个元组*/
- insert into Student values('200215122','刘晨','女',19,'IS') /*向表Student中插入一个元组*/
- insert into Student values('200215123','王敏','女',18,'MA') /*向表Student中插入一个元组*/
- insert into Student values('200215125','张立','男',19,'IS') /*向表Student中插入一个元组*/
- ----------------------------
- ----------------------------
- ---2)插入Course数据
- ----------------------------
- --注意顺序(先行课问题) 注意添加一个空元素用 null
- insert into Course values('2','数学',null,2)/*向表Course中插入一个元组*/
- insert into Course values('6','数据处理',null,2)/*向表Course中插入一个元组*/
- insert into Course values('4','操作系统','6',3)/*向表Course中插入一个元组*/
- insert into Course values('7','PASCAL语言','6',4)/*向表Course中插入一个元组*/
- insert into Course values('5','数据结构','7',4)/*向表Course中插入一个元组*/
- insert into Course values('1','数据库','5',4)/*向表Course中插入一个元组*/
- insert into Course values('3','信息系统','1',4)/*向表Course中插入一个元组*/
- ----------------------------
- ----------------------------
- ---3)插入SC数据
- ----------------------------
- insert into SC values('200215121','1',92)/*向表SC中插入一个元组*/
- insert into SC values('200215121','2',85)/*向表SC中插入一个元组*/
- insert into SC values('200215121','3',88)/*向表SC中插入一个元组*/
- insert into SC values('200215122','2',90)/*向表SC中插入一个元组*/
- insert into SC values('200215122','3',80)/*向表SC中插入一个元组*/
- ----------------------------
4、查询数据表
- --**************************
- --*** 4.查询数据 ***
- --**************************
- ----------------------------
- select * from student
- select * from course
- select * from sc
- select student.sno as 学号,sname as 姓名,cname as 课程名,grade as 成绩
- from student,course,sc
- where student.sno=sc.sno and course.cno=sc.cno
- order by 课程名,成绩 desc
- ----------------------------