PHP MySQL Create and Drop Tables
Use the query
function to get PHP to execute the SQL statements:
- the
DROP TABLE
statement, used to remove a database table, and
- the
CREATE TABLE
statement, used to create a table in MySQL.
query
is used to send a query or command to a MySQL connection.
A database must be selected before a table can be dropped or created.
The database is selected with the mysqli
function with one more parameter.
The following example drops and creates a table using the following SQL commands:
DROP TABLE CDs;
CREATE TABLE CDs (
ASIN CHAR(10) PRIMARY KEY,
Title VARCHAR(32) NOT NULL,
price REAL CHECK( price > 0.0 ) );
|
When you create a database field of type
VARCHAR
, you must specify the maximum length of the field, e.g.
VARCHAR(15)
.
The
CREATE TABLE
statement is complicated.
For further studies,
- its syntax can be found from here and
- some example code can be found from here.