PHP MySQL Create Table

πŸ—„οΈ PHP MySQL Create Table Tutorial

After creating a database, the next step is to create tables to store your data. PHP can execute SQL CREATE TABLE statements using mysqli or PDO.


1️⃣ Using mysqli (Procedural)


 

Explanation:

  • id β†’ Primary key, auto-incremented

  • name β†’ VARCHAR, cannot be NULL

  • email β†’ UNIQUE constraint

  • age β†’ Integer

  • reg_date β†’ Timestamp, default is current time


2️⃣ Using mysqli (Object-Oriented)


 


3️⃣ Using PDO


 

βœ… PDO::exec() is used for queries that don’t return results like CREATE, UPDATE, DELETE.


4️⃣ Key Points

  1. Data types: INT, VARCHAR, TEXT, DATE, TIMESTAMP, etc.

  2. Constraints:

    • PRIMARY KEY β†’ unique identifier

    • UNIQUE β†’ prevents duplicate values

    • NOT NULL β†’ column cannot be empty

    • AUTO_INCREMENT β†’ automatically increments numeric field

  3. Always check connection and query errors.

  4. Tables must be created inside an existing database.

CodeCapsule

Sanjit Sinha β€” Web Developer | PHP β€’ Laravel β€’ CodeIgniter β€’ MySQL β€’ Bootstrap Founder, CodeCapsule β€” Student projects & practical coding guides. Email: info@codecapsule.in β€’ Website: CodeCapsule.in

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *