SQL CREATE DATABASE, CREATE TABLE, and ALTER TABLE





The CREATE DATABASE statement is used to create a new SQL database and has the following syntax:

CREATE DATABASE DatabaseName

The CREATE DATABASE implementation and syntax varies substantially between different RDBMS implementations.

The CREATE TABLE statement is used to create a new database table. Here is how a simple CREATE TABLE statement looks like:

CREATE TABLE TableName ( Column1 DataType, Column2 DataType, Column3 DataType, )

The DataType specified after each column name is a placeholder fro the real data type of the column.

The following CREATE TABLE statement creates the Users table we used in one of the first chapters:

CREATE TABLE Users ( FirstName CHAR(100), LastName CHAR(100), DateOfBirth DATE )

The CREATE TABLE statement above creates a table with 3 columns - FirstName of type CHAR with length of 100 characters, LastName of type CHAR with length of 100 characters and DateOfBirth of type DATE.

The ALTER TABLE statement is used to change a table definition by adding, modifying or dropping columns. Below you can see the syntax of an ALTER TABLE statement, which adds a new column to the table:

ALTER TABLE TableName ADD ColumnName DataType

If we want to delete the newly added ColumnName column we can do it with the following ALTER TABLE statement:

ALTER TABLE TableName DROP ColumnName

If your site has a huge amount of traffic, which results to a lot of queries to the database, try migrating into one of those virtual private servers that other web hosting providers offer.