MySQL: Table related operations

Create a data table

Basic grammar

In MySQL, you can use the CREATE TABLEstatement to create the table. The syntax format is:

CREATE TABLE <表名> ([表定义选项])[表选项][分区选项];

Among them, [表定义选项]the format is:

<列名1> <类型1> [,] <列名n> <类型n>

CREATE TABLE There are many command syntaxes, which are mainly composed of create-definition, table-options and partition-options.

Here first described a simple example of the new table, and then highlights CREATE TABLEsome of the main points of grammar commands.

CREATE TABLE The main syntax and usage instructions of the statement are as follows:

  • CREATE TABLE: Used to create a table with a given name, you must have table CREATEpermissions.
  • <表名>: Specifies the name of the table to be created in the CREATE TABLEgiven after, must comply with the identifier naming rules. The table name is specified as db_name.tbl_name to create the table in a specific database. Whether or not there is a current database, it can be created in this way. When creating a table in the current database, you can omit the db-name. If you use quoted distinguished names, quote the database and table names separately. For example, 'mydb'. 'Mytbl' is legal, but 'mydb.mytbl' is not legal.
  • <表定义选项>: Table creation definition, consisting of column name (col_name), column definition (column_definition), and possible null value description, integrity constraint, or table index.

By default, the table is created in the current database. If the table already exists, there is no current database or the database does not exist, an error will occur .

Tip: Use CREATE TABLEwhen creating a table, you must specify the following information:

  • Name of the table to be created is not case sensitive, you can not use SQL language keywords, such as DROP, ALTER, INSERTand so on.
  • The name and data type of each column (field) in the data table. If you create multiple columns, separate them with commas.

Create a table in the specified database

The data table belongs to the database. Before creating the data table, you should use the statement " USE<数据库>" to specify the database in which the operation is performed. If the database is not selected, a No database selected error will be thrown.

View table structure

In MySQL, after creating a data table using SQL statements, you can view the structure definition to confirm whether the table definition is correct. In MySQL, the table structure can be used to view DESCRIBEand SHOW CREATE TABLEstatements.

DESCRIBE/DESC The statement can view the field information of the table, including the field name, field data type, whether it is the primary key, whether there is a default value, etc. The syntax rules are as follows:

DESCRIBE <表名>;

Or abbreviated as:

DESC <表名>;

Examples:

mysql> DESCRIBE tb_emp1;
+--------+-------------+------+-----+---------+-------+
| Field  | Type        | Null | Key | Default | Extra |
+--------+-------------+------+-----+---------+-------+
| id     | int(11)     | YES  |     | NULL    |       |
| name   | varchar(25) | YES  |     | NULL    |       |
| deptId | int(11)     | YES  |     | NULL    |       |
| salary | float        | YES  |     | NULL    |       |
+--------+-------------+------+-----+---------+-------+
4 rows in set (0.14 sec)

Among them, the meaning of each field is as follows:

  • Null: Indicates whether the column can store NULLvalues.
  • Key: Indicates whether the column has been indexed. PRI indicates that the column is part of the primary key of the table, UNI indicates that the column is part of the UNIQUE index, and MUL indicates that a given value in the column is allowed to appear multiple times.
  • Default: Indicates whether the column has a default value, and if so, what the value is.
  • Extra: Indicates additional information associated with a given column can be obtained, such as AUTO_INCREMENTand the like.

SHOW CREATE TABLEThe statement can be used to display the CREATE TABLEstatement when creating the table. The syntax is as follows:

SHOW CREATE TABLE <表名>\G;

Examples:

mysql> SHOW CREATE TABLE tb_emp1\G
*************************** 1. row ***************************
       Table: tb_emp1
