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 mysqli_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=mysqli_connect("localhost","root","","users") or die(mysqli_error());
// Create table
$sql="CREATE TABLE users(
id INT AUTO_INCREMENT,
name VARCHAR(20) NOT NULL,
salary INT NOT NULL,
primary key (id)
)";
if (mysqli_query($db,$sql)){
echo "Table user created successfully";
}else{
echo "Error creating database: " .mysqli_error($db);
}
?>
Output:
Table users created successfully
© 2024 Easy To Learning. All Rights Reserved | Design by Easy To Learning