MySQL Build table: Primary key, FOREIGN KEY constraint

Source: Internet
Author: User

CREATE DATABASE Db_studentinfo; Use Db_studentinfo;D rop TABLE IF EXISTS t_student; CREATE TABLE t_student (studentid varchar (+) NOT NULL, Stuno varchar (DEFAULT) NULL, stuname varchar DEFA  ULT NULL, Stusex varchar (TEN) default NULL, Stubirthday date default NULL, STURXSJ date default NULL, Stunation   varchar () default NULL, STUZZMM varchar default NULL, ClassId int (one) default null, Studesc text, Stupic varchar () default NULL, PRIMARY KEY (studentid)) engine=innodb default Charset=utf8;drop TABLE IF EXISTS t_class ; CREATE TABLE t_class (classId int (one) not NULL auto_increment, ClassName varchar (DEFAULT) NULL, Gradeid int (1 1) Default NULL, Classdesc text, PRIMARY KEY (classId)) Engine=innodb auto_increment=8 default Charset=utf8;drop TAB LE IF EXISTS T_grade; CREATE TABLE t_grade (gradeid int) not NULL auto_increment, Gradename varchar (DEFAULT) NULL, Gradedesc Tex T, PRIMARY KEY (Gradeid)) Engine=innODB auto_increment=4 DEFAULT Charset=utf8;insert into T_student (StudentID, Stuno, Stuname, Stusex, Stubirthday, Stu RXSJ, Stunation, stuzzmm, ClassId, Studesc, Stupic) VALUES (' 10bea402-23e4-4066-a4fd-534c3798a7d7 ', ' 0011 ', ' Beauty 111 ', ' male ', ' 2014-05-16 ', ' 2014-06-08 ', ' hui ', ' League member ', 5, ' Belle 12222222 ', ' 20140712081542.jpg '), (' + ', ' + ', null,null,null,null, Null,null,4,null,null), (' + ', ' + ', null,null,null,null,null,null,4,null,null), (' A ', ' 2 ', Null,null,null,null, Null,null,4,null,null), (' + ', ' 2 ', null,null,null,null,null,null,4,null,null), (' + ', ' 2 ', null,null,null,null,null , Null,4,null,null), (' 19 ', ' small Three ', ' 21 ', ' Female ', ' 2014-05-14 ', ' 2014-05-14 ', ' Han ', ' Communist Party ', 5, ' is ', ' 20140502030158.jpg '), (' 4 ', ' 11 ', ' 212 ', ' male ', ' 2014-04-08 ', ' 2014-04-05 ', ' Mongolian ', ' CCP Probationary ', 4, ' + ', ' 20140429052236.JPG ', (' 5 ', ' 11 ', ' 21 ', ' Male ', ' 2014-04-08 ', ' 2014-04-05 ', ' Mongolian ', ' CCP Probationary ', 4, ' + ', ' 20140429052433.JPG '), (' 6 ', ' 21 ', ' 2 ', ' Male ', ' 2014-04-08 ', ' 2014-04-05 ', ' Mongol ', Null,4,null,null), (' 7fac0234-59ef-46ea-981a-de50d24741b2 ', ' 000 ', ' 11 ', ' Male ', ' 2014-07-10 ', ' 2014-07-09', ' Han ', ' CCP member ', 4, ' Test picture ', ' 20140710090403.jpg '), (' a1cf592d-d9d4-4de1-b8ff-0db7e9852411 ', ' sss22 ', ' ss2 ', ' female ', ' 2014-05-08 ', ' 2014-05-10 ', ' Mongolian ', ' League member ', 3, ' Big 2 ', ' 20140508022528.jpg '), (' A8A1816B-F24C-4FB9-B160-C8C107C80ACF ', ' 21 ', ' 321 ', ' Male ', ' 2014-05-13 ', ' 2014-05-15 ', ' Mongolian ', ' MDC ', 3, ' 123 ', ' 20140508020709.jpg '), ('  b53b0b8a-7f1c-4254-8cff-e8353902f012 ', ' 121 ', ' 21 ', ' Male ', ' 2014-07-09 ', ' 2014-07-16 ', ' Han ', ' CCP member ', 3, ' + ', '); insert Into T_class (ClassId, ClassName, Gradeid, Classdesc) VALUES (3, ' 09 count ', 2, ' 22 '), (4, ' 08 ', 1, ' 111 '), (5, ' 08 ', 1, ' 222 ') , (6, ' 10 web-based ', 3, ' 22 '), (7, ' 10 ', 3, ' 10-year computer undergraduate '); insert into T_grade (Gradeid, Gradename, Gradedesc) VALUES (1, ' 08 ', ' 111 ' ), (2, ' 09 ', ' 222 '), (3, ' 10 ', ' EXISTS ');D rop TABLE IF t_datadic; CREATE TABLE t_datadic (ddid int (one) not NULL auto_increment, Ddtypeid Int (one) DEFAULT NULL, Ddvalue varchar (20) Default NULL, Dddesc text, PRIMARY KEY (ddid)) Engine=innodb auto_increment=343 DEFAULT charset=utf8;drop TABLE IF E Xists T_datadictype; CREATE TABLE T_datAdictype (Ddtypeid Int (one) not NULL auto_increment, Ddtypename varchar () DEFAULT NULL, ddtypedesc text, Primar Y KEY (Ddtypeid)) Engine=innodb auto_increment=4 DEFAULT Charset=utf8;insert into T_datadic (Ddid, Ddtypeid, DdValue, DDDESC) VALUES (1, 1, ' Male ', ' 1 '), (2,1, ' female ', null), (3,2, ' Communist Party ', null), (4,2, ' CCP probationary ', null), (5,2, ' League member ', NULL), (6,2, ' MDC ', NULL), (7,2, ' NLD leaguer ', null), (8,2, ' dab member ', NULL), (9,2, ' DPP member ', NULL), (10,2, ' Farmer party members ', null), (11,2, ' Zhi Gong Party Party member ', NULL), (12,2, ' JIU San Society member ', NULL), (13,2, ' Taixin leaguer ', null), (14,2, ' independents ', null), (15,2, ' ordinary citizen ', NULL), (16,2, ' compatriots ', null), (17,2, ' traitor ', null), ( 18,2, ' counter-revolutionaries ', null), (334,3, ' Han ', null), (335,3, ' Mongol ', null), (336,3, ' hui ', null), (337,3, ' Tibetan ', null), (338,3, ' Uighur ', null ), (339,3, ' Moinba ', null), (340,3, ' foreign descent ', null), (342,3, ' Haha tribe ', ' one '); insert into T_datadictype (Ddtypeid, Ddtypename, DDTYPEDESC) VALUES (1, ' Gender ', ' 121 '), (2, ' political face ', null), (3, ' family ', null);D ROP TABLE IF EXISTS T_user; CREATE TABLE t_user (userId int (one) not NULL auto_increment, userName varchar) DEFAULT NULL, passWord varchar () default NULL, PRIMARY KEY (userId)) Engine=innodb auto_increment=2 default Charset=utf8;insert into T_user (userId, userName, password) VALUES (1, ' java1234 ', ' n '); ALTER TABLE t_student add constraint fk_t_student forei GN Key (ClassId) REFERENCES t_class (CLASSID); ALTER TABLE T_class add constraint fk_t_class foreign key (Gradeid) refere NCES T_grade (Gradeid); ALTER TABLE t_datadic add constraint fk_t_datadic foreign key (Ddtypeid) REFERENCES T_datadictype ( Ddtypeid);

  

MySQL Build table: Primary key, FOREIGN KEY constraint

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.