Home > Database > Mysql Tutorial > What are the mysql add statements?

What are the mysql add statements?

青灯夜游
Release: 2022-06-21 14:58:22
Original
11469 people have browsed it

Add statements include: 1. CREATE DATABASE statement, used to add a database, the syntax "CREATE DATABASE database name;"; 2. CREATE TABLE statement, used to add a data table, the syntax "CREATE TABLE table name (column name type);"; 3. ALTER TABLE statement, which can add fields to the data table, the syntax is "ALTER TABLE table name ADD field name type;"; 4. INSERT statement, which can add data to fields.

What are the mysql add statements?

The operating environment of this tutorial: windows7 system, mysql8 version, Dell G3 computer.

1. MySQL adds a database (CREATE DATABASE statement)

In MySQL, You can use the CREATE DATABASE statement to create a database. The basic syntax format is as follows:

CREATE DATABASE [IF NOT EXISTS] 数据库名
[CHARACTER SET 字符集名] 
[COLLATE 校对规则名];
Copy after login
  • IF NOT EXISTS: Make a judgment before creating the database. The operation can only be performed if the database does not currently exist. This option can be used to avoid duplicate creation errors when the database already exists.

  • CHARACTER SET: Specifies the character set of the database. The purpose of specifying the character set is to avoid garbled data stored in the database. If you do not specify a character set when creating the database, the system's default character set is used.

  • COLLATE: Specifies the default collation rule for the character set.

MySQL's character set (CHARACTER) and collation rules (COLLATION) are two different concepts. Character sets are used to define how MySQL stores strings, and collation rules define how to compare strings. We will explain MySQL's character set and collation rules separately later.

Example: Create a database named test_db

CREATE DATABASE test_db;
Copy after login

What are the mysql add statements?

View or display the database

SHOW DATABASES;
Copy after login

What are the mysql add statements?

2. MySQL adds a data table (CREATE TABLE statement)

In MySQL, you can use the CREATE TABLE statement to create a table. The syntax format is:

CREATE TABLE <表名> ([表定义选项])[表选项][分区选项]);
Copy after login

Among them, the format of [Table Definition Options] is:

<列名1> <类型1> [,…] <列名n> <类型n>
Copy after login

The CREATE TABLE command syntax is more, which mainly consists of table creation definition (create-definition), Composed of table options (table-options) and partition options (partition-options).

Tip: When using CREATE TABLE to create a table, you must specify the following information:

  • The name of the table to be created is not case-sensitive and cannot use SQL language Keywords in , such as DROP, ALTER, INSERT, etc.

  • The name and data type of each column (field) in the data table. If you create multiple columns, separate them with commas.

Example: Create a table in the specified database

Note: The data table belongs to the database. Before creating the data table, you should Use the statement "USE" to specify which database the operation is to be performed in. If no database is selected, a No database selected error will be thrown.

Select the database test_db to create the table and create the tb_emp1 data table:

What are the mysql add statements?

CREATE TABLE tb_emp1
(
id INT(11),
name VARCHAR(25),
deptId INT(11),
salary FLOAT
);
Copy after login

What are the mysql add statements?

After the statement is executed, it is created For a data table named tb_emp1, use the SHOW TABLES; statement to check whether the data table is created successfully

What are the mysql add statements?

##3. MySQL data Add fields to the table (ALTER TABLE statement)

You can use the ALTER TABLE statement in MySQL to change the structure of the original table, such as adding or deleting columns, changing the original column type, and re- Name columns or tables etc.

A complete field includes field name, data type and constraints. The syntax format for adding fields in MySQL is as follows:

ALTER TABLE <表名> ADD <新字段名><数据类型>[约束条件];
Copy after login

The syntax format is explained as follows: