SQL Constraints:: Example
SQL Constraints:: Example
Constraints are the rules enforced on data columns on table. These 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 database.
Constraints could be column level or table level. Column level constraints are applied only to one column, whereas
table level constraints are applied to the whole table.
Example:
For example, the following SQL creates a new table called CUSTOMERS and adds five columns, three of which,
ID and NAME and AGE, specify not to accept NULLs:
);
If CUSTOMERS table has already been created, then to add a NOT NULL constraint to SALARY column in Oracle
and MySQL, you would write a statement similar to the following:
TUTORIALS POINT
Simply Easy Learning
DEFAULT Constraint:
The DEFAULT constraint provides a default value to a column when the INSERT INTO statement does not provide
a specific value.
Example:
For example, the following SQL creates a new table called CUSTOMERS and adds five columns. Here, SALARY
column is set to 5000.00 by default, so in case INSERT INTO statement does not provide a value for this column.
then by default this column would be set to 5000.00.
);
If CUSTOMERS table has already been created, then to add a DFAULT constraint to SALARY column, you would
write a statement similar to the following:
UNIQUE Constraint:
The UNIQUE Constraint prevents two records from having identical values in a particular column. In the
CUSTOMERS table, for example, you might want to prevent two or more people from having identical age.
Example:
For example, the following SQL creates a new table called CUSTOMERS and adds five columns. Here, AGE
column is set to UNIQUE, so that you can not have two records with same age:
TUTORIALS POINT
Simply Easy Learning
NAME VARCHAR (20) NOT NULL,
);
If CUSTOMERS table has already been created, then to add a UNIQUE constraint to AGE column, you would write
a statement similar to the following:
You can also use following syntax, which supports naming the constraint in multiple columns as well:
If you are using MySQL, then you can use the following syntax:
PRIMARY Key:
A primary key is a field in a table which uniquely identifies each row/record in a database table. Primary keys must
contain unique values. A primary key column cannot have NULL values.
A table can have only one primary key, which may consist of single or multiple fields. When multiple fields are used
as a primary key, they are called a composite key.
If a table has a primary key defined on any field(s), then you can not have two records having the same value of
that field(s).
Note: You would use these concepts while creating database tables.
TUTORIALS POINT
Simply Easy Learning
CREATE TABLE CUSTOMERS(
);
To create a PRIMARY KEY constraint on the "ID" column when CUSTOMERS table already exists, 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).
For defining a PRIMARY KEY constraint on multiple columns, use the following SQL syntax:
);
To create a PRIMARY KEY constraint on the "ID" and "NAMES" columns when CUSTOMERS table already exists,
use the following SQL syntax:
TUTORIALS POINT
Simply Easy Learning
FOREIGN Key:
A foreign key is a key used to link two tables together. This is sometimes called a referencing key.
Foreign Key is a column or a combination of columns whose values match a Primary Key in a different table.
The relationship between 2 tables matches the Primary Key in one of the tables with a Foreign Key in the
second table.
If a table has a primary key defined on any field(s), then you can not have two records having the same value of
that field(s).
Example:
Consider the structure of the two tables as follows:
CUSTOMERS table:
CREATE TABLE CUSTOMERS(
);
ORDERS table:
CREATE TABLE ORDERS (
DATE DATETIME,
AMOUNT double,
);
If ORDERS table has already been created, and the foreign key has not yet been set, use the syntax for specifying
a foreign key by altering a table.
TUTORIALS POINT
Simply Easy Learning