DBMS Constraint
DBMS Constraint
Create constraints
Constraints can be specified when the table is created with the CREATE TABLE statement,
or after the table is created with the ALTER TABLE statement.
Syntax
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.
Constraints can be column level or table level. Column level constraints apply to a
column, and table level constraints apply to the whole table. E.g. 2,3,4 & 5
Prevents rows that do not satisfy the CHECK criteria from being stored in the
table.
-
6. DEFAULT - Sets a default value for a column if no value is specified
1. UNIQUE Constraint
The UNIQUE constraint ensures that all values in a column are different.
Both the UNIQUE and PRIMARY KEY constraints provide a guarantee for
uniqueness for a column or set of columns.
A PRIMARY KEY constraint automatically has a UNIQUE constraint. However,
you can have many UNIQUE constraints per table, but only one PRIMARY KEY
constraint per table.
Primary Key constraints can’t be null. UNIQUE constraints may be null.
The following SQL creates a UNIQUE constraint on the "ID" column when the "Persons"
table is created:
The following SQL ensures that the "ID", "LastName", and "FirstName" columns will NOT
accept NULL values when the "Persons" table is created:
To create a NOT NULL constraint on the "Age" column when the "Persons" table is already
created, use the following SQL:
Example
The following SQL creates a PRIMARY KEY on the "ID" column when the "Persons" table
is created:
To create a PRIMARY KEY constraint on the "ID" column when the table is already created,
use the following SQL:
The FOREIGN KEY constraint is used to prevent actions that would destroy links
between tables.
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.
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
Notice that the "PersonID" column in the "Orders" table points to the "PersonID" column in
the "Persons" table.
The "PersonID" column in the "Persons" table is the PRIMARY KEY in the "Persons" table.
The "PersonID" column in the "Orders" table is a FOREIGN KEY in the "Orders" table.
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.
The following SQL creates a FOREIGN KEY on the "PersonID" column when the "Orders"
table is created:
To create a FOREIGN KEY constraint on the "PersonID" column when the "Orders" table is
already created, use the following SQL:
CHECK Constraint
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.
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.
CHECK on CREATE TABLE
The following SQL creates a CHECK constraint on the "Age" column when the "Persons"
table is created. The CHECK constraint ensures that the age of a person must be 18, or older:
To create a CHECK constraint on the "Age" column when the table is already created, use the
following SQL:
DEFAULT Constraint
The following SQL sets a DEFAULT value for the "City" column when the "Persons" table is
created:
When we insert a new record into the "Persons" table, we do NOT have to specify a value for
the "Personid" column (a unique value will be added automatically):
The SQL statement above would insert a new record into the "Persons" table. The "Personid"
column would be assigned a unique value automatically. The "FirstName" column would be
set to "Lars" and the "LastName" column would be set to "Monsen".
The most difficult part when working with dates is to be sure that the format of the date you are
trying to insert, matches the format of the date column in the database.
MySQL Date Data Types
MySQL comes with the following data types for storing a date or a date/time value in the
database:
Note: The date data type are set for a column when you create a new table in your database!
Orders Table
OrderId ProductName OrderDate
1 Geitost 2008-11-11
2 Camembert Pierrot 2008-11-09
3 Mozzarella di Giovanni 2008-11-11
4 Mascarpone Fabioli 2008-10-29
Now we want to select the records with an OrderDate of "2008-11-11" from the table above.
We use the following SELECT statement:
MySQL 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.
You can add SQL statements and functions to a view and present the data as if the data were
coming from one single table.
The following SQL creates a view that shows all customers from Brazil:
Example
###
SELECT * FROM [Brazil Customers];
The following SQL creates a view that selects every product in the "Products" table with a
price higher than the average price:
Example
Drop column
The following SQL deletes the "Email" column from the "Customers" table:
To change the data type of a column in a table, use the following syntax:
Column-Level Constraints
Column-level constraints are applied directly to a single column within a table. These
constraints define rules specific to the data in that column, and they are written right after the
column definition.
Table-level constraints are applied to the entire table and can reference multiple columns at
once. These constraints are typically used when the rule involves more than one column (e.g.,
a PRIMARY KEY or FOREIGN KEY constraint).
OrderID INT,
CustomerID INT,
OrderDate DATE,
);
MySQL:
o AUTO_INCREMENT is used for automatically incrementing primary key
values.
Example
name VARCHAR(100)
);
QL Server:
name VARCHAR(100)
);
3. String Concatenation
MySQL:
Example
SQL Server:
Example
4. Limit vs Top
MySQL:
o Uses LIMIT to restrict the number of rows returned.
SQL Server: