MySQL Create data table

Source: Internet
Author: User
Tags mysql create php script

MySQL Create data table

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

    • Table name
    • table field Name
    • Define each table field
Grammar

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

(column_name column_type); 

In the following example we will create a data table Tutorials_tbl in the tutorials database:

Tutorials_tbl(   tutorial_id INT not NULL auto_increment,   tutorial_title VARCHAR(100 ) not null, tutorial_author VARCHAR(+) not null, submission_date DATE,());               

Instance parsing:

    • If you do not want the fields to be null , you can set the field's property to not null, and you will get an error if the data entered in the field is null when you manipulate the database.
    • Auto_increment defines a property that is self-increasing, typically used for a primary key, and the value is automatically added to 1.
    • The PRIMARY key keyword is used to define the column as the primary key. You can use multiple columns to define a primary key, and the columns are separated by commas.
To create a table from the command prompt

The mysql> Command window makes it easy to create MySQL data tables. You can create a table by using the SQL statement.

Instance

The following is a tutorials_tbl instance of creating a data table:

[Email protected]# mysql-u Root-pEnterPassword:*******Mysql> UseTutorials;DatabaseChangedmysql>CREATE TABLE Tutorials_tbl( -tutorial_id INT not NULL auto_increment, -Tutorial_title VARCHAR(100)Not NULL, - Tutorial_author varchar (40 not null, ->, -> PRIMARY KEY Span class= "pun" > ( tutorial_id )  ->< Span class= "PLN" > ); query Ok,0 rows affected  (0.16 Secmysql>        

Note: The MySQL command terminator is a semicolon (;).

Create a data table using a PHP script

You can use the PHP mysql_query () function to create a data table for a database that already exists.

The function has two parameters, returns TRUE on successful execution, or FALSE.

Grammar
BOOL mysql_query( sql,);    
Parameters Description
Sql Necessary. Specifies the SQL query to send. Note: The query string should not end with a semicolon.
Connection Optional. Specifies the SQL connection identifier. If not specified, the previous open connection is used.
Instance

The following example uses PHP scripts to create a data table:

<title>Creating MySQL Tables</title><body><?Php$dbhost= ' localhost:3036 ';$dbuser= ' Root ';$dbpass= ' Rootpassword ';$conn=Mysql_connect($dbhost,$dbuser,$dbpass);If(!$conn){ Die(' Could not connect: ' .Mysql_error());}Echo' Connected successfully<br/> ';$sql= The CREATE TABLE tutorials_tbl (". "tutorial_id INT not NULL auto_increment,". "Tutorial_title VARCHAR (+) not NULL,". "Tutorial_author VARCHAR (+) not NULL,". "Submission_date date,". "PRIMARY KEY (tutorial_id));";mysql_select_db( ' Tutorials ' );$retval=Mysql_query($sql, $conn ); if (! $retval ) { die ( ' Could not create TABLE: ' 

MySQL Create data 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.