MySQL operation table

1. Create a table

1. Syntax format for creating tables

grammar:

create table table_name (
	field1 datatype,
    field2 datatype,
    field3 databyte,
) character set 字符集 collate 校验规则 engine 存储引擎;

illustrate:

  • field represents the column name
  • datatype indicates the type of column
  • character set character set, if no character set is specified, the character set of the database where it is located shall prevail
  • collate verification rule, if no verification rule is specified, the verification rule of the database where it is located shall prevail

Among them, the various types represented by datatype are not the focus of this section. In the actual creation of the table, you can not specify the validation rules, character sets, and storage engines, and the database will have default character sets and validation rules.

2. The case of creating a table

Case number one:

create  table users (
     id int,
     name varchar(20) comment '用户名',
     password char(32) comment '用户的密码',
     birthday date comment '用户的生日'
) character set utf8 collate utf8_general_ci engine MyISAM;

image-20230719162823105

illustrate:

  1. comment is the meaning of the variable, which can be written or not.
  2. The set collate engine option can be written or not.
  3. Different storage engines have different files for creating tables. The storage engine of the users table is MyISAM, and there are three different files in the data directory, which are:
    • users.frm: table structure
    • users.MYD: table data
    • users.MYI: table index

Case two:

create  table user2 (
     id int,
     name varchar(20) comment '用户名',
     password char(32) comment '用户的密码',
     birthday date comment '用户的生日'
) charset=utf8 collate=utf8_general_ci engine=InnoDB;

image-20230719163743046

Through the phenomenon on the right, it is found that the table structures of Case 1 and Case 2 are different. users has three table structures, and user2 has two table structures. This is because the two use different storage engines. This is related to the subsequent index. However, it can be seen that different storage engines have different requirements for the number and requirements of stored files.

It will be introduced in detail later: MyIsam's MYD and MYI store data and indexes separately; InnoDB's ibd stores data and indexes together. In addition, it can be found that creating a table is similar to creating a database, and can also be added if not exists.

2. View the table structure

First determine which database it is in:

image-20230719165326317

  • See which tables are available:
show tables;

image-20230719170024948

  • View table details:
desc 表名

image-20230719170127874

image-20230719170916401

  • View details when creating a table:
show create table 表名;

image-20230719170320411

Use \Greplace ;to format and remove all unnecessary formats:

image-20230719170449535

This result is somewhat different from the result when we created the table ourselves, such as the final character set, validation rules, order of the storage engine, and case. Inside the sql server, the lexical syntax analysis will be performed on the sql command we issued, and our command encoding will be corrected to make it standardized.

3. Modify the table structure

In the actual development of the project, the structure of a certain table is often modified, such as field name, field size, field type, character set type of the table, storage engine of the table, and so on. We also have requirements, adding fields, removing fields, and so on. Then we need to modify the table.

Modify the table name: change the user1 table name to user

alter table user1 rename to user;

image-20230719173354447


Before the demo changes internal properties, some data needs to be inserted:

image-20230719173803678

1. Add fields

alter table user add image_path varchar(128) comment '这个是用户的头像路径' after birthday;

After inserting a new field, it has no effect on the data in the original table:

image-20230719182607082

Analyze the composition of this statement:

  • alter table table name: Indicates the table to be modified.

  • add: Indicates that the modification method is to add a field, followed by the corresponding field name image_path and field type varchar(128)

  • comment: Indicates the meaning of the field name, which is equivalent to a comment.

  • after: birthday after after means that the newly added field is after the birthday field.

Since there are two pieces of data, when adding a new field, the result will display: 2 row in set.

2. Modify fields

For example, if you want to change the length of the type attribute corresponding to name from 20 to 60:

alter table user modify name varchar(60);

image-20230719183420473

But it should be noted that our previous name has a corresponding explanation, that is, the text description after the comment, that is, the user's name, but after the modification at this time, it will no longer be displayed:

image-20230719183600021

This means that the new field information covers all the previous field information, rather than a single modification.

3. Delete field

First look at the previous table structure:

image-20230719184116626

If the password field is not wanted, it can be deleted through the following statement, that is, delete the password column:

alter table user drop password;

image-20230719184214383

4. Rename the table

To rename user to User, execute the following statement:

alter table user rename to User;

image-20230719184550102

Note: tocan be omitted. Then change the table name again by omitting to:

image-20230719184655273

5. Renaming of columns (fields) in tables

To rename the name field in the table to xingming, execute the following statement:

alter table users change name xingming varchar(60) DEFAULT NULL;

image-20230719185050010

Among them, attributes cannot be omitted.

4. Delete table

To delete the table users, execute the following statement:

drop table users;

image-20230719185611092

5. Summary operation table

What we are talking about in this section is the structure of the operation table, not the content of the operation table. Like inserting data insert and viewing data select, these are all operations on the contents of the table. The types of SQL language we learn include DDL, DCL, and DML. The operations of the database and its tables we have learned so far belong to DDL, because these operations belong to the structure of the defined table.

Then for 3. Modifying the table structure, the actual addition, modification, deletion, and renaming all have corresponding action signs, that is, the corresponding word control, and the grammatical structure is basically the same.

  1. Add field: add
  2. Modify field: modify
  3. Delete field: drop
  4. Table renaming: rename
  5. Renaming of columns in a table: change

Finally, it should be noted that in the future development work, it is best not to modify, rename, or delete the structure of the table, otherwise the operation at the language level will be very cumbersome, so be sure to determine the structure of the table and the corresponding table structure in advance. Field naming.

Guess you like

Origin blog.csdn.net/NEFUT/article/details/131821337