Professional Documents
Culture Documents
Key Concepts: create, alter, drop, truncate, rename, primary key, foreign key, not null, check, unique and
default
DDL Commands:
1. Create:
-Creates a new table, a view of a table, or other object in database.
Syntax:
SQL Constraints
SQL constraints are used to specify rules for the data in a table.
If there is any violation between the constraint and the data action, the action is aborted by the constraint.
Constraints can be specified when the table is created (inside the CREATE TABLE statement) or after the
table is created (inside the ALTER TABLE statement).
SQL CREATE TABLE + CONSTRAINT
Syntax
PRIMARY KEY - A combination of a NOT NULL and UNIQUE. Ensures that a column (or combination
of two or more columns) have a unique identity which helps to find a particular record in a table more
easily and quickly
FOREIGN KEY - Ensure the referential integrity of the data in one table to match values in another table
UNIQUE - Ensures that each row for a column must have a unique value
- A PRIMARY KEY constraint automatically has a UNIQUE & NOT NULL constraint defined on
it.
Syntax:
Syntax:
create table table_name(column_name1 data_type(size) column_name2
data_type(size) ,),primary key(column_name1,column_name2)....);
Syntax: Table 1
Table 2
The NOT NULL constraint enforces a column to NOT accept NULL values.
The NOT NULL constraint enforces a field to always contain a value. This means that you cannot insert a
new record, or update a record without adding a value to this field.
The following SQL enforces the "P_Id" column and the "LastName" column to not accept NULL values:
Example
UNIQUE Constraint
The UNIQUE constraint uniquely identifies each record in a database table.
The UNIQUE and PRIMARY KEY constraints both provide a guarantee for uniqueness for a column or set
of columns.
Note that you can have many UNIQUE constraints per table, but only one PRIMARY KEY constraint per
table.
create table persons(p_id int not null unique,lastname varchar(255) not null,
firstname varchar(255),address varchar(255),city varchar(255))
To allow naming of a UNIQUE constraint, and for defining a UNIQUE constraint on multiple columns,
use the following SQL
syntax:
The CHECK Constraint enables a condition to check the value being entered into a record. If the condition
evaluates to false, the record violates the constraint and isn't entered the table.
Example:
create table customers( id int not null, name varchar (20) not null, age int
not null check (age >= 18), address char (25) , salary decimal (18, 2), primary key (id));
Default Constraint
The DEFAULT constraint provides a default value to a column when the INSERT INTO statement does not
provide a specific value.
Example:
2. al
create table customers (id int not null, name varchar (20) not null, age int not t
drop table table_name
null, address char (25) , salary decimal (18, 2) default 5000.00, primary key (id)); e
r
:
- Modifies an existing database object, such as a table.
Syntax:
To delete a column in a table, use the following syntax (notice that some database systems don't allow
deleting a column):
To change the data type of a column in a table, use the following syntax:
3.
3.
drop
- Deletes an entire table, a view of a table or other object in the database.
Syntax:
What if we only want to delete the data inside the table, and not the table itself?
4.
drop database database_name 4.
4.
4.
truncate
- deletes the data inside the table, and not the table itself
Conclusion: