The CREATE DATABASE Statement
The CREATE DATABASE Statement
The data type specifies what type of data the column can hold. For a complete
reference of all the data types available in MS Access, MySQL, and SQL Server,
go to our complete Data Types reference.
CREATE TABLE Example
Now we want to create a table called "Persons" that contains five columns:
P_Id, LastName, FirstName, Address, and City.
The P_Id column is of type int and will hold a number. The LastName,
FirstName, Address, and City columns are of type varchar with a maximum
length of 255 characters.
The empty table can be filled with data with the INSERT INTO statement.
SQL Constraints
Constraints are used to limit the type of data that can go into a table.
Constraints can be specified when a table is created (with the CREATE TABLE
statement) or after the table is created (with the ALTER TABLE statement).
NOT NULL
UNIQUE
PRIMARY KEY
FOREIGN KEY
CHECK
DEFAULT
The NOT NULL constraint enforces a field to always contain a value. This means
that you cannot insert a new record, or update a record without adding a value
to this field.
The following SQL enforces the "P_Id" column and the "LastName" column to not accept NULL values:
The UNIQUE and PRIMARY KEY constraints both provide a guarantee for
uniqueness for a column or set of columns.
Note that you can have many UNIQUE constraints per table, but only one
PRIMARY KEY constraint per table.
SQL UNIQUE Constraint on CREATE TABLE
The following SQL creates a UNIQUE constraint on the "P_Id" column when the
"Persons" table is created:
MySQL:
MySQL:
Each table should have a primary key, and each table can have only ONE
primary key.
MySQL:
To allow naming of a PRIMARY KEY constraint, and for defining a PRIMARY KEY
constraint on multiple columns, use the following SQL syntax:
To allow naming of a PRIMARY KEY constraint, and for defining a PRIMARY KEY
constraint on multiple columns, use the following SQL syntax:
Note: If you use the ALTER TABLE statement to add a primary key, the primary
key column(s) must already have been declared to not contain NULL values
(when the table was first created).
MySQL:
Let's illustrate the foreign key with an example. Look at the following two
tables:
Note that the "P_Id" column in the "Orders" table points to the "P_Id" column in
the "Persons" table.
The "P_Id" column in the "Persons" table is the PRIMARY KEY in the "Persons"
table.
The "P_Id" column in the "Orders" table is a FOREIGN KEY in the "Orders" table.
The FOREIGN KEY constraint is used to prevent actions that would destroy links
between tables.
The FOREIGN KEY constraint also prevents that invalid data form being inserted
into the foreign key column, because it has to be one of the values contained in
the table it points to.
MySQL:
To allow naming of a FOREIGN KEY constraint, and for defining a FOREIGN KEY
constraint on multiple columns, use the following SQL syntax:
To allow naming of a FOREIGN KEY constraint, and for defining a FOREIGN KEY
constraint on multiple columns, use the following SQL syntax:
MySQL:
If you define a CHECK constraint on a single column it allows only certain values
for this column.
If you define a CHECK constraint on a table it can limit the values in certain
columns based on values in other columns in the row.
My SQL:
The default value will be added to all new records, if no other value is specified.
The DEFAULT constraint can also be used to insert system values, by using functions like GETDATE():
MySQL:
MySQL:
Indexes
An index can be created in a table to find data more quickly and efficiently.
The users cannot see the indexes, they are just used to speed up
searches/queries.
Note: Updating a table with indexes takes more time than updating a table
without (because the indexes also need an update). So you should only create
indexes on columns (and tables) that will be frequently searched against.
If you want to create an index on a combination of columns, you can list the column names within the
parentheses, separated by commas:
To delete a column in a table, use the following syntax (notice that some database systems don't allow
deleting a column):
To change the data type of a column in a table, use the following syntax:
Notice that the new column, "DateOfBirth", is of type date and is going to hold a
date. The data type specifies what type of data the column can hold. For a
complete reference of all the data types available in MS Access, MySQL, and
SQL Server, go to our complete Data Types reference.
Notice that the "DateOfBirth" column is now of type year and is going to hold a
year in a two-digit or four-digit format.
DROP COLUMN Example
Next, we want to delete the column named "DateOfBirth" in the "Persons" table.
To let the AUTO_INCREMENT sequence start with another value, use the following SQL statement:
To insert a new record into the "Persons" table, we will not have to specify a value for the "P_Id" column
(a unique value will be added automatically):
The SQL statement above would insert a new record into the "Persons" table.
The "P_Id" column would be assigned a unique value. The "FirstName" column
would be set to "Lars" and the "LastName" column would be set to "Monsen".
To specify that the "P_Id" column should start at value 10 and increment by 5,
change the identity to IDENTITY(10,5).
To insert a new record into the "Persons" table, we will not have to specify a value for the "P_Id" column
(a unique value will be added automatically):
The SQL statement above would insert a new record into the "Persons" table.
The "P_Id" column would be assigned a unique value. The "FirstName" column
would be set to "Lars" and the "LastName" column would be set to "Monsen".
To specify that the "P_Id" column should start at value 10 and increment by 5,
change the autoincrement to AUTOINCREMENT(10,5).
To insert a new record into the "Persons" table, we will not have to specify a value for the "P_Id" column
(a unique value will be added automatically):
INSERT INTO Persons (FirstName,LastName)
VALUES ('Lars','Monsen')
The SQL statement above would insert a new record into the "Persons" table.
The "P_Id" column would be assigned a unique value. The "FirstName" column
would be set to "Lars" and the "LastName" column would be set to "Monsen".
You will have to create an auto-increment field with the sequence object (this
object generates a number sequence).
The code above creates a sequence object called seq_person, that starts with 1
and will increment by 1. It will also cache up to 10 values for performance. The
cache option specifies how many sequence values will be stored in memory for
faster access.
To insert a new record into the "Persons" table, we will have to use the nextval function (this function
retrieves the next value from seq_person sequence):
The SQL statement above would insert a new record into the "Persons" table.
The "P_Id" column would be assigned the next number from the seq_person
sequence. The "FirstName" column would be set to "Lars" and the "LastName"
column would be set to "Monsen".
SQL CREATE VIEW Statement
In SQL, a view is a virtual table based on the result-set of an SQL statement.
A view contains rows and columns, just like a real table. The fields in a view are
fields from one or more real tables in the database.
You can add SQL functions, WHERE, and JOIN statements to a view and present
the data as if the data were coming from one single table.
Note: A view always shows up-to-date data! The database engine recreates the
data, using the view's SQL statement, every time a user queries a view.
The view "Current Product List" lists all active products (products that are not discontinued) from the
"Products" table. The view is created with the following SQL:
Another view in the Northwind sample database selects every product in the "Products" table with a
unit price higher than the average unit price:
CREATE VIEW [Products Above Average Price] AS
SELECT ProductName,UnitPrice
FROM Products
WHERE UnitPrice>(SELECT AVG(UnitPrice) FROM Products)
Another view in the Northwind database calculates the total sale for each category in 1997. Note that
this view selects its data from another view called "Product Sales for 1997":
We can also add a condition to the query. Now we want to see the total sale only for the category
"Beverages":