Article Directory
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:
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.
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
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();
Second, view the data sheet
-
View all data tables under the current database
show tables;
-
view table properties
desc table_name
-
View the creation statement of the data table
show create table table_name;
The display is not clear? It doesn't matter, you can
\G
replace it;
. MySQL will output the query results in rows and columns.
3. Modify the data table
The following data tables exist, and we modify them (both belong to DDL operations)
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
-
Case 2: You can
default
set the default value by keyword -
Case 3: Insert multiple columns at the same time.
2. Modify field properties
grammar:
ALTER TABLE tablename MODIfy columnname columndefinition,[MODIfy columnname columndefinition,]
case:
-
Modify field type
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);
4. Modify the data table name
grammar:
alter table tablename rename xxx
5. Modify the field name
grammar:
alter table tablename change oldname newname + 字段属性
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