建立JXGL和YGGL和Teachment数据库并插入数据

创建数据库JXGL

CREATE DATABASE JXGL

 建立student,course, sc表

USE JXGL
CREATE TABLE Student
(
	Sno char(10) NOT NULL PRIMARY KEY,
	Sname char(8) NOT NULL,
	Ssex char(2) NOT NULL,
	Sage smallint,
	Sdept char(10),
	)

USE JXGL
CREATE TABLE Course
(
	Cno char(4) PRIMARY KEY,
	Cname char(40) NOT NULL,
	Cpno char(4),
	Ccredit smallint,
	)
USE JXGL
CREATE TABLE SC
(
	Sno char(10), 
	Cno char(4),
	Grade smallint,
	CONSTRAINT PK_SC PRIMARY KEY(Sno, Cno),
	FOREIGN KEY(Sno) REFERENCES Student(Sno),
	FOREIGN KEY(Cno) REFERENCES Course(Cno),
	)

分别向三张表中插入数据

--向Student表中插入数据
INSERT INTO Student
values ('201215121', '李勇', '男', 20, 'CS'),
	   ('201215122', '刘晨', '女', 19, 'CS'),
	   ('201215123', '王敏', '女', 18, 'MA'),
	   ('201215124', '张立', '男', 19, 'IS');

--向Course表中插入数据
INSERT INTO Course(Cno, Cname, Cpno, Ccredit)
values ('1', '数据库', '5', 4);
INSERT INTO Course(Cno, Cname, Cpno, Ccredit)
values ('2', '数学', NULL, 2);
INSERT INTO Course(Cno, Cname, Cpno, Ccredit)
values ('3', '信息系统', '1', 4);
INSERT INTO Course(Cno, Cname, Cpno, Ccredit)
values ('4', '操作系统', '6', 3);
INSERT INTO Course(Cno, Cname, Cpno, Ccredit)
values ('5', '数据结构', '7', 4);
INSERT INTO Course(Cno, Cname, Cpno, Ccredit)
values ('6', '数据处理', NULL, 2);
INSERT INTO Course(Cno, Cname, Cpno, Ccredit)
values ('7', 'PASCAL语言', '6', 4);

--向SC表中插入数据
INSERT INTO SC
values ('201215121', '1', 92),
	   ('201215121', '2', 85),
	   ('201215121', '3', 88),
       ('201215122', '2', 90),
	   ('201215122', '3', 80);

 建立数据库YGGL

CREATE DATABASE YGGL

创建department,rank,people表,建立实验要求常用约束

USE YGGL
CREATE TABLE Department
(
	DepartmentID int NOT NULL primary key,
	DepartmentName varchar(50) NOT NULL,
	DepartmentRemark text NULL,
	)

--建立Rank表
USE YGGL
CREATE TABLE Rank
(
	RankID int NOT NULL primary key,
	RankName varchar(50) NOT NULL,
	RankRemark text NULL,
	)

--建立People表
USE YGGL
CREATE TABLE People
(
	PeopleID int NOT NULL primary key,
	DepartmentID int NOT NULL references Department(DepartmentID),
	RankID int NOT NULL references Rank(RankID),
	PeopleName varchar(50) NOT NULL,
	PeopleSex varchar(50) NOT NULL CHECK(PeopleSex = '男' or PeopleSex = '女'),
	PeopleBirth smalldatetime NULL,
	PeopleSalary decimal(12, 2) NOT NULL CHECK(PeopleSalary >= 100 and PeopleSalary <= 100000),
	PeoplePhone varchar(20) NOT NULL,
	PeopleAddress varchar(3000) NULL,
	PeopleAddTime smalldatetime NULL
	)

分别向三张表中插入数据

--向Department插入数据
INSERT INTO Department
values
(1, '市场部', '1234'),
(2, '软件部', '1235'),
(3, '企划部', '1236'),
(4, '硬件部', '1237'),
(5, '测试部', '1238'),
(6, '产品部', '1238'),
(7, '实施部', '12310');

--向Rank表中插入数据
INSERT INTO Rank
values 
(1, '初级', '123'),
(2, '中级', '124'),
(3, '高级', '125');

