时间:2021-07-01 10:21:17 帮助过:20人阅读
## 删除数据库
DROP DATABASE t14
## 添加库T14
CREATE DATABASE t14
## 添加student表
CREATE TABLE student
(
studentNo INT PRIMARY KEY NOT NULL AUTO_INCREMENT,
loginPwd VARCHAR(20) NOT NULL,
studentName VARCHAR(32) NOT NULL,
sex CHAR(2) DEFAULT ‘男‘ NOT NULL,
gradeId INT NOT NULL,
phone VARCHAR(32) NOT NULL,
address VARCHAR(255) DEFAULT ‘地址不详‘,
bornDate DATETIME,
email VARCHAR(32),
identityCard VARCHAR(18) UNIQUE KEY
)
## 添加grade表
CREATE TABLE grade
(
gradeId INT PRIMARY KEY NOT NULL AUTO_INCREMENT,
gradeName VARCHAR(32) NOT NULL
)
## 添加subject表
CREATE TABLE `subject`
(
subjectId INT PRIMARY KEY NOT NULL AUTO_INCREMENT,
subjectName VARCHAR(32),
gradeId INT,
classHour INT
)
## 添加result表
CREATE TABLE result
(
id INT PRIMARY KEY NOT NULL AUTO_INCREMENT,
studentNo INT NOT NULL,
subjectId INT NOT NULL,
examDate DATETIME,
studentResult INT
)
## 添加外键约束
ALTER TABLE student
ADD CONSTRAINT FK_grade_student_gradeId FOREIGN KEY(gradeId)
REFERENCES grade(gradeId)
ALTER TABLE SUBJECT
ADD CONSTRAINT FK_grade_subject_gradeId FOREIGN KEY(gradeId)
REFERENCES grade(gradeId)
ALTER TABLE result
ADD CONSTRAINT FK_subject_result_subjectId FOREIGN KEY(subjectId)
REFERENCES SUBJECT(subjectId)
ALTER TABLE result
ADD CONSTRAINT FK_student_result_studentNo FOREIGN KEY(studentNo)
REFERENCES student(studentNo)
## 向表grade表中添加数据
INSERT INTO grade(gradeName)
VALUES(‘T14‘)
## 向表student表中添加数据
INSERT INTO student
(studentNo,loginPwd,studentName,sex,gradeId,phone,address,bornDate,email,identityCard)
VALUES(1,‘0000‘,‘张三‘,‘男‘,1,‘188‘,‘中国‘,‘2017年7月11日17:51:15‘,‘qq@126.com‘,‘410149199901010000‘)
## 向表result表中添加数据
INSERT INTO result(studentNo,subjectId,examDate,studentResult)
VALUES(1,1,‘2017年7月11日18:00:51‘,80);
## 向表subject表中添加数据
INSERT INTO SUBJECT(subjectName,gradeId,classHour)
VALUES(‘语文‘,1,60),(‘数学‘,1,60);
## 备份student表
CREATE TABLE student1
(SELECT * FROM student)
## 修改表中数据
UPDATE student SET studentName = ‘李四‘ WHERE studentNo=1;
数据库的简单操作
标签:efault 修改表 删除 class null amd phone ssh prim