Sunteți pe pagina 1din 20

SQL Statements

• Data Definition Language (DDL) Statements


• Data Manipulation Language (DML)
Statements
• Transaction Control Statements
• Session Control Statements
• System Control Statement
• Embedded SQL Statements
Data Definition Language (DDL)
Statements
• Data definition language (DDL) statements let
you to perform these tasks:
• Create, alter, and drop schema objects
• Grant and revoke privileges and roles
• Analyze information on a table, index, or
cluster
• Establish auditing options
• DDL Commands auto commit.
DDL Cont..
Common DDL Statements
• CREATE
• ALTER
• DROP
• TRUNCATE
• GRANT
• REVOKE
• Create - This command is used to create database
objects such as Tables,Views,Functions,Procedures etc
• ALTER – To modify the structure of a database.
• DROP - To remove the objects from database.
• Truncate – To permanently remove objects from DB
• GRANT- To give privileges to a specific user or role, or to
all users, to perform actions on database objects
• Revoke - To remove privileges from a specific user or role,
or from all users, to perform actions on database objects.
Data manipulation commands
• Data manipulation language (DML) statements
access and manipulate data in existing schema
objects.
• These statements do not implicitly commit the
current transaction
• SELECT,INSERT,DELETE,UPDATE are the
commonly used DML statements.
• INSERT-To insert rows into an existing table

• SELECT-To selects data from one or more


tables or views.

• UPDATE - updates (changes the values of) a


set of existing table rows
• DELETE – Deletes records from a table.
Transaction Control Statements

• Transaction control statements manage


changes made by DML statements
• The transaction control statements are:
COMMIT
ROLLBACK
SAVEPOINT
SET TRANSACTION
Basic datatypes
• Varchar2
• Number
• Date
Constraints
• Constraints restrict the value that a table can
store.
• Use a constraint to define an integrity
constraint--a rule that restricts the values in a
database.
Types of constraints
• Not Null
• Unique Key
• Check
• Primary Key
• Foreign Key
Not Null constraints
If a column in a table is specified as Not
Null,then its not possible to insert a null in
such column.
• Implemented with create and alter commands
• When we implement a Not Null constraint
with alter command there should not be any
null value in existing table.
• Unique Key
The unique constraint doesn’t allow duplicate
values in a column.
• We can insert null values to a unique key
constraint column
• Two null values are not equal
• Check
Check constraint is used to restrict values before
inserting into table.
• Primary Key
Combination of unique and not null constraint.
It will not allow null and duplicate values.
• Foreign Key
Columns defined as foreign keys refer primary keys of
other table.
Creating a table
• Syntax
CREATE TABLE <name_of_table>
(
<column_name1> <data_type>[(<width>)]
[constraint <constraint name> <const_type>],
<column_name2> <data_type>[(<width>)] ,
<column_name3> <data_type>[(<width>)] ,
.
<column_nameN> <data_type>[(<width>)] ,
);
Create table example

• CREATE TABLE Employee(


• Empno NUMBER(4) constraint pk_empno primary key
• ,ename VARCHAR2(50) constraint nn_ename not null
• ,salary NUMBER(10,2)
• ,hire_date DATE
• ,gender CHAR(1) check (gender in ('M','F'))
• ,email VARCHAR2(50) unique
• );
Alter table command
• To add a column in a table, the SQL ALTER TABLE syntax is:

ALTER TABLE table_name ADD column_name column-definition;

Example
ALTER TABLE supplier
ADD supplier_name varchar2(50);
• ADD MULTIPLE COLUMNS IN TABLE
Syntax
ALTER TABLE table_name
ADD (column_1 column-definition,
column_2 column-definition,
... column_n column_definition);
Example
ALTER TABLE supplier
ADD (supplier_name varchar2(50)
, city varchar2(45)
);
• Modify column in a table
ALTER TABLE table_name
MODIFY column_name column_type;

Example
ALTER TABLE supplier
MODIFY supplier_name varchar2(100) not null;
• MODIFY MULTIPLE COLUMNS IN TABLE
ALTER TABLE table_name
MODIFY (column_1 column_type,
column_2 column_type,
... column_n column_type
);
Example
ALTER TABLE supplier
MODIFY (supplier_name varchar2(100) not null
, city varchar2(75)
);
Drop table command
• Syntax
DROP TABLE TABLE_NAME;

Example

DROP TABLE Employees;

S-ar putea să vă placă și