PHP MySQL creation database and table create

Source: Internet
Author: User
Tags php mysql

Create a database

The CREATE DATABASE statement is used for creating databases in MySQL.

Grammar
CREATE DATABASE database_name

In order for PHP to execute the above statement, we must use the mysql_query () function. This function is used to send a query or command to a MySQL connection.

Example

In the following example, we created a database named "my_db":

<?Php$con= Mysql_connect ("localhost","Peter","abc123");if(!$con) {Die ('Could not connect:'. Mysql_error ()); }if(Mysql_query ("CREATE DATABASE my_db", $con)) {echo"Database created"; }Else{echo"Error Creating database:". Mysql_error (); }mysql_close ($con);?>

Create a table

Create TABLE is used for creating database tables in MySQL.

Grammar
CREATE TABLE table_name (column_name1 data_type,column_name2 data_type,column_name3 data_type,.......)

In order to execute this command, I must add the CREATE TABLE statement to the mysql_query () function.

Example

The following example shows how to create a table named "Persons", which has three columns. The column names are "FirstName", "LastName", and "age":

<?Php$con= Mysql_connect ("localhost","Peter","abc123");if(!$con) {Die ('Could not connect:'. Mysql_error ()); }//Create Databaseif(Mysql_query ("CREATE DATABASE my_db", $con)) {echo"Database created"; }Else{echo"Error Creating database:". Mysql_error (); }//Create table in my_db databasemysql_select_db ("my_db", $con); $sql="CREATE TABLE Persons(FirstName varchar ( the), LastName varchar ( the), ageint)";mysql_query ($sql, $con); Mysql_close ($con);?>

Important: before you create a table, you must first select the database. Select the database by using the mysql_select_db () function.

Note: When you create a database field with a varchar type, you must specify the maximum length of the field, for example: varchar (15).

MySQL Data Type

The following are the various types of MySQL data that can be used:

primary key and auto increment fields

Each table should have a primary key field.

The primary key is used to uniquely identify the rows in the table. Each primary key value must be unique in the table. Additionally, the primary key field cannot be empty, because the database engine needs a value to locate the record.

The primary key field is always indexed. There is no exception to this rule. You must index the primary key field so that the database engine can quickly locate the row that is given the key value.

The following example sets the PersonID field as the primary key field. The primary key field is usually the ID number, and the auto_increment setting is typically used. Auto_increment will increment the value of the field each time the new record is added. To ensure that the primary key field is not empty, we must add a NOT NULL setting to the field.

Example

"int not NULL auto_increment, PRIMARY KEY (PersonID), FirstName varchar (  ), LastName varchar (int)"; mysql_query ($sql, $con);

PHP MySQL creation database and table create

Related Article

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.