Basic Operations of MySQL Data Tables

constraint

primary key constraint

  A primary key, also known as a primary key, is a combination of one or more columns in a table. The primary key constraint (Primary Key Constraint) requires that the data of the primary key column is unique and does not allow nulls. The primary key can uniquely identify a record in the table, can be combined with foreign keys to define the relationship between different data tables, and can speed up database query.
  There are two types of primary keys:

  • single field primary key

    Specify primary keys while defining columns: 字段名 数据类型 primary key
    Specify primary keys after all columns are defined:constraint <约束名> primary key [字段名]

  • Multi-field union primary key

    The syntax of the multi-field union primary key is as follows:primary key [字段1,字段2,...字段n]

E.g:primary key(id,name)

foreign key constraints

  A foreign key is used to establish a link between data in two tables, and it can be one or more columns. A table can have one or more foreign keys. 一个表的外键可以为空值,若不为空值,则每一个外键值必须等于另一个表中的主键的某个值。
  Foreign key: First of all, it is a field in the table, it may not be the primary key of this table, but corresponds to the primary key of another table. The role of foreign keys is to maintain the consistency and integrity of data.
  Primary table: For two associated tables, the table where the primary key in the associated field is located is the primary table.
  Slave table: For two associated tables, the table where the foreign key in the associated field is located is the slave table.
The syntax rules for creating foreign keys are as follows:
constraint <外键名> foreign key [字段1,字段2,....字段n] references <主表名> [主键列1,主键列2,...主键列n]
For example: constraint test foreign key(name) references table_1(id);
the foreign key of the slave table must be associated with the primary key of the master table, and the data types of the associated fields must be the same

Not Null Constraint

  A not-null constraint means that the value of a field cannot be null. For fields that use a not-null constraint, if the user does not specify a value when adding data, the database system will report an error.
The non-null constraint syntax rules are as follows:
字段名 数据类型 not null

unique constraint

  The uniqueness constraint requires the column to be unique, allowing nulls, but only one null value can appear. Unique constraints ensure that a column or columns do not have duplicate values.
The unique constraint syntax rules are as follows:

  • Specify the unique constraint directly after defining the column

    字段名 数据类型 unique

  • Specify a unique constraint after all columns are defined

    constraint <约束名> unique [字段名]
    E.g:constraint test unique(id)

uniqueThe difference between and primary key: a table can have multiple fields declared as unique, but only one primary keystatement; primary keycolumns declared as are not allowed to have null values, but uniquefields declared as allow the existence of null values.

default constraints

  Default constraint, which specifies the default value of a column. If there are many male employees, the gender can be set to "male" by default. If this field is not assigned a value when inserting a new record, the system will automatically assign a value of "male" to this field.
The syntax rules for default constraints are as follows:
字段名 数据类型 default
For example:sex varchar(25) default man ;

Set the property value of the table to automatically increase

  In database applications, it is often hoped that the system will automatically generate the primary key value of the field each time a new record is inserted. Bypass auto_increment, a table can only have one field using auto_incrementthe constraint, and that field must be part of the primary key. auto_incrementConstrained fields can be of any integer type.
The syntax rules for automatically increasing the attribute value of the setting table are as follows:
字段名 数据类型 auto_increment

The operation of the data table structure

Check

View table basic structure statement

  The describe/desc statement can view the field information of the table, including: field name, field data type, whether it is the primary key, whether there is a default value, etc.
The grammar rules are as follows:
describe 表名== desc 表名
Example:

mysql> describe user;
+------------------------+-----------------------------------+------+-----+-----------------------+-------+
| Field                  | Type                              | Null | Key | Default               | Extra |
+------------------------+-----------------------------------+------+-----+-----------------------+-------+
| Host                   | char(60)                          | NO   | PRI |                       |       |
| User                   | char(16)                          | NO   | PRI |                       |       |
| Password               | char(41)                          | NO   |     |                       |       |
| Select_priv            | enum('N','Y')                     | NO   |     | N                     |       |
| Insert_priv            | enum('N','Y')                     | NO   |     | N                     |       |
| Update_priv            | enum('N','Y')                     | NO   |     | N                     |       |
| Delete_priv            | enum('N','Y')                     | NO   |     | N                     |       |
| Create_priv            | enum('N','Y')                     | NO   |     | N                     |       |

