PHP CREATE Table
In PHP, you can create a database table using the CREATE TABLE
statement. This statement is used to create a new table in a database. In this page, you will learn how to create a table in PHP.
Syntax
Here is the basic syntax of the CREATE TABLE
statement:
CREATE TABLE table_name (
column1 datatype,
column2 datatype,
column3 datatype,
....
);
table_name
: The name of the table you want to create.column1
,column2
,column3
, etc.: The columns that should be included in the table.datatype
: The data type of the columns.
Example
Here is an example that demonstrates how to create a table named employees
with columns id
, name
, email
, and salary
:
<?php
// establish database connection
$con = mysqli_connect("localhost", "user", "password");
// create table
mysqli_query($con, "CREATE TABLE employees (
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(30) NOT NULL,
email VARCHAR(50),
salary DECIMAL(10,2)
)");
?>
Explanation
In the example above, we first connect to the database using the mysqli_connect()
function. Then we use the CREATE TABLE
statement to create a new table named employees
. The table has four columns:
id
: An auto-incrementing integer field that serves as the table's primary key.name
: A string field that cannot be null.email
: A string field that can be null.salary
: A decimal field with two decimal places.
Use
The CREATE TABLE
statement is used to create a new table in a database. Tables are used to keep track of structured data, like user information and product inventory.
Important Points
- The
CREATE TABLE
statement is used to create a new table in a database. - The syntax of the
CREATE TABLE
statement includes the table name and the columns you want to include. - Each column in the
CREATE TABLE
statement needs to have a datatype. - You can specify additional constraints for each column in the table, such as whether it can be null or whether it is a primary key.
- The table name and column names should not contain spaces or special characters.
Summary
In PHP, you can create a database table using the CREATE TABLE
statement. This statement is used to create a new table in a database. The syntax of the CREATE TABLE
statement includes the table name and the columns you want to include. You can specify additional constraints for each column in the table, such as whether it can be null or whether it is a primary key. Tables are used to keep track of structured data, like user information and product inventory. The table name and column names should not contain spaces or special characters.