SQL create table

A concept

Two principles

Three use

1) Create a table of statement
create table 'tablename'(表信息);
2) Create a list of general statements
create table 'tablename'(
  'id' int(40),
  'name' int(40),
  'sex' int(40)
);

Comma behind each column, the last one without a comma.
Format: 'Column name' data type (length),

3) create table statement limiting the columns (and columns declared in the same row)
create table 'tablename'(
  'id' int(40) NOT NULL PRIMARY KEY AUTO_INCREMENT COMMENT '备注',
  'name' int(40)
);

NOT NULL is not empty
PRIMARY KEY key primary
AUTO_INCREMENT growth since
the COMMENT 'Remarks' column Notes

4) create table statement limit (row and column is not declared on the same line)
create table 'tablename'(
  'id' int(40) NOT NULL PRIMARY KEY AUTO_INCREMENT COMMENT '备注',
  'name' int(40),
  'uid' int(50),
  INDEX uid('uid')
);

INDEX uid ( 'uid'): disposed index
PRIMARY KEY ( 'id'): set the primary key
PRIMARY KEY (id, name): setting a composite primary key

5) Table View created
desc 表名;
show create table 表名;
6) Add foreign key after creating the table
alter table 表名
    add constraint 外键名 foreign key (列名) references Course  (列名);

Four notes

1)

AUTO_INCREMENT column primary key must be set to PRIMARY KEY and set together.

2)

PRIMARY KEY (id, name) when setting a composite primary key do not add '', ( 'id') being given such

Published 23 original articles · won praise 3 · Views 4889

Guess you like

Origin blog.csdn.net/weixin_38615170/article/details/90411789