The CREATE TABLE statement is used to create a table, while creating a table we can define several constraints such as primary key, unique key,auto_increment, etc. We must add the CREATE TABLE statement to the mysql_query() function to execute the command.
Ex (Create Table):
create table users(
id INT AUTO_INCREMENT,
name VARCHAR(20) NOT NULL,
salary INT NOT NULL,
primary key (id)
)
The data type specifies what type of data the column can hold. For a complete reference of all the available data types, go to our Data Types reference.
After the data type, you can specify other optional attributes for each column:
<?php
$db=mysql_connect("localhost","root","") or die(mysql_error());
// select database
mysql_select_db("users",$db);
// Create table
$sql="CREATE TABLE users(
id INT AUTO_INCREMENT,
name VARCHAR(20) NOT NULL,
salary INT NOT NULL,
primary key (id)
)";
if (mysql_query($sql)){
echo "Table user created successfully";
}else{
echo "Error creating database: " .mysql_error();
}
?>
Output:
Table users created successfully
© 2024 Easy To Learning. All Rights Reserved | Design by Easy To Learning