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
-
Data types: INT, VARCHAR, TEXT, DATE, TIMESTAMP, etc.
-
Constraints:
-
PRIMARY KEYβ unique identifier -
UNIQUEβ prevents duplicate values -
NOT NULLβ column cannot be empty -
AUTO_INCREMENTβ automatically increments numeric field
-
-
Always check connection and query errors.
-
Tables must be created inside an existing database.
