I. Create Table Using Constraints What Is Constraint?
I. Create Table Using Constraints What Is Constraint?
What is constraint?
- Rule governs how database data is inserted or manipulated.
- Rule enforced on data columns on table.
- 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.
- If there is any violation between the constraint and the data action, the action is aborted by
the constraint.
Add constraints on one or more table columns using the following SQL commands:
CREATE TABLE—Add a constraint on one or more columns.
ALTER TABLE—Add or drop a constraint on one or more columns.
There are two syntax definitions you can use to add or change a constraint:
E.g., oracle
2) Table-Constraint—Use this syntax when you add a constraint after a column definition
in a CREATE TABLE statement, or when you add, alter, or drop a constraint on a column
using ALTER TABLE.
E.g., oracle
CREATE TABLE employee (
e_id number(4),
fname varchar2(20),
lname varchar2(20),
dob date,
salary number(6,2),
1
PRIMARY KEY (e_id) ); /* This is called unnamed constraint. */
ALTER TABLE Persons DROP e_id; /* Drop Unnamed constraint column */
Note:
Unnamed Constraints: by default constraint name will be defined.
Named Constraints: Constraint can be drop by its name.
2
1. NOT NULL Constraint
By default, a table column can hold NULL values. The NOT NULL constraint enforces a
column to NOT accept NULL values. The NOT NULL constraint enforces a field to always
contain a value.
2. UNIQUE Constraint
The UNIQUE constraint uniquely identifies each record in a database table. Unique
constraint columns can contain NULL values. Unique constraints columns can be modified or
updated. Unlike PRIMARY KEY, UNIQUE constraints cannot be used to define FOREIGN
KEY.
The UNIQUE and PRIMARY KEY constraints both provide a guarantee for uniqueness for a
column or set of columns. A PRIMARY KEY constraint automatically has a UNIQUE
constraint defined on it. Note that you can have many UNIQUE constraints per table, but
only one PRIMARY KEY constraint per table.
CREATE TABLE Persons
(
P_Id int,
LastName varchar(255),
FirstName varchar(255),
Address varchar(255),
City varchar(255),
UNIQUE (P_Id)
)
The PRIMARY KEY constraint uniquely identifies each record in a database table.
Primary keys must contain UNIQUE values.
A primary key column cannot contain NULL values.
3
Most tables should have a primary key, and each table can have only ONE primary key.
CREATE TABLE PPersons
(
P_Id int NOT NULL PRIMARY KEY,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Address varchar(255),
City varchar(255)
);
Or
5. 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 single column it allows 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.
4
City varchar2(255),
CHECK (P_Id>0)
);
6. DEFAULT Constraint
The DEFAULT constraint is used to insert a default value into a column. The default value
will be added to all new records, if no other value is specified.
CREATE TABLE CityPersons
(
P_Id int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Address varchar(255),
City varchar(255) DEFAULT 'Yangon'
)
- Like tables views must be uniquely named. (They cannot be named with the name of any
other table of views.)
- Views cannot be indexed, nor can they have triggers or default values associated with
them.)
- You can present different views of the same data according to their particular criteria.
- To create views, you must have security access. This is usually granted by the database
administrator.
6
1)
2)
CREATE VIEW dept_view AS SELECT * FROM dept;
3)
CREATE VIEW emp_det AS
SELECT e.empno, e.ename, e.sal, e.deptno, d.dname,d.loc
FROM emp e, dept d
WHERE e.deptno = d.deptno;
/* Creating View */
CREATE VIEW myview AS
SELECT empno EMPLOYEE_NO, ename NAME, sal SALARY, job JOB
FROM emp
WHERE deptno=20;
/* Modifying View */
CREATE OR REPLACE VIEW myview
AS SELECT empno EMPLOYEE_NO, ename NAME, sal SALARY
FROM emp
WHERE deptno=10;
/* Removing a view */
DROP VIEW view;
7
2. To show tables of using database.
SHOW CREATE TABLE persons; /* MySQL getting create table statement of “person” table*/
*Note: The DUAL table is a special one-row, one-column table present by default in Oracle and other
database installations. In Oracle, the table has a single VARCHAR2(1) column called DUMMY that
has a value of 'X'. It is suitable for use in selecting a pseudo column such as SYSDATE or USER.
https://en.wikipedia.org/wiki/DUAL_table
Refs:
[1] Sams Teach yourself SQL in 10 mins, Fourth Edition, Copyright@ 2013 by Pearson
Education, Inc.
https://my.vertica.com/docs/7.1.x/HTML/index.htm#Authoring/AdministratorsGuide/Const
raints/AddingConstraints.htm
http://www.w3schools.com/sql/sql_constraints.asp
http://www.tutorialspoint.com/sql/sql-constraints.htm
http://www.w3resource.com/sql/creating-and-maintaining-tables/creating-table-
constraint.php