--向People表中插入数据
INSERT INTO Peole
values 
(1, 7, 1, '刘备', '男', NULL, 5000, '138199981', '中国', NULL),
(2, 1, 1, '张飞', '男', NULL, 5000, '138199982', '中国', NULL),
(3, 2, 1, '关羽', '男', NULL, 5000, '138199983', '中国', NULL),
(4, 7, 3, '赵云', '男', NULL, 5000, '138199984', '中国', NULL),
(5, 7, 3, '章子怡', '女', NULL, 5000, '138199985', '中国', NULL),
(6, 3, 2, '刘涛', '女', NULL, 5000, '138199986', '中国', NULL),
(7, 3, 1, '刘亦菲', '女', NULL, 5000, '138199987', '中国', NULL),
(8, 5, 1, '白敬亭', '男', NULL, 5000, '138199988', '中国', NULL),
(9, 6, 3, '周迅', '女', NULL, 5000, '138199989', '中国', NULL),
(10, 4, 3, '秦海璐', '女', NULL, 5000, '13912341', '中国', NULL),
(11, 7, 2, '李郡主', '女', NULL, 5000, '13966666', '中国', NULL),
(12, 7, 1, '赵丽颖', '女', NULL, 5000, '13955555', '中国', NULL),
(13, 7, 1, '孙怡', '女', NULL, 5000, '13944444', '中国', NULL),
(14, 7, 1, '董子健', '男', NULL, 5000, '13933333', '中国', NULL),
(15, 7, 1, '罗翔', '男', NULL, 5000, '13922222', '中国', NULL),
(16, 7, 1, '范冰冰', '女', NULL, 5000, '13811111', '中国', NULL),
(17, 7, 1, '盆栽哥', '男', NULL, 5000, '13622222', '中国', NULL),
(18, 7, 1, '文章', '男', NULL, 5000, '13633333', '中国', NULL),
(19, 4, 2, '闫妮', '女', NULL, 5000, '13844444', '中国', NULL);

建立数据库Teachment

CREATE DATABASE teachmanage

分别建立表speciality,student,course,score,teacher,lecture。


USE teachmanage
CREATE TABLE speciality
(
	specno char(6) NOT NULL PRIMARY KEY,
	specname char(16) NULL,
	)

USE teachmanage
CREATE TABLE student
(
	sno char(6) NOT NULL PRIMARY KEY,
	sname char(8) NOT NULL,
	ssex char(2) NOT NULL,
	sbirthday date NOT NULL,
	tc tinyint NULL,
	specno char(6) NOT NULL,
	)

USE teachmanage
CREATE TABLE course
(
	cno char(4) NOT NULL PRIMARY KEY,
	cname char(16) NOT NULL,
	credit tinyint NULL,
	)

USE teachmanage 
CREATE TABLE score
(
	sno char(6) NOT NULL ,
	cname char(16) NOT NULL,
	grade tinyint NULL,
	CONSTRAINT PK_score PRIMARY KEY(sno, cname),
	)

USE teachmanage
CREATE TABLE teacher
(
	tno char(6) not null primary key,
	tname char(8) not null ,
	tsex char(2) not null,
	tbirthday date not null,
	title char(12) null,
	school char(12) null
	)

USE teachmanage
CREATE TABLE lecture
(
	tno char(6) not null,
	cno char(4) not null,
	location char(10) null,
	constraint PK_lecture primary key(tno, cno)
	)

依次向表中插入数据

--插入数据
insert into speciality
values('080701', '电子信息工程'),
('080702', '电子科学与技术'),
('080703', '通信工程'),
('080901', '计算机科学与技术'),
('080902', '软件工程'),
('080903', '网络工程');

insert into student
values('221001', '成远博', '男', '2002-04-17', '54', '080901'),
('221002', '傅春华', '女', '2001-10-03', '52', '080901'),
('221003', '路勇', '男', '2002-03-15', '52', '080901'),
('226001', '卫婉如', '女', '2001-08-21', '54', '080701'),
('226002', '孟茜', '女', '2002-12-19', '50', '080701'),
('226004', '夏志强', '男', '2001-09-08', '54', '080701');

insert into course
values('1004', '数据库系统', 4),
('1012', '计算机系统结构', 3),
('1201', '英语', 5),
('4008', '通信原理', 5),
('8001', '高等数学', 5);

insert into score
values('221001', '1004', 94),
('221001', '1201', 92),
('221001', '8001', 92),
('221002', '1004', 87),
('221002', '1201', 86),
('221002', '8001', 88),
('221003', '1004', 93),
('221003', '1201', 93),
('221003', '8001', 86),
('226001', '1201', 92),
('226001', '4008', 93),
('226001', '8001', 92),
('226002', '1201', NULL),
('226002', '4008', 78),
('226002', '8001', 75),
('226004', '1201', 93),
('226004', '4008', 86),
('226004', '8001', 91);

insert into teacher
values('100003', '杜明杰', '男', '1978-11-04', '教授', '计算机学院'),
('100018', '严芳', '女', '1994-09-21', '讲师', '计算机学院'),
('120032', '袁书雅', '女', '1991-07-18', '副教授', '外国语学院'),
('400006', '范慧英', '女', '1982-12-25', '教授', '通信学院'),
('800014', '简毅', '男', '1987-05-13', '副教授', '数学学院');

insert into lecture
values('100003', '1004', '2-106'),
('120032', '1201', '4-204'),
('400006', '4008', '6-114'),
('800014', '8001', '3-219');

为了省事,已将代码模板复制到上面,有的需要稍作修改。根据实验要查询的操作可以适当修改插入数据,以达到查询效果 。如果你不会建表插数据等,请不要直接copy,用手一个一个打出来

                                                                                                                -------ericrisk

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