database
Database
create database
1 | create database database_name; |
delete database
1 | drop database database_name; |
Table
create table
1 | CREATE TABLE table_name ( |
The column parameters specify the names of the columns of the table.
The datatype parameter specifies the type of data the column can hold (e.g. varchar, integer, date, etc.).
1 | CREATE TABLE Persons ( |
| PersonID | LastName | FirstName | Address | City |
|---|---|---|---|---|
alter table
The alter table statement can allows you to add, alter column and drop column for a table.
add
1
2ALTER TABLE table_name
ADD column_name datatype;alter column
1
2ALTER TABLE table_name
ALTER COLUMN column_name datatype;drop column
1
2ALTER TABLE table_name
DROP COLUMN column_name;
Constraints
1 | CREATE TABLE table_name ( |
As you see, the constraints are used to specify rules for the columns.
The following constraints are commonly used in SQL:
NOT NULL- Ensures that a column cannot have a NULL valueUNIQUE- Ensures that all values in a column are differentPRIMARY KEY- A combination of aNOT NULLandUNIQUE. Uniquely identifies each row in a table(However, you can have many
UNIQUEconstraints per table, but only onePRIMARY KEYconstraint per table.)FOREIGN KEY- Prevents actions that would destroy links between tablesCHECK- Ensures that the values in a column satisfies a specific conditionDEFAULT- Sets a default value for a column if no value is specifiedCREATE INDEX- Used to create and retrieve data from the database very quickly
UNIQUE
1 | CREATE TABLE Persons ( |
To name a UNIQUE constraint, and to define a UNIQUE constraint on multiple columns, use the following SQL syntax:
1 | CREATE TABLE Persons ( |
If want to add a unique constraint on the table that already exists, use the following syntax:
1 | use Teach; |
To drop a unique constraint, use the following syntax:
1 | use Teach; |
PRIMARY KEY
The PRIMARY KEY contain UNIQUE values, and also can not contain NULL value.
A table can have only one PRIMARY KEY, but a PRIMARY KEY can consist of multiple columns(field).
1 | CREATE TABLE Persons ( |
To allow naming of a PRIMARY KEY constraint, and for defining a PRIMARY KEY constraint on multiple columns, use the following SQL syntax:
1 | CREATE TABLE Persons ( |
To create a PRIMARY KEY constraint on the “ID” column when the table is already created, use the following SQL:
1 | use Teach; |
To allow naming of a PRIMARY KEY constraint, and for defining a PRIMARY KEY constraint on multiple columns, use the following SQL syntax:
1 | ALTER TABLE Persons |
To drop a PRIMARY KEY constraint, use the following SQL:
1 | ALTER TABLE Persons |
FOREIGN KEY
The relationship between the PRIMARY KEY and FOREIGN KEY is that the FOREIGN KEY refers to the PRIMARY KEY, so that the table with foreign key is a child table, and the table with primary key is parent table.
The function of the foreign key prevent invalid data from being inserted into the foreign column.
Just like this:
Persons Table
| PersonID | LastName | FirstName | Age |
|---|---|---|---|
| 1 | Hansen | Ola | 30 |
| 2 | Svendson | Tove | 23 |
| 3 | Pettersen | Kari | 20 |
Orders Table
| OrderID | OrderNumber | PersonID |
|---|---|---|
| 1 | 77895 | 3 |
| 2 | 44678 | 3 |
| 3 | 22456 | 2 |
| 4 | 24562 | 1 |
MySQL
1 | CREATE TABLE Orders ( |
SQL Server / Oracle / MS Access:
1 | CREATE TABLE Orders ( |
To allow naming of a FOREIGN KEY constraint, and for defining a FOREIGN KEY constraint on multiple columns, use the following SQL syntax:
1 | CREATE TABLE Orders ( |
To create a FOREIGN KEY constraint on the “PersonID” column when the “Orders” table is already created, use the following SQL:
1 | ALTER TABLE Orders |
To allow naming of a FOREIGN KEY constraint, and for defining a FOREIGN KEY constraint on multiple columns, use the following SQL syntax:
1 | ALTER TABLE Orders |
To drop a FOREIGN KEY constraint, use the following SQL:
1 | ALTER TABLE Orders |
Check
The check constraint is used to limit the value range that can be placed in the column.
MySQL
1 | CREATE TABLE Persons ( |
SQL Server / Oracle / MS Access:
1 | CREATE TABLE Persons ( |
To allow naming of a CHECK constraint, and for defining a CHECK constraint on multiple columns, use the following SQL syntax:
1 | CREATE TABLE Persons ( |
To create a CHECK constraint on the “Age” column when the table is already created, use the following SQL:
1 | ALTER TABLE Persons |
To allow naming of a CHECK constraint, and for defining a CHECK constraint on multiple columns, use the following SQL syntax:
1 | ALTER TABLE Persons |
To drop a CHECK constraint, use the following SQL:
1 | ALTER TABLE Persons |
Views
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.
1 | CREATE VIEW view_name AS |
The following SQL creates a view that shows all customers from Brazil:
1 | CREATE VIEW [Brazil Customers] AS |
1 | SELECT * FROM [Brazil Customers]; |
output
| CustomerName | ContactName |
|---|---|
| Comércio Mineiro | Pedro Afonso |
| Familia Arquibaldo | Aria Cruz |
| Gourmet Lanchonetes | André Fonseca |
| Hanari Carnes | Mario Pontes |
| Que Delícia | Bernardo Batista |
| Queen Cozinha | Lúcia Carvalho |
| Ricardo Adocicados | Janete Limeira |
| Tradição Hipermercados | Anabela Domingues |
| Wellington Importadora | Paula Parente |
