Foreword:
In the previous issue, we explained the basic operations of the library in mysql accordingly. In this issue, I will explain to you the basic operations of tables in mysql.
Table of contents
(1) Create a table
To create a MySQL table, you need to use CREATE TABLE
the statement, and specify the name of the table and column names, data types and constraints.
- The following is an example MySQL table creation statement:
CREATE TABLE table_name (
field1 datatype,
field2 datatype,
field3 datatype
) character set 字符集 collate 校验规则 engine 存储引擎;
- 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
In the syntax above, you need to 表名
replace <name> with the actual name of the table you want to create. Then, specify the name, data type, and constraints for each column separated by commas.
For example, to create a user1
table named with four columns, , password, and birthday, you can use the following statement : id
name
【illustrate】
This SQL statement is used to create a user1
table named . Each line is explained below:
create table if not exists user1
: Creates auser1
table named if it does not exist.(id int, name varchar(20) comment '用户名', password char(32) comment '用户密码', birthday date comment '生日')
: defines the columns and data types of the table. The table contains four columns, which areid
(integer type),name
(string type with a maximum length of 20, with comments'用户名'
),password
(character type with a length of 32, with comments'用户密码'
) andbirthday
(date type, with comments'生日'
).character set utf8 collate utf8_general_ci
: Set the character set to UTF-8, and set the collation toutf8_general_ci
. This supports storing and processing Chinese characters.engine MyIsam
: Specifies to use the MyISAM storage engine. MyISAM is a storage engine of MySQL, which is suitable for frequent reading and less writing.
In short, the above SQL statement creates a user1
table named , which contains four columns id
, name
, password
and birthday
, and each column has its corresponding data type and comment. The charset is set to UTF-8 and the collation is utf8_general_ci
. The table uses the MyISAM storage engine for data storage. If the table user1
already exists, no action will be taken.
- Let's go to the corresponding directory to view the data created in the current file, as follows:
Immediately afterwards, I am creating a new table. The style is a little different from the above-mentioned creation. You can choose the corresponding creation method according to your favorite. details as follows:
【illustrate】
This SQL statement is used to create a user2
table named . Each line is explained below:
- This creation is exactly the same except for the final engine, but the creation method is different;
engine=InnoDB
: Specifies to use the InnoDB storage engine. InnoDB is a transactional storage engine for MySQL, suitable for scenarios that require transaction support and data integrity.
- When we checked the created data under the corresponding file, we found a difference (the data created under the two engines are different). details as follows:
- 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
(2) Check the table structure
In MySQL, you can use the DESCRIBE
or SHOW CREATE TABLE
statement to view the structure of a table.
1. DESCRIBE
Method
- Use
DESCRIBE
statement:
DESCRIBE table_name;
Replace table_name
with the name of the table whose structure you want to see. After executing this statement, a result set containing table structures such as column names, data types, and key information will be returned.
- For example, currently I want to view the relevant information of the previously created table, we can do this:
2、SHOW CREATE TABLE方式
- Use
SHOW CREATE TABLE
statement:
Again, table_name
replace with the name of the table whose structure you want to see. After the statement is executed, a result set will be returned that contains the SQL statement to create the table, including the complete table structure definition.
【Compare】
- Both of these statements can be used to view the structure of the table, but
SHOW CREATE TABLE
it provides a SQL statement containing the complete table structure, which is more detailed and comprehensive; - Instead
DESCRIBE
, it provides concise column information. According to specific needs, select the appropriate statement to use.
(3) Amendment table
ALTER TABLE tablename ADD (column datatype [DEFAULT expr][,columndatatype]...);ALTER TABLE tablename MODIfy (column datatype [DEFAULT expr][,columndatatype]...);ALTER TABLE tablename DROP (column);
In MySQL, you can use the ALTER TABLE statement to modify the structure and attributes of a table. Here are some examples of common table operations:
- Modify the table name to Hello
- Add two records to the Hello table
- Add a field to the Hello table, assuming it is used to save the image path
【illustrate】
This ALTER TABLE statement is used to add a column named image_path in the table named Hello, the data type of this column is VARCHAR(100), and the comment is 'photo path'. The new column will be inserted after the existing birthday column.
The specific explanation is as follows:
ALTER TABLE Hello
: Indicates that the table to be modified is the Hello table.ADD image_path varchar(100)
: Indicates that a column named image_path is to be added to the Hello table, and the data type is VARCHAR(100). VARCHAR(100) means that the column can store a string with a maximum length of 100.COMMENT '照片路径'
: Indicates to add a comment to a new column, and the content of the comment is 'photo path'.AFTER birthday
: Indicates that the new column will be inserted after the existing birthday column. This means that the image_path column will be the next column after the birthday column.- And after inserting the new field, it has no effect on the data in the original table
By executing this ALTER TABLE statement, a column named image_path that can store a string with a maximum length of 100 and has the annotation 'photo path' will be successfully added to the Hello table.
- Modify the data type of the column ( modify the name and change its length to 60 ) :
We can also find a difference before and after modification:
- Delete column:
Note : Be careful when deleting a field, as the deleted field and its corresponding column data are gone
- Modify column names:
- drop table :
Syntax format : DROP [TEMPORARY] TABLE [IF EXISTS] tbl_name [, tbl_name] ...
Summarize
The above is all about the basic operations of tables in mysql. Thank you for watching and supporting! ! !