MySQL must know must learn notes Chapter 21 to create and manipulate tables

Example of creating a table:
Insert picture description here
MySQL ignores the spaces in the statement and can format the statement like the previous example.

The created table name must not exist, otherwise an error will be reported. If you want to create a table when it does not exist, you should add IF NOT EXISTS after the table name:

CREAT TABLE IF NOT EXISTS tableName ... 

The NOT NULL in the column definition means that this field cannot be empty, and NULL means that the sub-pay can be empty. The default value is NULL.

NULL is no value, it is not an empty string.

The primary key value must be unique. If the primary key is multiple columns, the combined value of these columns must be unique.

The primary key can be defined when the table is built, or it can be defined later.

Columns that allow NULL values ​​cannot be used as unique identifiers.

Only one AUTO_INCREMENT column is allowed per table, and it must be indexed (such as by making it the primary key).

When inserting a row into a table with self-incrementing columns, you can replace the automatically generated value with a unique value that has not been used so far, and the subsequent increment is increased from the manually inserted value. (When I tested the auto-increment column can use repeated values, and the next value of the auto-increment column will be the next value of the maximum value in the table).

Get the value just inserted by AUTO_INCREMENT. This value is client-independent, and each client gets the self-increment value just operated by itself:

SELECT last_insert_id();

Default values ​​of columns:
Insert picture description here
Unlike most DBMSs, MySQL does not allow functions to be used as default values.

When you create a table, MySQL uses the user-specified engine or the default engine to create the table. When your SELECT statement or other database processing, the engine processes your request internally.

Several engines:
1. InnoDB: reliable transaction processing engine, does not support full text search.
2. MEMORY: The function is equivalent to MyISAM, the data is stored in the memory, the speed is very fast, suitable for temporary tables.
3. MyISAM: supports full text search, does not support transaction processing.

Each table can have different engines, but foreign keys cannot cross engines.

When the table stores data, the table should not be updated again, and should be considered during the design process of the table so that no major changes to the table will be made later.

Add a column to the table:

ALTER TABLE tableName
ADD 列定义语句(定义表时的列语句);

Delete a column:

ALTER TABLE tableName
DROP COLUMN columnName;

Add a foreign key to the table:

ALTER TABLE tableName1
ADD CONSTRAINT constraintName
FOREIGN KEY(field1) REFERENCES tableName2(field2)

The above SQL uses field1 as a foreign key, which requires a unique constraint on field2 in tableName2.

For the same table, multiple changes can be separated by commas.

A complete backup should be made before using ALTER TABEL. The changes to the database table cannot be undone. If you delete a column that should not be deleted, you may lose all data in the column.

For complex table structure changes, you can create a new table, and then copy the old table data to the new table through the INSERT SELECT statement, and use conversion functions and calculated fields if necessary.

Delete table:

DEOP TABLE tableName;

Rename the table:

RENAME TABLE oldTableName TO newTableName;

You can rename multiple tables separated by commas.

Guess you like

Origin blog.csdn.net/tus00000/article/details/111480072