将以下代码给为mysql代码:create table student(sno char(5) primary key, sname varchar(20), sage int, ssex char(2), sdept char(15)); create table course(cno char(3) primary key, cname varchar(20), cpno char(3), ccredit int, foreign key(cpno) references course); create table sc( sno char(5), cno char(3), grade int, primary key(sno,cno), foreign key(sno) references student, foreign key(cno) references course, check(grade>=0 and grade<=100));
时间: 2024-01-04 07:02:14 浏览: 386
创建student、course、sc表代码.sql
以下是对应的 MySQL 代码:
```
-- Connect to the database
mysql -h hostname -u username -p password
-- Create the student table
CREATE TABLE student (
sno CHAR(5) PRIMARY KEY,
sname VARCHAR(20),
sage INT,
ssex CHAR(2),
sdept CHAR(15)
);
-- Create the course table
CREATE TABLE course (
cno CHAR(3) PRIMARY KEY,
cname VARCHAR(20),
cpno CHAR(3),
ccredit INT,
FOREIGN KEY (cpno) REFERENCES course (cno)
);
-- Create the sc table
CREATE TABLE sc (
sno CHAR(5),
cno CHAR(3),
grade INT,
PRIMARY KEY (sno, cno),
FOREIGN KEY (sno) REFERENCES student (sno),
FOREIGN KEY (cno) REFERENCES course (cno),
CHECK (grade >= 0 AND grade <= 100)
);
-- Close the connection
QUIT;
```
需要注意的是,在 MySQL 中,`CHECK` 约束不能使用函数,只能使用常量表达式。因此,`CHECK(grade >= 0 AND grade <= 100)` 不能写成 `CHECK(BETWEEN grade AND 0 AND 100)`。
阅读全文