View the detailed structure of the table

 show create tableThe statement can be used to display the create tablestatement when creating a table. The syntax is as follows:
show create table 表名\G;
Example:

mysql> show create table user\G;
*************************** 1. row ***************************
       Table: user
Create Table: CREATE TABLE `user` (
  `Host` char(60) COLLATE utf8_bin NOT NULL DEFAULT '',
  `User` char(16) COLLATE utf8_bin NOT NULL DEFAULT '',
  `Password` char(41) CHARACTER SET latin1 COLLATE latin1_bin NOT NULL DEFAULT '',
  `Select_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
  `Insert_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
  `Update_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
  `Delete_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
  `Create_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
  `Drop_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
  `Reload_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
  `Shutdown_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
  `Process_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
  `File_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
  `Grant_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
  `References_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
  `Index_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
  `Alter_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
  `Show_db_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
  `Super_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
  `Create_tmp_table_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',

Modify data sheet

Modify table name

The syntax rules are as follows:
alter table 旧表名 rename 新表名
  Modifying the table name does not affect the data structure when the table is created.

Modify field data type

The grammar rules are as follows:
alter table 表名 modify 字段名 [数据类型]
Example:
alter table test modify name varchar(30);

Modify field name

The grammar rules are as follows:
alter table 表名 change 旧字段名 新字段名 [新数据类型]
Example:
alter table test change name user_name varchar(30);

add field

The grammar rules are as follows:
alter table 表名 add 新字段名 [数据类型] [约束条件] [first|after 已存在字段名] ;
Example:
alter table test add id int(11) first;

Add an id field to the first column of the table.

alter table test add id int(11) after name;

Add an id field under the name column of the table.

delete field

The grammar rules are as follows:
alter table 表名 drop 字段名

Modify the arrangement of fields

The grammar rules are as follows:
alter table 表名 modify 字段1 [数据类型] first|after 字段2;
Example:
alter table test modify id varchar(12) first;

Adjust the id field to the first column in the table

alter table test modify id varchar(12) after name

After adjusting the id field to the name field in the table

Change the storage engine of the table

  You can use show enginesto see the storage engines supported by MySQL.

mysql> show engines;
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
| Engine             | Support | Comment                                                        | Transactions | XA   | Savepoints |
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
| FEDERATED          | NO      | Federated MySQL storage engine                                 | NULL         | NULL | NULL       |
| MRG_MYISAM         | YES     | Collection of identical MyISAM tables                          | NO           | NO   | NO         |
| MyISAM             | YES     | MyISAM storage engine                                          | NO           | NO   | NO         |
| BLACKHOLE          | YES     | /dev/null storage engine (anything you write to it disappears) | NO           | NO   | NO         |
| CSV                | YES     | CSV storage engine                                             | NO           | NO   | NO         |
| MEMORY             | YES     | Hash based, stored in memory, useful for temporary tables      | NO           | NO   | NO         |
| ARCHIVE            | YES     | Archive storage engine                                         | NO           | NO   | NO         |
| InnoDB             | DEFAULT | Supports transactions, row-level locking, and foreign keys     | YES          | YES  | YES        |
| PERFORMANCE_SCHEMA | YES     | Performance Schema                                             | NO           | NO   | NO         |
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
9 rows in set (0.00 sec)

The syntax for changing the storage engine of a table is as follows:
alter table 表名 engine=更改后的存储引擎名
Example:
alter table test engine=MyISAM;

Drop foreign key constraints on a table

The grammar rules are as follows:
alter table 表名 drop foreign key 外键约束名

delete data table

drop table [if exists] 表1,表2...,表n;
The parameter if existsis used to determine whether the deleted table exists before deletion. After adding the parameter, when the table is deleted, if the table does not exist, the SQL statement can be executed smoothly, but a warning will be issued. It should be noted that: when there is a foreign key association in the data table, deleting the main table directly will display the deletion failure, and you need to contact the foreign key constraints first.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325003590&siteId=291194637