数据库表查询

/*

  一对一

*/

CREATE TABLE teacher(

  id INT PRIMARY KEY AUTO_INCREMENT,

  NAME VARCHAR(20)

);

CREATE TABLE student(

  id INT PRIMARY KEY AUTO_INCREMENT,

  s_name VARCHAR(20),

  t_id INT UNIQUE,

  CONSTRAINT wj_001 FOREIGN KEY (t_id) REFERENCES teacher(id)

);

/*

    多对多

*/

CREATE TABLE t1(

  id INT PRIMARY KEY AUTO_INCREMENT,

  NAME VARCHAR(20)


);

CREATE TABLE s1(

  id INT PRIMARY KEY AUTO_INCREMENT,

  s_name VARCHAR(20)



);

CREATE TABLE a(

s_id INT,

t_id INT,

  CONSTRAINT wj_002 FOREIGN KEY (t_id) REFERENCES t1(id),

  CONSTRAINT wj_003 FOREIGN KEY (s_id) REFERENCES s2(id)

);

/*

一对多

*/

CREATE TABLE t(

  id INT PRIMARY KEY AUTO_INCREMENT,

  NAME VARCHAR(20)

);

CREATE TABLE s(

  id INT PRIMARY KEY AUTO_INCREMENT,

  s_name VARCHAR(20),

  t_id INT,

  CONSTRAINT wj_004  FOREIGN KEY (t_id) REFERENCES t(id)

);

你可能感兴趣的:(数据库表查询)