Msyql 通过一个小练习学习主外键

hao记性不如烂笔头,多敲敲代码建个实例表比看啥文章都强


select * from course;
create database if not exists temp;
use temp;
create table if not exists x1(
    id int not null auto_increment primary key ,
    name char(20),
    age tinyint,
    gender boolean not null default 0,
    address varchar(20)
);
delete from x1 where age =6;
select user();
create table x2
(
	name char(10) null,
	class varchar(15) null,
	type char(5) null,
	birth date default 19990101 null,
    id int not null,
	constraint x2_fk_x1_id
		foreign key (id) references x1 (id)
);

alter table x2
	add x2_id int;

create unique index x2_x2_id_uindex
	on x2 (x2_id);

alter table x2
	add constraint x2_pk
		primary key (x2_id);

# create database if not exists student;
create table if not exists student(
    stu_id int auto_increment unique,
    stu_name char(10),
    stu_gender boolean default 0,
    stu_class char(15),
    stu_birth date default 20000101
);
create table if not exists course(
    crs_id int unique,
    crs_name char(20)
);
create table if not exists grades(
    gds_stu_id int,
    gds_Mandarin tinyint,
    gds_Math tinyint,
    gds_History tinyint,
    gds_English tinyint
);
alter table student auto_increment=10000001; # 设置自增的起始值
insert into student (stu_name, stu_gender, stu_class, stu_birth) values
('刘华强',1,'三次元の1班',19780612),
('拉菲',0,'駆逐艦1班',20100214),
('独立',0,'空母ニ班',20100214),
('香格里拉',0,'空母①班',20100214),
('胡德',0,'戦艦1班',20100214);
insert into course (crs_id, crs_name) values
(5688,'国語'),
(5321,'英語'),
(3443,'数学'),
(5656,'歴史');
alter table grades add name char(20);                               # 插入字段
alter table grades change name gds_name char(20);                   # 重命名字段
alter table grades modify gds_name char(20) null after gds_stu_id;  # 重排列字段

alter table course add crs_type char(5);
insert into grades (gds_stu_id,gds_name,gds_Mandarin, gds_Math, gds_History, gds_English) VALUES
(10000005,'フード',98,73,91,45),
(10000006,'ラフィー',38,43,93,76),
(10000007,'シャングリラ',58,87,56,86),
(10000008,'りゅはちん',76,123,93,88),
(10000009,'インディペンデンス',43,76,67,73);

# 学生学号设置主键
alter table student modify stu_id int auto_increment comment '学号';
alter table student
	add constraint student_pk
		primary key (stu_id);
# 成绩表中学号的是学生中学号的外键
alter table grades
	add constraint grades_student_stu_id_fk
		foreign key (gds_stu_id) references student (stu_id);

# 学生的选课信息
create table curriculum
(
	stu_id int null comment '学号',
	stu_name char(20) null comment '学生名字',
	crs_id int null comment '所选科目的id'
)
comment '课表';

alter table course
	add constraint course_pk
		primary key (crs_id);

alter table curriculum
	add constraint curriculum_course_crs_id_fk
		foreign key (crs_id) references course (crs_id);
alter table curriculum
add constraint curriculum_course_stu_id_fk
foreign key (stu_id) references student (stu_id);

alter table grades
add constraint grades_pk
primary key (gds_stu_id);

Msyql 通过一个小练习学习主外键_第1张图片

Msyql 通过一个小练习学习主外键_第2张图片
Msyql 通过一个小练习学习主外键_第3张图片
Msyql 通过一个小练习学习主外键_第4张图片
constraint 约束
reference 参考

外键是别人表的主键,当前表的字段对于主键表来说就是外键,外键内容可以重复但只能是主键中的内容
主键唯一且不可以重复,删除主键之前必须清空外键

你可能感兴趣的:(SQL,主键,外键,mysql)