FOREIGN KEY (`Stu_id`) REFERENCES `Student` (`Stu_id`),
时间: 2023-10-24 13:07:22 浏览: 43
这段引用是关于数据库中外键的定义和使用的代码示例。在这个示例中,`FOREIGN KEY (`Stu_id`) REFERENCES `Student` (`Stu_id`)` 表示在一个表中创建一个外键,将`Stu_id`列与`Student`表中的`Stu_id`列进行关联。这样做可以确保在插入或更新数据时,`Stu_id`的值必须存在于`Student`表中的`Stu_id`列中。 这样的定义可以帮助确保数据的完整性和一致性。
相关问题
-- 如果存在则先删除 drop database if exists jzlg; -- 创建数据库 create database jzlg; -- 使用自己创建的数据库 use jzlg; -- 学生信息表 -- 如果存在则先删除 drop table if exists tb_student; -- 创建新表 create table tb_student( stu_id int primary key auto_increment, stu_name varchar(50) unique, create_date date, gender varchar(2), age int check(age>18 and age<24) )auto_increment=1; -- 查询 select * from tb_student; -- 学生成绩 -- 如果存在则先删除 drop table if exists tb_score; -- 创建新表 create table tb_score( score_id int primary key auto_increment, stu_id int, c decimal(4,2), s decimal(4,2), html decimal(4,2), constraint fk_stu_id foreign key (stu_id) references tb_student(stu_id) )auto_increment=1; -- 查询 select * from tb_score; 在这两张表里面添加数据每个表添加三条数据
以下是向两张表中添加三条数据的 SQL 语句:
向 tb_student 表中添加三条数据:
```
insert into tb_student(stu_name, create_date, gender, age) values('小明', '2002-01-01', '男', 19);
insert into tb_student(stu_name, create_date, gender, age) values('张三', '2003-02-02', '男', 20);
insert into tb_student(stu_name, create_date, gender, age) values('李四', '2004-03-03', '女', 21);
```
向 tb_score 表中添加三条数据:
```
insert into tb_score(stu_id, c, s, html) values(1, 80, 85, 90);
insert into tb_score(stu_id, c, s, html) values(2, 50, 40, 60);
insert into tb_score(stu_id, c, s, html) values(3, 45, 60, 25);
```
注意:在向 tb_score 表中插入数据时,需要保证 tb_student 表中已经存在对应的 stu_id。
1,新建三张表,分别为班级表(classes),学生表(student),学生成绩表(student_score) 对应的字段如下: 班级表: 班级id(class_id),班级名称(class_name),创建时间(create_time) 学生表: 班级id(class_id),学生id(stu_id),学生姓名(stu_name),年龄(age),性别(sex),家庭住址(address),生日(brithday) 学生成绩表: 班级id(class_id),学生id(stu_id),分数(score) 交付内容:三张表的完整建表语句,每张表需包含主键或者索引。
班级表(classes)建表语句:
```
CREATE TABLE classes (
class_id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
class_name VARCHAR(50) NOT NULL,
create_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
```
学生表(student)建表语句:
```
CREATE TABLE student (
class_id INT NOT NULL,
stu_id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
stu_name VARCHAR(50) NOT NULL,
age INT NOT NULL,
sex ENUM('M', 'F') NOT NULL,
address VARCHAR(100) NOT NULL,
birthday DATE NOT NULL,
INDEX(class_id),
FOREIGN KEY (class_id) REFERENCES classes(class_id)
);
```
学生成绩表(student_score)建表语句:
```
CREATE TABLE student_score (
class_id INT NOT NULL,
stu_id INT NOT NULL,
score DECIMAL(5,2) NOT NULL,
PRIMARY KEY (class_id, stu_id),
INDEX (class_id),
FOREIGN KEY (class_id) REFERENCES classes(class_id),
FOREIGN KEY (stu_id) REFERENCES student(stu_id)
);
```
相关推荐
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)