用sql语句创建数据库Student(Sno,Sname, Ssex, Sage,Sdept) SC(Sno,Cno,Grade) C(Cno,Cname,Cpno,Ccredit)并且给我插入一点数据
时间: 2024-01-10 10:04:18 浏览: 96
SQL语言创建数据库(以学生信息为例)
3星 · 编辑精心推荐
好的,以下是创建数据库Student、SC、C的SQL语句:
```
CREATE DATABASE Student;
USE Student;
CREATE TABLE Student(
Sno VARCHAR(10) PRIMARY KEY,
Sname VARCHAR(20) NOT NULL,
Ssex CHAR(2) CHECK(Ssex='男' OR Ssex='女'),
Sage INT CHECK(Sage>=18 AND Sage<=35),
Sdept VARCHAR(20) NOT NULL
);
CREATE TABLE C(
Cno VARCHAR(10) PRIMARY KEY,
Cname VARCHAR(20) NOT NULL,
Cpno VARCHAR(10),
Ccredit FLOAT CHECK(Ccredit>=1 AND Ccredit<=5)
);
CREATE TABLE SC(
Sno VARCHAR(10),
Cno VARCHAR(10),
Grade FLOAT CHECK(Grade>=0 AND Grade<=100),
PRIMARY KEY(Sno, Cno),
FOREIGN KEY(Sno) REFERENCES Student(Sno),
FOREIGN KEY(Cno) REFERENCES C(Cno)
);
```
插入数据:
```
INSERT INTO Student VALUES('01', '张三', '男', 20, '计算机科学与技术');
INSERT INTO Student VALUES('02', '李四', '男', 19, '软件工程');
INSERT INTO Student VALUES('03', '王五', '女', 21, '信息管理');
INSERT INTO C VALUES('01', '数据库', NULL, 3.0);
INSERT INTO C VALUES('02', '数据结构', NULL, 4.0);
INSERT INTO C VALUES('03', '操作系统', '01', 4.5);
INSERT INTO SC VALUES('01', '01', 90.0);
INSERT INTO SC VALUES('01', '02', 85.0);
INSERT INTO SC VALUES('02', '01', 70.0);
INSERT INTO SC VALUES('02', '02', 80.0);
INSERT INTO SC VALUES('03', '01', 95.0);
INSERT INTO SC VALUES('03', '02', 90.0);
INSERT INTO SC VALUES('03', '03', 85.0);
```
阅读全文