Oracle SQL in 10 Minutes - Asim Abbasi
CH1: SQL Basics | CH2: SQL Operators | CH3: SQL Built-in Functions | CH4: SQL Joins | CH5: UPDATE, INSERT & DELETE Statements | CH6: CREATE, ALTER & DROP Statements | CH7: Constraints | CH8: Linking Tables vs Joining Tables | CH9: SQL Statements for Other Database Objects | CH10: SQL Statements for Database Security
Chapter 7: Constraints
NOT NULL is a type of constraint once implemented on
any column; you would not be able to insert NULL values in it. Similarly
with UNIQUE once defined on any column we would not be able to insert
any record in the table with a value that was already there in UNIQUE
constraint column.
Example:
CREATE TABLE dept
(deptno NUMBER(2),
dname VARCHAR2(9) CONSTRAINT unq_dname UNIQUE,
loc VARCHAR2(10) );
In this example we have assigned a unique constraint on dname column. We
have also used the CONSTRAINT clause to assign this constraint a name.
We can also achieve the same result using the following statement.
CREATE TABLE dept
(deptno NUMBER(2),
dname VARCHAR2(9) UNIQUE,
loc VARCHAR2(10) );
The difference between this statement and the one mentioned above, we
haven’t used the CONSTRAINT clause. So what is the benefit of using this
clause? The benefit is, we can disable/enable this constraint using the
ALTER TABLE statement.
Chapter 7: Constraints
CH1: SQL Basics | CH2:
SQL Operators |
CH3: SQL Built-in Functions | CH4:
SQL Joins | CH5:
UPDATE, INSERT & DELETE Statements |
CH6: CREATE, ALTER & DROP Statements | CH7:
Constraints | CH8:
Linking Tables vs Joining Tables | CH9:
SQL
Statements for Other Database Objects | CH10:
SQL Statements for
Database Security Share with others: