2. Basic operation of data table

Grammatical format hints in SQL sentences:

1. The content in square brackets ([]) is optional;

2. [, ...] means that the previous content can be repeated;

3. Curly brackets ({}) and vertical bars (|) indicate options, and only one of the options needs to be selected;

(1) Create a data table:

The basic syntax format for creating a data table in MySQL is as follows:

CREATE TABLE table_name (
    column1 datatype constraints,
    column2 datatype constraints,
    ...
    PRIMARY KEY (one_or_more_columns)
) ENGINE = engine_type;

Among them, CREATE TABLEis the keyword used to create the data table. table_nameis the name of the data table you want to create. column1, column2etc. are the column names in the table, datatypeare the data types to be stored in the column, and constraintsare the clauses used to set column-level constraints.

In this syntax, PRIMARY KEYit is used to define the primary key. In SQL, a primary key is a column or combination of columns that uniquely identifies each row of a table. The values ​​in these columns must be unique and not null in order to correctly distinguish each row in the table.

Finally, ENGINE = engine_typespecify the type of storage engine used by MySQL, such as InnoDB or MyISAM. The storage engine is the core component used by the database to store and manage data. Different engines provide different features and performance advantages.

Note: If you use a non-graphical tool to operate the data table, you should use the "USE database name" command to specify which database the operation is performed in before the operation, otherwise a "No database selected" error will be thrown.

eg: Create a department data table tb_dept in the database ems, the department data table contains the department number deptno, department name dname, location loc 

(2) View the data sheet:

After the data table is successfully created, you can view the data table through SQL statements to ensure that the data table is created successfully and the definition of the data table is correct. The following three SQL statements can view the data table:

2.1: Use the SHOW TABLES statement to view the data table:

SHOW TABLES [LIKE 'pattern' | WHERE expr];

2.2: Use SHOW CREATE TABLE to view the data table creation statement:

SHOW CREATE TABLE tbl_name;

2.3: Use the DESCRIBE statement to view the data table structure information:

DESCRIBE data table name;

 

 (3) Modify the data table:

If you want to make some structural modifications to the already created data table, such as: data table name, field type, field name, field arrangement position, adding or deleting fields, modifying or deleting table constraints, etc., you can choose to delete the original For some data tables, create a new data table, but if the original data table already has a large amount of data, choosing to delete it at this time will have a huge impact. In MySQL, it can be modified by the following SQL syntax:

ALTER TABLE tbl_name [alter_option [, alter_option] ...] [partition_options];

eg01: Change the name of the data table tb_dept in the database ems to dept

 eg02: Modify the character set of the data table dept to gbk

Modify the field name: The ALTER TABLE statement provides two ways to modify the field name, namely RENAME COLUMN and CHANGE. The main difference between these two methods is: RENAME COLUMN can only modify the field name, and the CHANGE clause can not only modify the field name, but also redefine the data type, constraint, and sorting position of the field. 

 eg: Change the field name loc in the department table dept to local_name (use RENAME COLUMN to modify)

Modify the data type of the field: it can be done through MODIFY and CHANGE in the ALTER TABLE statement, where MODIFY can only redefine the data type and arrangement position of the field, while CHANGE can not only redefine the data type and arrangement position of the field, but also Modify the field name. The effects of the two modifications are the same, but the syntax of MODIFY is relatively concise.

eg: Change the data type of the field dname in the department table dept from VARCHAR(14) to CHAR(16) (using the MODIFY syntax)

Modify the arrangement position of the field: the arrangement position of the field in the data table can be specified when creating the data table. If you want to modify the arrangement position of the field after the data table is created, the ALTER TABLE statement also provides MODIFY and CHANGE.

eg01: Change the position of the field local_name in the department table dept to the first field of the data table, and change the data type to CHAR(20) (use CHANGE to modify the arrangement position of the field)

eg02: Modify the field deptno in the department table dept to the field dname (use MODIFY to modify the arrangement position of the field)

 Add fields:

eg: Add an INT type field id in the first column of the data table dept

Delete field:

 eg: Delete the id field of the department table dept

(4) Delete the data table:

Deleting a data table refers to deleting an existing table in the database. When deleting the data table, the data stored in the data table will also be deleted. The following is the SQL statement to delete the data table:

DROP [TEMPORARY] TABLE [IF EXISTS] tbl_name [, tbl_name] ... [RESTRICT | CASCADE];

eg: delete the department table dept

Guess you like

Origin blog.csdn.net/weixin_61275790/article/details/131188873
Recommended