[MySQL Getting Started Guide] Basic DDL operations on data tables

1. Create a data table

1. Grammar

CREATE TABLE table_name (
    field1 datatype,
    field2 datatype,
    ……
) character set 字符集 collate 校验规则 engine 存储引擎;

illustrate:

  • field represents the column name
  • datatype indicates the data type of the column
  • If no character set is specified, the character set of the database in which it is located shall prevail (principle of proximity)
  • If no verification rules are specified, the verification rules of the database where they are located shall prevail

Troubleshooting:

  1. The default character set and checksum?

    ​ When we create a database, if we do not specify a character set and a verification set, we will use the default one in the mysql configuration file; when we create a table, if we do not specify a character set and a verification set, it will be the same as the current database be consistent. This is a principle of proximity.

    ​ If there is no special requirement, it is not recommended to specify explicitly, and the default is fine.

  2. What is a storage engine?

    ​ The MySQL storage engine is a software component used to store and retrieve data. It defines how data is stored on disk, and how data is retrieved from disk. MySQL provides a variety of storage engines, the most commonly used of which are InnoDB and MyISAM.

    ​ The storage engine in MySQL is plug-in, we choose the most suitable storage engine according to our actual needs

    image-20230414090807497

2. Case

​ Note that the data table is created under the database, because when we create the data table, we need to specify a database first: ues db_name;. If you forget which database we are currently in, you can use the following command to check:select database();

image-20230414095558557image-20230414095442371

Second, view the data sheet

  • View all data tables under the current databaseshow tables;

    image-20230414095758063

  • view table propertiesdesc table_name

    image-20230414105225115

  • View the creation statement of the data tableshow create table table_name;

    image-20230414103955289

    ​ The display is not clear? It doesn't matter, you can \Greplace it ;. MySQL will output the query results in rows and columns.

    image-20230414104245070

3. Modify the data table

The following data tables exist, and we modify them (both belong to DDL operations)

image-20230414105402021

1. Add a new field

grammar:

alter table table_name add column datatype [DEFAULT val],  [add column datatype [DEFAULT expr]] ……

Suggestion: After the general data table is designed, it is not recommended to insert new fields. Especially if there is already data in the data table

  • Case 1: Add a row of new columns

    image-20230414110300238

  • Case 2: You can defaultset the default value by keyword

    image-20230414110932321

  • Case 3: Insert multiple columns at the same time.

    image-20230414120307776

2. Modify field properties

grammar:

ALTER TABLE tablename MODIfy columnname columndefinition,[MODIfy columnname columndefinition,]

case:

  • Modify field type

    image-20230414120820215

    Note: modifying the field will change the original field attributefull coverage. In the above case, the original comment information of the tele field is overwritten

  • modify the default

3. Delete field

grammar:

ALTER TABLE tablename DROP (column);

image-20230414124932133

4. Modify the data table name

grammar:

alter table tablename rename xxx

image-20230414125208907

5. Modify the field name

grammar:

alter table tablename change oldname newname + 字段属性

image-20230414125818845

When modifying the field name will alsocoverAll attributes of the original field

4. Delete the data table

grammar:

drop [TEMPORARY] temporary [if exists] tbl_name [, tbl_name] ...
  • The TEMPORARY keyword is used to indicate the deletion of a temporary table

Note: Be careful when deleting the table, the data in the data table and the Chuangjing command will be completely deleted

Guess you like

Origin blog.csdn.net/whc18858/article/details/130157201