create table Student115( sno char(7) primary key comment '学号', sname varchar(10) not null, ssex char(2) check(gender in('男','女')), sage int check(sage>=15 and sage<=45), sdept varchar(20) DEFAULT '计算机系' ); create table course115( cno char(10) primary key, cname varchar(20) not null, credit int check(credit>0), semester int check(credit>0), period int not null ); create table sc115( sno char(7), cno varchar(10), grade int check(grade>=0 and grade<=100), primary key (student115,course115,sc1115), foreign key(sno) references Student115(sno), foreign key(cno) references course115(cno) );为什么无法运行
时间: 2023-03-25 08:04:26 浏览: 162
可能是因为在创建 sc115 表时,主键的命名有误,应该是 primary key (sno,cno),而不是 primary key (student115,course115,sc1115)。此外,semester 字段的约束条件应该是 semester>=1 and semester<=2,而不是 credit>。
相关问题
create database Schhool115; use schhool115; create table Student115( sno char(7) primary key comment '学号', sname varchar(10) not null, ssex char(2) check(gender in('男','女')), sage int check(sage>=15 and sage<=45), sdept varchar(20) DEFAULT '计算机系' ); crea
te database Schhool115; use schhool115; create table Student115( sno char(7) primary key comment '学号', sname varchar(10) not null, ssex char(2) check(gender in('男','女')), sage int check(sage>=15 and sage<=45), sdept varchar(20) DEFAULT '计算机系' );
这是一个 SQL 数据库的创建语句,用于创建一个名为 Schhool115 的数据库,并在其中创建一个名为 Student115 的表格,包含学号、姓名、性别、年龄和所在系别等字段。
将以下代码给为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));
以下是对应的 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)`。
阅读全文