SQL 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
CREATE TABLE table_name (
column1 datatype constraint,
column2 datatype constraint,
column3 datatype constraint,
....
);
• 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.
• Constraints can be column level or table level. Column level
constraints apply to a column, and table level constraints apply to the
whole table.
The following constraints are commonly used in SQL:
•NOT NULL - Ensures that a column cannot have a NULL value
•UNIQUE - Ensures that all values in a column are different
•PRIMARY KEY - A combination of a NOT NULL and UNIQUE.
Uniquely identifies each row in a table
•FOREIGN KEY - Prevents actions that would destroy links between
tables
•CHECK - Ensures that the values in a column satisfies a specific
condition
•DEFAULT - Sets a default value for a column if no value is specified
•CREATE INDEX - Used to create and retrieve data from the database
very quickly
1. Character Data Types
•CHAR(size) – Fixed-length character data (max 2000 bytes).
•VARCHAR2(size) – Variable-length character data (max 4000 bytes).
•NCHAR(size) – Fixed-length Unicode character data.
•NVARCHAR2(size) – Variable-length Unicode character data.
•CLOB – Character Large Object, stores large text (up to 4GB).
•NCLOB – National Character Large Object, Unicode text storage.
2. Numeric Data Types
•NUMBER(p,s) – Fixed and floating-point numbers.
•BINARY_FLOAT – 32-bit floating point.
•BINARY_DOUBLE – 64-bit floating point.
3. Date and Time Data Types
•DATE – Stores date and time (YYYY-MM-DD HH:MI:SS).
•TIMESTAMP – Stores date and time with fractional seconds.
•TIMESTAMP WITH TIME ZONE – Includes time zone information.
•TIMESTAMP WITH LOCAL TIME ZONE – Normalized to the
session’s time zone.
•INTERVAL YEAR TO MONTH – Stores differences in years and
months.
•INTERVAL DAY TO SECOND – Stores differences in days, hours,
minutes, seconds.