02Create database and table

02Create database and table

One, create a database

  • credate database database name;

  • When creating a database, you can specify the character set and character sequence

    Command: create database database name default character set character set name [collate character sequence name];

  • Display database structure: show create database database name

  • Use the use command to select the current database: use database name

  • Modify the character set of the database (the database has been created, and the character set needs to be modified):

    alter database database name character set character set collate character sequence;

  • Delete the database: drop database database name

Second, the naming convention

  • Consists of any letter, Arabic numerals, underscore (_) and $

  • Windows is not case sensitive, MySQL keywords cannot be used, and the name can be up to 64 characters long

Three, storage engine

Before creating a table, you must clarify the storage engine of the table (note: the storage engine is based on the table, the same database, different tables, the storage engine can be different; even the same database table, in different occasions should use different storage engines )

  • View the storage engines supported by the MySQL service instance: show engines

InnoDB storage engine

Features:

  • Support foreign key (foreign key)

  • Support transaction (Transaction)-add, delete, modify and check

If you need to perform a lot of update operations, the innodb storage engine is a better choice

MyISAM

Features:

  • Most tools with check and repair tables

  • Table can be compressed

  • Does not support transactions

  • Does not support foreign key (foreign key)

If you need to execute a large number of select statements, for performance reasons, MyISAM storage engine is a better choice

note:

  • The default storage engine for MySQL 5.6 and 5.7 is InnoDB

  • Temporarily change the storage engine of the current MySQL session command:

    set default_storage_engine=MyISAM;

Four, data type

Integer type (tinyint, smallint, mediumint, int, bigint)

Decimal type

  • Decimal(length,precision) means that the precision is determined (the number of digits after the decimal point is determined)

  • length: determines the maximum number of decimal places;

  • precision: Set the precision (the number of digits after the decimal point)

float、double

String type : enclosed in single quotes

The difference between char and varchar(): fixed length and variable length

  • varchar(255): Indicates that 255 Chinese characters can be stored. If varchar(255) contains only one Chinese character and 2 bytes, then varchar(255) only occupies 2 bytes of storage space

  • char(20): Regardless of whether 20 bytes are stored in char(20), it is counted as 20 bytes

Date type

  • date: indicates the date; the default format is: YYYY-MM-DD

  • time: indicates the time; format: HH:mm:ss

  • datetime and timestamp are mixed types of date and time, the format is: YYYY-MM-DD HH:mm:ss

  • datetime is not affected by time zone timestamp is affected by time zone

  • Inserting null into a datetime type field is null

  • Insert null into the timestamp type field, the value of this field is actually the current date and time of the MySQL server

  • now(): Get the current time of the mysql server

Compound type

MySQL supports two composite data types enum enumeration type and set collection type

  • Enum type: Only a certain value is allowed to be obtained from the collection; an enum type data contains up to 65535 elements

  • set type: allows multiple values ​​to be obtained from a set; a set type data can contain up to 64 elements

Five, create a table

1. Syntax format: create table table name (field name data type [column-level constraint], ...[table-level constraint 1], [table-level constraint 2]) other options (such as storage engine, character set, etc.) Option)

CREATE TABLE `runoob_tbl`(
   `runoob_id` INT UNSIGNED AUTO_INCREMENT,
   `runoob_title` VARCHAR(100) NOT NULL,
   `runoob_author` VARCHAR(40) NOT NULL,
   `submission_date` DATE,
   PRIMARY KEY ( `runoob_id` )
)ENGINE=InnoDB DEFAULT CHARSET=utf8;

2. The essence of creating a new table is to define the structure of the new table. There are three major elements to determine, namely

  • Column name

  • type of data

  • Restrictions

3. Specify the storage engine of the table: engine = storage engine name

Specify the character set of the table: default charset = character set name

Six, constraints

The constraint mechanism to ensure the correctness and integrity of the data in the database is called integrity constraint

  • Constraint range: table-level constraints, column-level constraints

  • Constraints: entity integrity, referential integrity, domain integrity constraints

  • The constraints defined in the column are called column-level constraints

  • As an independent constraint item at the end of the table definition, it is called a table-level constraint

  • If the integrity constraint involves multiple attribute columns, they must be defined at the table level

7. Entity integrity

  • Also known as row integrity, it requires that no identical rows exist in the table, and each row must have a non-empty and non-repeating field value

  • Entity integrity mainly relies on the realization of primary key constraints, which use the keyword primary key

  • The primary key constraint is used to define the primary key of the basic table, which serves as a unique identification, and the value cannot be null

  • If the primary key of a table is a single field, it is defined as a column-level constraint

    Field name data type [other constraints] primary key

  • Defined as a table-level constraint, after defining all the fields in the definition

    primary key (field name)

  • If the primary key of a table is composed of multiple fields, it can only be defined as a table-level constraint

    primary key (field name 1, field name 2)

Unique key (unique) constraint

  • Uniqueness: The unique constraint is used to specify that the value of a certain column or a combination of multiple columns of the basic table is one

  • The unique key can be empty, at most there is only one initial null value

  • The primary key is non-empty and unique

  • Use unique if it is not a primary key but has a unique value

  • Setting unique key constraints can be defined as column-level constraints or table-level constraints

    Column level: field name data type [other constraints] unique

    Table level: unique (field name)

Self-incrementing field

  • Self-incrementing fields can only be defined as column-level constraints

  • The value of MySQL self-incrementing field increases from 1 with a step size of 1

  • Grammatical format

    Field name data type auto_increment

  • Advantages: automatic numbering, fast speed, number type, small footprint, no need to worry about the main key duplication

note:

  1. Each table can only define one self-incrementing field, which must be an integer, and must be defined as a primary key or a unique key

  2. When inserting a null or 0 into an auto-incremented field, the field will be automatically set to the value of bi that was greater than the last inserted value

8. Referential integrity

Referential integrity, also known as referential integrity, refers to the rules between tables. It acts on two or more tables that are related. Through the relationship between the primary key and the foreign key (or unique key), the key values ​​in the table are related. Consistent in the table (implemented through foreign key constraints)

The value of the foreign key field of table A is either null or the value of the primary key field of table B (at this time, table A is called the child table of table B, and table B is called the parent table of table A)

note:

  • The primary key of the parent table and the foreign key of the child table must be defined in the same field;

  • The foreign key and the corresponding primary key can have different names

Syntax rules for column-level constraints:

  • constraint constraint name foreign key (table A field name) references table B (primary key field name) [on delete cascade option] [on update cascade option]

Cascade options: four values

  1. cascade: The parent table records delete/update operations, and the child table records are automatically deleted/updated

  2. set null: The parent table records the delete/update operation, and the corresponding foreign key value in the child table is automatically set to null

  3. no action: The parent table records the delete/update operation. If there is a corresponding record in the child table, the delete/update operation fails

  4. restrict: Same as no action, and is the default value of cascading options

Nine, domain integrity

Also known as column integrity, the data input to the specified column in the table must have the correct data type, format, and valid data range

Realization of domain integrity and user integrity includes non-null constraint (not null), default value constraint (default), unique constraint (unique), check constraint (enum)

  • Set non-null constraints: field name data type [other constraints] not null

  • Set default value constraint: field name data type [other constraints] default default value

X. Table management

  • Copy a table structure: create table new table name like source table;

  • Copy the structure of a table and the records of the source table: create table new table name select * from source table

Guess you like

Origin blog.csdn.net/weixin_42248871/article/details/109910459