Database
Database
Drop DB
The drop database statement is used to drop an existing SQL database.
Syntax:
DROP DATABASE databasename;
Create Table
The create table statement is used to create a new table in a database.
Syntax
CREATE TABLE table_name (
column1 datatype,
column2 datatype,
column3 datatype,
…
);
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.).
Drop table
The drop table statement is used to drop an existing table in a database.
Syntax:
DROP TABLE table_name;
Alter Table
The alter table statement is used to add, delete, or modify columns in an existing table. It’s also
used to add and drop various constraints on an existing table.
Not Null
By default, a column can hold null values. The not null constraint enforces a column to not accept null
values. This enforces a field to always contain a value, which means that you cannot insert a new record,
or update a record without adding a value to this field.
Example:
CREATE TABLE Persons (
ID int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255) NOT NULL,
Age int
);
Unique
The unique constraint ensures that all values in a column are different. You can have many
unique constraints per table, but only one Primary Key constraint per table.
Example on MySQL:
CREATE TABLE Persons (
ID int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Age int,
UNIQUE (ID)
);
Primary key
The primary key constraint uniquely identifies each record in a table.
Primary keys must contain unique values, and cannot contain null values. A table can have only one
primary key; and in the table, this primary key can consist of single or multiple columns (fields).
Example on MySQL:
CREATE TABLE Persons (
ID int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Age int,
PRIMARY KEY (ID)
);
Foreign key
The foreign key constraint prevents invalid data from being inserted into the foreign key column,
because it has to be one of the values contained in the parent table. A foreign key is a field (or
collection of fields) in one table, that refers to the primary key in another table.
The table with the foreign key is called the child table, and the table with the primary key is called
the referenced or parent table.
Example on MySQL:
CREATE TABLE Orders (
OrderID int NOT NULL,
OrderNumber int NOT NULL,
PersonID int,
PRIMARY KEY (OrderID),
FOREIGN KEY (PersonID) REFERENCES Persons(PersonID)
);
Check
The check constraint is used to limit the value range that can be placed in a column.
If you define a check constraint on a column it will allow only certain values for this column.
Example on MySQL:
CREATE TABLE Persons (
ID int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Age int,
CHECK (Age>=18)
);
Default
The default constraint is used to set a default value for a column.
The default value will be added to all new records, if no other value is specified.
Example on MySQL
CREATE TABLE Persons (
ID int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Age int,
City varchar(255) DEFAULT 'Sandnes'
);
Auto Increment
Auto-increment allows a unique number to be generated automatically when a new record is
inserted into a table.
Often this is the primary key field that we would like to be created automatically every time a new
record is inserted.
Select
Select distinct
Where
In
Not in
Between
#
Like