Easy To Learning
  • Login
  • Sign Up
  • Q&A
  • Home
  • Php Tutorial
  • MySql Tutorial
  • Object-Oriented Programming
  • Java Tutorial
  • HTML Tutorial
  • React JS Tutorials
  • Free Projects
  • Blogs & Articles
  • Others
    • About Us
    • Contact Us
MySQL Create Table using PHP
Easy To Learning

Education Needs Complete Solution

Online Ask Question Question And Answer
  1. Home
  2. MySql Tutorials
  3. MySQL Create Table using PHP

MySql Tutorial

Basic MySql Tutorials

  • MySql Table Content
  • MySQL Introduction
  • MySQL Constraints
  • MySQL Connection In PHP
  • MySQL Create Database In PHP
  • MySQL Create Table using PHP
  • MySQL Insert Using PHP
  • MySQL Update Using PHP
  • MySQL Delete Using PHP
  • MySQL Select Using PHP
  • MySQL Order By Using PHP
  • MySQL Where Clause Using PHP
  • MySQL Fetch Array Using PHP
  • MySQL Fetch Assoc Using PHP
  • MySQL Fetch Row Using PHP
  • MySQL Fetch Object Using PHP
  • MySQL Basic Queries

Basic MySqli Tutorials

  • MySQLi Connection In PHP
  • MySQLi Create Database In PHP
  • MySQLi Create Table using PHP
  • MySQLi Insert Using PHP
  • MySQLi Update Using PHP
  • MySQLi Delete Using PHP
  • MySQLi Select Using PHP
  • MySQLi Order By Using PHP
  • MySQLi Where Clause Using PHP
  • MySQLi Fetch Array Using PHP
  • MySQLi Fetch Assoc Using PHP
  • MySQLi Fetch Row Using PHP
  • MySQLi Fetch Object Using PHP
  • MySQLi Predefined Function In PHP

MySQL Create Table using PHP

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):

Syntax

create table users(
  id INT AUTO_INCREMENT,
  name VARCHAR(20) NOT NULL,  
  salary INT NOT NULL,
  primary key (id)
)

Mysql Table Detail

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:

  • NOT NULL - Each row must contain a value for that column, null values are not allowed
  • DEFAULT Value - Set a default value that is added when no other value is passed
  • UNSIGNED - Used for number types, limits the stored data to positive numbers and zero
  • AUTO_INCREMENT - MySQL automatically increases the value of the field by 1 each time a new record is added
  • PRIMARY KEY - Used to uniquely identify the rows in a table. The column with the PRIMARY KEY setting is often an ID number and is often used with AUTO_INCREMENT
<?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

 

Series: MySql Tutorials Hub
All Topics
Free Download Projects
Previous Chapter
Next Chapter

Ask a Question

Have a coding query or doubt? Ask here and get help from our community.

500 characters remaining
Submitting...
Free Download Projects
Join Telegram Group Join WhatsApp Group
Related Topics:
php table code mysql create table example php echo html table insert into table mysql mysql create table primary key how to create table in mysql command line mysql create table auto_increment mysql create table if not exists

Member Sign In

Forgot?
Signing in...

Don't have an account? Register here

Create Free Account

Register Captcha
Registering...

Already have an account? Sign In

Ask a Question

500 characters remaining
Submitting...

About Easy To Learning

Easy To Learning is a comprehensive programming learning portal offering free, practical, developer-driven tutorials for PHP, Java, Python, Spring, MySQL, CSS, Bootstrap, CakePHP, and more.

Explore community-contributed questions and answers, and download ready-to-run source code projects.

Quick Links

  • Home
  • Questions & Answers
  • Download Projects
  • Blog & Articles
  • Member Login

Community

  • Telegram Group
  • WhatsApp Community
  • Ask a Question
  • Join as Member

Connect With Us

Stay updated with our newest programming tutorials and free open source projects.

© 2026 Easy To Learning. All Rights Reserved.