Detailed description of the tutorial for creating a table in MySQL, detailed description of mysql

Source: Internet
Author: User

Detailed description of the tutorial for creating a table in MySQL, detailed description of mysql

Command requirements for creating a table:

  • Table Name
  • Table field name
  • Definition of each field

Syntax:

Below is a general SQL syntax to create a MySQL table:

CREATE TABLE table_name (column_name column_type);

Now, we will create the following tutorial database table.

tutorials_tbl(  tutorial_id INT NOT NULL AUTO_INCREMENT,  tutorial_title VARCHAR(100) NOT NULL,  tutorial_author VARCHAR(40) NOT NULL,  submission_date DATE,  PRIMARY KEY ( tutorial_id ));

The following items need to be described:

  • The field attribute not null is used because we do NOT want this field to be NULL. Therefore, if you try to create a record with a NULL value, MySQL will generate an error.
  • The field property AUTO_INCREMENT tells MySQL to continue increasing, and the id field of the next available number.
  • The primary key keyword is used to define a column as the primary key. You can use multiple columns separated by commas to define a primary key.

Create a table from a command prompt:

It is easy to create a mysql table from the MySQL> prompt. You will use the SQL command CREATE TABLE to CREATE a TABLE.
Example:

The following is an example of creating tutorials_tbl:

root@host# mysql -u root -pEnter password:*******mysql> use TUTORIALS;Database changedmysql> CREATE TABLE tutorials_tbl(  -> tutorial_id INT NOT NULL AUTO_INCREMENT,  -> tutorial_title VARCHAR(100) NOT NULL,  -> tutorial_author VARCHAR(40) NOT NULL,  -> submission_date DATE,  -> PRIMARY KEY ( tutorial_id )  -> );Query OK, 0 rows affected (0.16 sec)mysql>

Note: MySQL terminates the command until you end with a semicolon.
Use a PHP script to create a MySQL table:

To create any existing database in a new table, use the PHP function mysql_query (). The second parameter and the correct SQL command will be used to create a table.
Example:

The following is an example of using a PHP script to create a table:


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.