建立关系表
假如我们现在要建立三张表
学生表(students):
id
name
gender
科目表(subjects):
id
title
成绩表(scores):
id
score
stutid(外键)
subid(外键)
create database stu chartset=utf8;
use stu;
create table students(int id auto_increment primary key,name varchar(10) not null,gender bit default 1 )
create table subjects(int id auto_increment primary key,title varchar(10) not null)
create table scores(int id auto_increment primary key,score decimal(4,1) not null,stuid int,subid int,foreign key(stuid) references students(id),foreign key(subid) references subjects(id)
);
最有一个分数表的建立,也可以先建立表,然后在添加外键
建立表:create table scores(int id auto_increate primary key not null,score decimal(4,1) not null);
添加外键:alter table scores add