Data Definition Language (DDL), Data Definition Language (DDL), Data Manipulation Language (DML) , Transaction Control Language (TCL) , Data Control Language (DCL) - , SQL Constraints
5. SQL commands are instructions, coded into SQL statements, which are used
to communicate with the database to perform specific tasks, work, functions
and queries with data.
SQL commands can be used not only for searching the database but also to
perform various other functions like, for example, you can create tables, add
data to tables, or modify data, drop the table, set permissions for users. SQL
commands are grouped into four major categories depending on their
functionality:
Data Definition Language (DDL) - These SQL commands are used for
creating, modifying, and dropping the structure of database objects. The
commands are CREATE, ALTER, DROP, RENAME, and TRUNCATE.
Data Manipulation Language (DML) - These SQL commands are used for
storing, retrieving, modifying, and deleting data.
These Data Manipulation Language commands are: SELECT, INSERT, UPDATE,
and DELETE.
Transaction Control Language (TCL) - These SQL commands are used for
managing changes affecting the data. These commands are COMMIT,
ROLLBACK, and SAVEPOINT.
Data Control Language (DCL) - These SQL commands are used for providing
security to database objects. These commands are GRANT and REVOKE.
5
6. The Create Table Command
The create table command defines each column of the table
uniquely. Each column
has minimum of three attributes.
Name
Data type
Size(column width).
Each table column definition is a single clause in the create table
syntax. Each table column definition is separated from the other
by a comma. Finally, the SQL statement is terminated with a
semicolon.
Syntax:
CREATE TABLE <table_name>
(
<column_name1> <datatype1> <constraint1>
<column_name2> <datatype2> <constraint2>
<constraint-list>
) ;
6
7. ALTER command
SQL command can be used to add, modify, or drop a column from the existing
table or to rename a table.
ALTER TABLE - ADD Column
To add a column in a table, use the following syntax:
ALTER TABLE table_name
ADD column_name datatype;
Eg: alter table clint_master add (telephone number(10));
ALTER TABLE - DROP COLUMN
To delete a column in a table, use the following syntax (notice that some
database systems don't allow deleting a column):
ALTER TABLE table_name
DROP COLUMN column_name;
Eg: alter table clint_master
drop column address2;
ALTER TABLE - ALTER/MODIFY COLUMN
To change the data type of a column in a table, use the following syntax:
ALTER TABLE table_name
MODIFY column_name datatype;
Eg: Alter table product_master modify(sellprice number(10,2));
7
8. The SQL DROP TABLE Statement
The DROP TABLE statement is used to drop an existing table in a
database.
Syntax
DROP TABLE table_name;
drop table clint_master;
SQL TRUNCATE TABLE
The TRUNCATE TABLE statement is used to delete the data inside a table,
but not the table itself.
Syntax
TRUNCATE TABLE table_name;
TRUNCATE TABLE clint_master;
RENAME command
The SQL command that removes the entire table.
Syntax:
RENAME TO old_table_name to new_table_name;
Rename salesman_master to sman_mast;
8
9. SQL constraints are used to specify rules for data in a table.
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,
....
);
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 - Uniquely identifies a row/record in another table
CHECK - Ensures that all values in a column satisfies a specific condition
DEFAULT - Sets a default value for a column when no value is specified
INDEX - Used to create and retrieve data from the database very quickly
9
10. SQL NOT NULL Constraint
By default, a column can hold NULL values.
The NOT NULL constraint enforces a column to NOT accept NULL values.
This enforces a field to always contain a value, which means that you cannot
insert a new record, or update a record without adding a value to this field.
The following SQL ensures that the "ID", "LastName", and "FirstName"
columns will NOT accept NULL values:
Example
CREATE TABLE Persons (
ID int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255) NOT NULL,
Age int
);
SQL 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.
10
11. Primary Key
Each table must normally contain a column or set of columns that uniquely identifies rows
of data that are stored in the table.This column or set of columns is referred to as the
primary key.Most tables have a single column as the primary key.Primary key columns are
restricted against NULLs and duplicate values.
Points to be noted -
A table can have only one primary key.
Multiple columns can be clubbed under a composite primary key.
Oracle internally creates unique index to prevent duplication in the column values.Indexes
would be discussed later in PL/SQL.
Syntax:
Column level:
◦ COLUMN [data type] [CONSTRAINT <constraint name> PRIMARY KEY]
Table level:
CONSTRAINT [constraint name] PRIMARY KEY [column (s)]
The following example shows how to use PRIMARY KEY constraint at column level.
CREATE TABLE TEST ( ID NUMBER CONSTRAINT TEST_PK PRIMARY KEY, ... );
The following example shows how to define composite primary key using PRIMARY KEY
constraint at the table level.
◦ CREATE TABLE TEST ( ..., CONSTRAINT TEST_PK PRIMARY KEY (ID) );
11
12. Foreign Key
When two tables share the parent child relationship based on specific
column, the joining column in the child table is known as Foreign Key.This
property of corresponding column in the parent table is known as Referential
integrity.Foreign Key column values in the child table can either be null or
must be the existing values of the parent table.Please note that only primary
key columns of the referenced table are eligible to enforce referential
integrity.
Syntax:
Column Level:
COLUMN [data type] [CONSTRAINT] [constraint name] [REFERENCES] [table
name (column name)]
Table level:
CONSTRAINT [constraint name] [FOREIGN KEY (foreign key column name)
REFERENCES] [referenced table name (referenced column name)]The following
example shows how to use FOREIGN KEY constraint at column level.
CREATE TABLE TEST (ccode varchar2(5) CONSTRAINT TEST_FK REFERENCES
PARENT_TEST(ccode), ... );
12
13. Check constraint
Sometimes the data values stored in a specific column must fall within some
acceptable range of values.A CHECK constraint requires that the specified
check condition is either true or unknown for each row stored in the
table.Check constraint allows to impose a conditional rule on a column, which
must be validated before data is inserted into the column. The condition must
not contain a sub query or pseudo column CURRVAL NEXTVAL, LEVEL,
ROWNUM, or SYSDATE.
Oracle allows a single column to have more than one CHECK constraint. In
fact, there is no practical limit to the number of CHECK constraints that can be
defined for a column.
Syntax:
Column level:
COLUMN [data type] CONSTRAINT [name] [CHECK (condition)]
Table level:
CONSTRAINT [name] CHECK (condition)The following example shows how to
use CHECK constraint at column level.
CREATE TABLE TEST ( ..., GRADE char (1) CONSTRAINT TEST_CHK CHECK
(upper (GRADE) in ('A','B','C')), ... );The following example shows how to use
CHECK constraint at table level.
CREATE TABLE TEST ( ..., CONSTRAINT TEST_CHK CHECK (stdate < = enddate),
);
13
14. DML(Data Manipulation Language) : The SQL commands that deals
with the manipulation of data present in database belong to DML
or Data Manipulation Language and this includes most of the SQL
statements.Examples of DML:
SELECT – is used to retrieve data from the a database.
INSERT – is used to insert data into a table.
UPDATE – is used to update existing data within a table.
DELETE – is used to delete records from a database table.
14
15. INSERT command
Insert command is used to insert data into a table. Following
is its general syntax,
INSERT INTO table_name VALUES(data1, data2, ...)
Insert value into only specific columns
We can use the INSERT command to insert values for only
some specific columns of a row. We can specify the column
names along with the values to be inserted like this,
INSERT INTO student(id, name) values(102, 'Alex');The above
SQL query will only insert id and name values in the newly
inserted record.
15
16. UPDATE command
UPDATE command is used to update any record of data in a table. Following
is its general syntax,
UPDATE table_name SET column_name = new_value WHERE some_condition;
WHERE is used to add a condition to any SQL query, we will soon study
about it in detail.
Updating Multiple Columns
We can also update values of multiple columns using a
single UPDATE statement.
UPDATE student SET name='Abhi', age=17 where s_id=103;
DELETE command
DELETE command is used to delete data from a table.
Syntax,
DELETE FROM table_name;
Delete all Records from a Table
Eg: DELETE FROM student;
The above command will delete all the records from the table student.
Delete a particular Record from a Table
In our student table if we want to delete a single record, we can use
the WHERE clause to provide a condition in our DELETE statement.
Eg : DELETE FROM student WHERE s_id=103;The above command will delete
the record where s_id is 103 from the table student.
16
17. Transaction Control Language(TCL) commands are used to manage transactions in
the database. These are used to manage the changes made to the data in a table
by DML statements. It also allows statements to be grouped together into logical
transactions.
COMMIT command
COMMIT command is used to permanently save any transaction into the database.
When we use any DML command like INSERT, UPDATE or DELETE, the changes
made by these commands are not permanent, until the current session is closed,
the changes made by these commands can be rolled back.
To avoid that, we use the COMMIT command to mark the changes as permanent.
Following is commit command's syntax,
COMMIT;
ROLLBACK command
This command restores the database to last commited state. It is also used
with SAVEPOINT command to jump to a savepoint in an ongoing transaction.
If we have used the UPDATE command to make some changes into the database,
and realise that those changes were not required, then we can use
the ROLLBACK command to rollback those changes, if they were not commited
using the COMMIT command.
Following is rollback command's syntax,
ROLLBACK TO savepoint_name;
17
18. SAVEPOINT command
SAVEPOINT command is used to temporarily save a
transaction so that you can rollback to that point whenever
required.
Following is savepoint command's syntax,
SAVEPOINT savepoint_name; In short, using this command we
can name the different states of our data in any table and
then rollback to that state using the ROLLBACK command
whenever required.
18