SQL Statement
SQL Statement
Syntax
CREATE DATABASE databasename;
Example
CREATE DATABASE testDB;
Syntax
CREATE TABLE table_name (
column1 datatype,
column2 datatype,
column3 datatype,
....
);
Example
CREATE TABLE Persons (
PersonID int,
LastName varchar(255),
FirstName varchar(255),
Address varchar(255),
City varchar(255)
);
The ALTER TABLE statement is also used to add and drop various
constraints on an existing table.
SQL Constraints
SQL constraints are used to specify rules for data in a table.
Syntax
CREATE TABLE table_name (
column1 datatype constraint,
column2 datatype constraint,
column3 datatype constraint,
....
);
SQL Constraints
SQL constraints are used to specify rules for the data in a table.
Constraints are used to limit the type of data that can go into a
table. This ensures the accuracy and reliability of the data in the
table. If there is any violation between the constraint and the data
action, the action is aborted.
The NOT NULL constraint enforces a column to NOT accept NULL values.
The following SQL ensures that the "ID", "LastName", and "FirstName"
columns will NOT accept NULL values:
Example
CREATE TABLE Persons (
ID int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255) NOT NULL,
Age int
);
Both the UNIQUE and PRIMARY KEY constraints provide a guarantee for
uniqueness for a column or set of columns.
However, you can have many UNIQUE constraints per table, but only
one PRIMARY KEY constraint per table.
MySQL:
Both the UNIQUE and PRIMARY KEY constraints provide a guarantee for
uniqueness for a column or set of columns.
However, you can have many UNIQUE constraints per table, but only
one PRIMARY KEY constraint per table.
MySQL:
Primary keys must contain UNIQUE values, and cannot contain NULL
values.
A table can have only one primary key, which may consist of single
or multiple fields.
MySQL:
MySQL:
The table containing the foreign key is called the child table, and
the table containing the candidate key is called the referenced or
parent table.
"Persons" table:
The FOREIGN KEY constraint also prevents invalid data from being
inserted into the foreign key column, because it has to be one of
the values contained in the table it points to.
MySQL:
MySQL:
MySQL:
The default value will be added to all new records IF no other value
is specified.
MySQL:
Indexes are used to retrieve data from the database very fast. 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, only
create indexes on columns that will be frequently searched against.
MS Access:
Often this is the primary key field that we would like to be created
automatically every time a new record is inserted.
To let the AUTO_INCREMENT sequence start with another value, use the
following SQL statement:
Tip: To specify that the "ID" column should start at value 10 and
increment by 5, change it to IDENTITY(10,5).
To insert a new record into the "Persons" table, we will NOT have to
specify a value for the "ID" column (a unique value will be added
automatically):
Tip: To specify that the "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 "ID" column (a unique value will be added
automatically):
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 first way specifies both the column names and the values to be inserted:
Example
INSERT INTO Customers (CustomerName, ContactName, Address, City, PostalCode,
Country)
VALUES ('Cardinal', 'Tom B. Erichsen', 'Skagen 21', 'Stavanger', '4006', 'Norway');
Try it Yourself »
The selection from the "Customers" table will now look like this:
Did you notice that we did not insert any number into the CustomerID field?
The CustomerID column is an auto-increment field and will be generated automatically
when a new record is inserted into the table.
The following SQL statement will insert a new record, but only insert data in the
"CustomerName", "City", and "Country" columns (CustomerID will be updated
automatically):
Example
INSERT INTO Customers (CustomerName, City, Country)
VALUES ('Cardinal', 'Stavanger', 'Norway');
Try it Yourself »
The selection from the "Customers" table will now look like this:
We will have to use the IS NULL and IS NOT NULL operators instead.
IS NULL Syntax
SELECT column_names
FROM table_name
WHERE column_name IS NULL;
IS NOT NULL Syntax
SELECT column_names
FROM table_name
WHERE column_name IS NOT NULL;
Demo Database
Assume we have the following "Persons" table: