Home  >  Article  >  Database  >  Create database tables using MySQL database (4)

Create database tables using MySQL database (4)

黄舟
黄舟Original
2016-12-27 17:21:041333browse

Create database table

Use the CREATE table statement to complete the creation of the table. The common form of CREATEtable:

CREATE table table name (column declaration);

ends with Take the creation of the students table as an example. The table will store the student number (id), name (name), gender (sex), age (age), and contact number (tel):

create table students
(
id int unsigned not null auto_increment primary key,
name char(8) not null,
sex char(4) not null,
age tinyint unsigned not null,
tel char (13) null default "-"
);

It may be easy to make mistakes in the command prompt for some long statements, so we can enter the statements through any text editor After saving the file as createtable.sql, execute the script through file redirection execution in the command prompt.

Open the command prompt, enter: mysql -D samp_db -u root -p < createtable.sql

(Tips: 1. If you connect to a remote host, please add the -h command; 2 If the createtable.sql file is not in the current working directory, you need to specify the full path of the file. )

Explanation of statements:

create table tablename(columns) is the command to create a database table, the name of the column. And the data type of the column will be completed in parentheses;

5 column contents are declared in the parentheses, id, name, sex, age, tel are the names of each column, followed by the data type description, column Separate the column description with a comma (,);

Introduce with the line "id int unsigned not null auto_increment primary key":

"id" is the name of the column;

"int" specifies the type of the column as int (the value range is -8388608 to 8388607). Later we use "unsigned" to modify it, indicating that the type is unsigned. At this time, the column The value range is 0 to 16777215;

"not null" indicates that the value of this column cannot be empty and must be filled in. If this attribute is not specified, it can be empty by default;

"auto_increment " needs to be used in integer columns. Its function is that if the column is NULL when inserting data, MySQL will automatically generate a unique identifier value larger than the existing value. There can be only one such value in each table and the column must be an index column.

"primary key" means that this column is the primary key of the table. The value of this column must be unique, and MySQL will automatically index this column.

The char(8) below indicates that the stored character length is 8, the value range of tinyint is -127 to 128, and the default attribute specifies the default value when the column value is empty.

Tips: 1. Use the show tables; command to view the names of the created tables; 2. Use the describe table name; command to view the details of the created tables.

The above is the content of creating database tables using MySQL database (4). For more related content, please pay attention to the PHP Chinese website (www.php.cn)!


Statement:
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