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