Home > Database > Mysql Tutorial > What is the command to create a table in mysql?

What is the command to create a table in mysql?

青灯夜游
Release: 2020-09-15 16:48:04
Original
22841 people have browsed it

The mysql create table command is [CREATE TABLE table_name (column_name column_type);], where the parameters include the table name, table field name, and each defined table field.

What is the command to create a table in mysql?

#How to create a MySQL data table? The following article will introduce to you how to create a data table through the command prompt. I hope it will be helpful to you.

The following information is required to create a MySQL data table:

● Table name

● Table field name

● Define each table field

Basic syntax

The following is the general SQL syntax for creating MySQL data tables:

CREATE TABLE table_name (column_name column_type);
Copy after login

Example: Create a table through the command prompt

MySQL data tables can be easily created through the mysql> command window. You can create a data table using the SQL statement CREATE TABLE.

1. First connect to the mysql server and enter the password

root@host# mysql -u root -p
Enter password:*******
Copy after login

2. Enter the database RUNOOB

use RUNOOB;
Copy after login

3. Create the table

mysql> CREATE TABLE runoob_tbl(
   -> runoob_id INT NOT NULL AUTO_INCREMENT,
   -> runoob_title VARCHAR(100) NOT NULL,
   -> runoob_author VARCHAR(40) NOT NULL,
   -> submission_date DATE,
   -> PRIMARY KEY ( runoob_id )
   -> )ENGINE=InnoDB DEFAULT CHARSET=utf8;
Copy after login

Output:

Query OK, 0 rows affected (0.16 sec)
Copy after login

indicates that the data table is created successfully.

Note: MySQL command terminator is semicolon;.

Note: -> is a newline identifier, do not copy it.

Example analysis:

●If you don’t want the field to be NULL, you can set the attribute of the field to NOT NULL. When operating the database, if the data entered in this field is NULL, an error will be reported.

● AUTO_INCREMENT is defined as a self-increasing attribute, generally used for primary keys, and the value will automatically increase by 1.

● The PRIMARY KEY keyword is used to define a column as the primary key. You can define a primary key using multiple columns, separated by commas.

● ENGINE sets the storage engine, CHARSET sets the encoding.

We can use the command to view the command line view table structure

What is the command to create a table in mysql?

The above is the detailed content of What is the command to create a table in mysql?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template