mysql 建表 插入数据 添加删除主外键

  • Post author:
  • Post category:mysql


-- CREATE DATABASE school;
-- use school;
-- CREATE TABLE student1
-- (
--  id  INT UNIQUE,
--  sno INT PRIMARY KEY,
--  sname CHAR(10),
--  ssex CHAR(2),
--  sdept char(20),
-- sprovince CHAR(10),
-- scity CHAR(9),
-- sstreet CHAR(9),
-- sbirthday date DEFAULT '2010-01-01'
-- );
-- USE school;
-- INSERT INTO student1 VALUES(2,2,'李丽','女','数学系','江苏','苏州','长桥','1997-07-18');
-- INSERT INTO student1 VALUES(4,4,'张天宇','男','计算机系','上海','上海','仁义','1991-07-07');
-- INSERT INTO student1 VALUES(5,5,'李大奎','男','计算机系','浙江','杭州','东营','1996-02-08');
-- INSERT INTO student1 VALUES(6,6,'张思源','男','数学系','广东','深圳','华强','1989-02-06');
-- INSERT INTO student1 VALUES(7,7,'tony','男','中文','浙江','杭州','东营','1996-05-29');
-- INSERT INTO student1 (id,sno,sname,ssex,sdept)VALUES(9,9,'nicy','女','中文');
-- INSERT INTO student1 VALUES(10,9,'李彤','女','数学系','山东','青岛','人民','1993-01-01');
-- CREATE TABLE course
-- (
-- cno int auto_increment PRIMARY KEY,
-- cname  CHAR(9),
-- cpno INT,
-- ccridit INT
-- );
-- --添加主外键 
-- -- ALTER table course
-- add CONSTRAINT cno_sno FOREIGN KEY (cno) REFERENCES student1(sno);
-- 
-- 删除主外键
-- ALTER table course 
-- DROP FOREIGN KEY cno_sno;
-- INSERT INto course (cname,cpno,ccridit) VALUES('java',2,3);
-- INSERT INto course (cname,cpno,ccridit) VALUES('c++',4,2);
-- INSERT INto course (cname,ccridit) VALUES('数学',5);
-- INSERT INto course (cname,cpno,ccridit) VALUES('c',3,3);
-- INSERT INto course (cname,ccridit) VALUES('英语',5);
-- INSERT INto course (cname,cpno,ccridit) VALUES('数据结构',3,3);



版权声明:本文为wkakasa原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。