MySQL Database DDL Review

Table of contents

What is DDL

Database operations

Inquire

use

create

delete

Table operations

create

Field constraints

type of data

Numeric type

string type

datetime type

Inquire

Revise

delete


What is DDL

The full English name of DDL is Data Definition Language, which is used to define database objects (databases, tables).

Database operations

Inquire

Query all databases: show databases;

Query the current database: select database();

use

Use database: use database name;

create

Create database: create database if not exists database name;

delete

Delete database: drop database if exists database name;

Database in the above syntax can also be replaced by schema. For example: show schemas;

Table operations

create

create table table name(

        Field 1 field type constraint comment field 1 comment,

        ······

        Field n field type constraint comment field n comment

) comment table comment;

Field constraints
constraint describe Keywords
non-null constraint Limit the value of this field to not be null not null
unique constraint Ensure that all data in the fields are unique and non-duplicate unique
primary key constraints The primary key is the unique identifier of a row of data and must be non-empty and unique. primary key
Default constraints When saving data, if the field value is not specified, the default value is used default
foreign key constraints Let the data of the two tables be connected to ensure the consistency and integrity of the data foreign key
type of data
Numeric type

Among thembigint and int are the most commonly used and can be used without them. Thinking too hard about the range of numeric types. For example, the age can be expressed using the int type, and there is no need to deliberately use the tinyint type. The primary key is generally generated using automatic increment, and the type is bigint.

type Size (byte) describe
tinyint 1 small integer value
smallint 2 large integer value
mediumint 3 large integer value
int 4 large integer value
bigint 8 extremely large integer value
float 4 single precision floating point value
double 8 Double precision floating point value
decimal Decimal values ​​(higher precision)
string type

char(n), can store up to n characters, less than n characters, occupy n character space. High performance, waste of space.

varchar(n), can store up to n characters, less than n characters, occupying the actual length character space. Low performance, saving space.

type Size (bytes) describe
char 0 - 255 bytes Fixed length string
varchar 0 - 65535 bytes variable length string
tinyblob 0 - 255 bytes Binary data of no more than 255 characters
tinytext 0 - 255 bytes short text string
blob 0 - 65535 bytes Long text data in binary form
text 0 - 65535 bytes long text data
mediumblob 0 - 16777215 bytes Medium-length text data in binary form
mediumtext 0 - 16777215 bytes Medium length text data
lungblob 0 - 4294967295 bytes Very large text data in binary form
longtext 0 - 4294967295 bytes Very large text data
datetime type

The field create_time creation time and the field update_time update time both use datetime type.

type Format describe
date YYYY - MM - DD date value
time HH : MM : SS time value or duration
year YYYY year value
datetime YYYY - MM - DD HH : MM : SS Mix date and time values
timestamp YYYY - MM - DD HH : MM : SS Mixed date and time values, timestamps

Inquire

Query all tables in the current database: show tables;

Query table structure: desc table name;

Query the table creation statement: show create table table name;

Revise

Add fields: alter table table name add field name type comment comment constraint;

Modify field type: alter table table name modify field name new data type;

Modify field names and field types: alter table table name change old field name new field name type comment comment constraint;

Delete a field: alter table table name drop column field name;

Modify the table name: rename table table name to new table name;

delete

Delete table: drop table if exists table name;

Guess you like

Origin blog.csdn.net/qq_74312711/article/details/134904359