"Day2" MySQL primary key, foreign key, self-increment

Source: Internet
Author: User

Primary key: Primary key A table has a single primary key, and the key value can be unique. You can set the primary key to self-increment.

Foreign key: Represents the related relationship between two relationships. A table with a foreign key of another relationship as the primary key is called the primary table, and a table with a key in it is called the primary table from the table.

Self-increment: CREATE TABLE t1 (ID int primary key auto_increment,name char (10));

CREATE tableclass(CID int primary key auto_increment,caption char (20) notnull); #创建class表 Insert intoclass(caption) VALUES ('three-year Class II'),('Three shifts a year'),('one class for three years'); #插入班级数据 CREATE TABLE student (SID int primary key Auto_increment,sname char (20) notNull,gender enum ('male','female'), class_id int notNull,constraint foreign KEY (class_id) referencesclass(CID) on the DELETE cascade on UPDATE cascade) #创建学生表, class ID associated to the class table Cidinsert into student (sname,gender,class_id) v Alues ('Steel Egg','female', 1), ('Steel Hammer','female', 1), ('Shanbao','male', 2) #插入数据create table teacher (tid int primary key Auto_increment,tname char (20) notnull); Insert into teacher (Tname) VALUES ('Puerto'),('Cang Jing'),('Rice Island Love'), CREATE TABLE course (CID int primary key Auto_increment,cname char (20) notnull,teach_id int notnull,constraint foreign KEY (teach_id) References teacher (tid) on DELETE cascade on UPDATE cascade), insert INTO course (cname,teach_id) VALUES ('Biological', 1), ('Sports', 1), ('Physical', 2CREATE TABLE score (SID int primary key auto_increment,student_id int notnull,course_id int notNull,number Int (3) notnull,constraint foreign KEY (student_id) references student (SID) on DELETE cascade on update Cascade,constraint Foreig N Key (course_id) References course (CID) on DELETE cascade on UPDATE cascade), insert into course values (1,1,1,60), (2,1,2,59), (3,2,2,100);

"Day2" MySQL primary key, foreign key, self-increment

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.