Create Table: CREATE TABLE `tb_emp1` (
  `id` int(11) DEFAULT NULL,
  `name` varchar(25) DEFAULT NULL,
  `deptId` int(11) DEFAULT NULL,
  `salary` float DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=gb2312
1 row in set (0.03 sec)

Tip: Use the SHOW CREATE TABLE statement to view not only the detailed statement when creating the table, but also the storage engine and character encoding. If the "\ G" parameter is not added, the displayed result may be very confusing. After adding the "\ G" parameter, the displayed result may be more intuitive and easy to view.

Modify data table

In MySQL you can use the ALTER TABLEstatement to change the structure of the original tables, for example, add or delete columns, create or destroy indexes, change the type of existing columns, rename columns or lists.

Basic grammar

Modifying a table refers to modifying the structure of a data table that already exists in the database. MySQL use the ALTER TABLEstatement to modify tables. Common operations for modifying a table include modifying the table name, modifying the field data type or field name, adding and deleting fields, modifying the arrangement of fields, changing the storage engine of the table, and deleting foreign key constraints on the table.

The commonly used syntax format is as follows:

ALTER TABLE <表名> [修改选项]

[修改选项]The syntax is as follows:

{ ADD COLUMN <列名> <类型>
| CHANGE COLUMN <旧列名> <新列名> <新列类型>
| ALTER COLUMN <列名> { SET DEFAULT <默认值> | DROP DEFAULT }
| MODIFY COLUMN <列名> <类型>
| DROP COLUMN <列名>
| RENAME TO <新表名> }

Add field

As business changes, new fields may need to be added to existing tables. A complete field includes the field name, data type, and integrity constraints. The syntax for adding fields is as follows:

ALTER TABLE <表名> ADD <新字段名> <数据类型> [约束条件] [FIRST|AFTER 已存在的字段名]

The new field name is the name of the field that needs to be added; it FIRSTis an optional parameter whose role is to set the newly added field as the first field of the table; it AFTERis an optional parameter whose role is to add the newly added field to the specified Behind the existing field name.

Tip: " FIRSTor AFTERan existing field name" is used to specify the location of the new field in the table, if the SQL statement without these two parameters, the default will be the newly added fields to the data table the last column.

Modify the field data type

Modifying the data type of a field is to convert the data type of the field to another data type. The syntax rules for modifying field data types in MySQL are as follows:

ALTER TABLE <表名> MODIFY <字段名> <数据类型>

Among them, the table name refers to the name of the table where the field of the data type is to be modified, the field name refers to the field to be modified, and the data type refers to the new data type of the modified field.

Delete field

Deleting a field removes a field from the data table. The syntax is as follows:

ALTER TABLE <表名> DROP <字段名>

Among them, the field name refers to the name of the field that needs to be deleted from the table.

Modify the field name

The syntax rules for modifying table field names in MySQL are as follows:

ALTER TABLE <表名> CHANGE <旧字段名> <新字段名> <新数据类型>

Among them, the old field name refers to the field name before the modification; the new field name refers to the modified field name; the new data type refers to the modified data type. Same, but the data type cannot be empty.

CHANGEYou can also modify only the data type, and achieve MODIFYthe same effect, by the SQL statement "the new field name" and "old field names" is set to the same name, just change the "data type."

Tip: Because different types of data are not stored in the machine in the same way and length, modifying the data type may affect the existing data records in the data table, so when the data table already has data, do not easily modify the data type .

Modify the table name

By MySQL ALTER TABLEto implement the statement modifies the table name, syntax rules are as follows:

ALTER TABLE <旧表名> RENAME [TO] <新表名>

Among them, it TOis an optional parameter, whether used or not does not affect the result.

Tip: Users can use the DESC command to view the structure of the two modified tables when modifying the table name. Modifying the table name does not modify the structure of the table. Therefore, the structure of the table after the name modification and the table before the name modification are the same.

Delete data table

In the MySQL database, we can delete data tables that are no longer needed from the database.

While deleting the table, the structure of the table and all data in the table will be deleted, so it is best to back up before deleting the data table to avoid irreparable loss.

Let's take a look at the deletion method of the data table in the MySQL database.

Basic grammar

Use DROP TABLEstatement can delete one or more data tables, the following syntax:

DROP TABLE [IF EXISTS] 表名1 [ ,表名2, 表名3 ...]

The description of the syntax format is as follows:

  • 表名1, 表名2, 表名3 ...Indicates the name of the data table to be deleted. DROP TABLEMultiple tables can be deleted at the same time, as long as the table names are written in sequence, separated by commas.
  • IF EXISTSUsed to determine whether the table exists before deleting the data table. If not IF EXISTS, when the data table does not exist MySQL will prompt an error, interrupt the execution of SQL statements; plus IF EXISTSafter, SQL statements can be executed successfully when the data table does not exist, but will warn (warning).

Two points to note:

  • Users must have execute DROP TABLEpermissions command, otherwise the data table is not deleted.
  • When the table is deleted, the user's permissions on the table will not be automatically deleted.
Published 94 original articles · liked 0 · visits 722

Guess you like

Origin blog.csdn.net/qq_46578181/article/details/105420811
Recommended