MySQL summary (four)-DDL language

Table management

Library management

One, create a library

create database [if not exists] library name [character set character set name];

Two, modify the library

alter database library name character set character set name;

Three, delete the library

drop database 【if exists】 库名;

type of data

1. Numerical

1. Integer
tinyintsmallintmediumintint/integerbigint
	1         2        3          4            8

Features:

  1. Both unsigned and signed can be set, the default is signed, and unsigned is set by unsigned
  2. If it exceeds the range, an out or range exception will be reported, and the critical value will be inserted
  3. The length can not be specified, there will be a length by default,

The length represents the maximum width of the display. If it is not enough, the left side is filled with 0, but it needs to be matched with zerofill, and it becomes an unsigned integer by default

2. Floating point
定点数:decimal(M,D)
浮点数:
	float(M,D)   4
	double(M,D)  8

Features:

  1. M represents the number of integer part + decimal part, D represents the decimal part
  2. If it exceeds the range, an out or range exception will be reported, and the critical value will be inserted
  3. Both M and D can be omitted, but for fixed-point numbers, M defaults to 10 and D defaults to 0
  4. If the accuracy requirements are higher, the fixed-point number will be given priority

Two, character type

char、varchar、binary、varbinary、enum、set、text、blob

char: A fixed-length character, written as char(M), the maximum length cannot exceed M, where M can be omitted, and the default is 1

varchar: Variable-length characters, written as varchar(M), the maximum length cannot exceed M, and M cannot be omitted

Three, date type

year
date date
time time
datetime date + time 8
timestamp date + time 4 is more susceptible to the influence of time zone, syntax mode, version, and can better reflect the real time in the current time zone

Common constraints

1. Common constraints

NOT NULL: non-empty, the value of the field is required
UNIQUE: unique, the value of the field cannot be repeated
DEFAULT: default, the value of the field does not need to be manually inserted with a default value
CHECK: check, mysql does not support
PRIMARY KEY: primary key, this field The value of is not repeatable and non-empty unique+not null
FOREIGN KEY: foreign key, the value of this field refers to the field of another table

Primary key and unique
1. The difference:

①, a table has at most one primary key, but there can be multiple unique
②, the primary key is not allowed to be empty, the only can be empty

2. The same points

Both are unique and
all support key combinations, but not recommended

Foreign key:

1. Used to limit the relationship between the two tables. The field value of the slave table refers to a field value of the main table.
2. The foreign key column and the referenced column of the main table require the same type and the same meaning. The name is not required.
3. The main table The quoted column requires a key (usually the primary key)
4. Insert data, first insert the main table

To delete data, delete the slave table first.
You can delete the records of the master table in the following two ways

Method 1: cascade delete
ALTER TABLE stuinfo ADD CONSTRAINT fk_stu_major FOREIGN KEY(majorid) REFERENCES major(id) ON DELETE CASCADE;
Method 2: cascade blank
ALTER TABLE stuinfo ADD CONSTRAINT fk_stu_major FOREIGN KEY(majorid) REFERENCES major(id) ON DELETE SET NULL;

Two, add constraints when creating a table

create table 表名(
	字段名 字段类型 not null,#非空
	字段名 字段类型 primary key,#主键
	字段名 字段类型 unique,#唯一
	字段名 字段类型 default,#默认
	constraint 约束名 foreign key(字段名) references 主表(被引用列)
)

注意:

			支持类型		可以起约束名			
列级约束		除了外键		不可以
表级约束		除了非空和默认	可以,但对主键无效
列级约束可以在一个字段上追加多个,中间用空格隔开,没有顺序要求

Three, add or delete constraints when modifying the table

1. Not empty

添加非空

alter table table name modify column field name field type not null;

删除非空

alter table table name modify column field name field type;

2. Default

添加默认

alter table table name modify column field name field type default value;

删除默认

alter table table name modify column field name field type;

3. Primary key

添加主键

alter table table name add [constraint constraint name] primary key (field name);

删除主键

alter table 表名 drop primary key;

4. Unique

添加唯一

alter table table name add [constraint constraint name] unique (field name);

删除唯一

alter table table name drop index index name;

5. Foreign keys

添加外键

alter table table name add [constraint constraint name] foreign key (field name) references the main table (referenced column);

删除外键

alter table table name drop foreign key constraint name;

Four, self-increasing column

特点

  • You do not need to manually insert the value, you can automatically provide the sequence value, the default starts from 1, the step size is 1
    auto_increment_increment
    If you want to change the starting value: manually insert the value
    If you want to change the step size: change the system variable
    set auto_increment_increment=value;
  • A table has at most one self-increasing column
  • Self-incrementing column can only support numeric type
  • The self-increasing column must be a key
4.1, set the self-growth column when creating a table

create table table (
          field name field type constraint auto_increment
)

4.2, set the self-growth column when modifying the table

alter table table modify column field name field type constraint auto_increment

4.3, delete the self-growth column

alter table table modify column field name field type constraint

Guess you like

Origin blog.csdn.net/qq_43518425/article/details/113749046