SlideShare a Scribd company logo
LECTURE -3
S T R U C T U R E D Q U E R Y L A N G U A G E
D A T A D E F I N I T I O N L A N G U A G E ( D D L )
D e l i v e r e d B y :
M r . S h u b h a m S h u k l a
DATA DEFINITION LANGUAGE (DDL):
Three basic commands:
• CREATE
• DROP
• ALTER
CREATE
• CREATE TABLE Statement
Syntax
CREATE TABLE table_name
(
column1 datatype [ NULL | NOT NULL ],
column2 datatype [ NULL | NOT NULL ],
...
column_n datatype [ NULL | NOT NULL ]
);
Example:
CREATE TABLE customers
(
customer_id number(10) NOT NULL,
customer_name varchar2(50) NOT NULL,
city varchar2(50),
PRIMARY KEY (customer_id)
);
CREATE TABLE AS Statement:
Create Table - By Copying all columns from another table
Syntax
CREATE TABLE new_table
AS (SELECT * FROM old_table);
Create Table - By Copying selected columns from another table
Syntax
CREATE TABLE new_table
AS (SELECT column_1, column2, ... column_n FROM old_table);
Create table - By Copying selected columns from multiple tables:
Syntax
CREATE TABLE new_table
AS (SELECT column_1, column2, ... column_n
FROM old_table_1, old_table_2, ... old_table_n);
Example:
CREATE TABLE sup_cust
AS (SELECT customers.customer_id, customers.city, Supplier1.Supplier_name
FROM customers, Supplier1
WHERE customers.customer_id = Supplier1.Supplier_id);
ALTER
• ALTER TABLE Statement
Add Single column in table
Syntax
ALTER TABLE table_name
ADD column_name column-definition;
Example:
ALTER TABLE customers
ADD customer_name varchar2(45);
Add multiple columns in table
Syntax
ALTER TABLE table_name
ADD (column_1 column-definition,
column_2 column-definition,
...
column_n column_definition);
For example:
ALTER TABLE customers
ADD (customer_name varchar2(45),
city varchar2(40));
Modify column in table
Syntax
ALTER TABLE table_name
MODIFY column_name column_type;
For example:
ALTER TABLE customers
MODIFY customer_name varchar2(100) not null;
Modify Multiple columns in table
Syntax
ALTER TABLE table_name
MODIFY (column_1 column_type,
column_2 column_type,
...
column_n column_type);
For example:
ALTER TABLE customers
MODIFY (customer_name varchar2(100) not null,
city varchar2(75));
Drop column in table
Syntax
ALTER TABLE table_name
DROP COLUMN column_name;
For example:
ALTER TABLE customers
DROP COLUMN customer_name;
Rename column in table
Syntax
ALTER TABLE table_name
RENAME COLUMN old_name to new_name;
For example:
ALTER TABLE customers
RENAME COLUMN customer_name to cname;
Rename table
Syntax
ALTER TABLE table_name
RENAME TO new_table_name;
For example:
ALTER TABLE customers
RENAME TO contacts;
DROP
DROP TABLE Statement
Syntax
DROP TABLE [schema_name].table_name
[ CASCADE CONSTRAINTS ]
[ PURGE ];
CASCADE CONSTRAINTS: Optional. If specified, all referential integrity constraints will
be dropped as well.
PURGE: Optional. If specified, the table and its dependent objects will be purged from the
recycle bin and you will not be able to recover the table. If not specified, the table and its
dependent objects are placed in the recycle bin and can be recovered later, if needed.
For example:
DROP TABLE customers PURGE;
VIEW
• It is a virtual table that does not physically exist. Rather, it is created by a
query joining one or more tables.
Create VIEW
Syntax
CREATE VIEW view_name AS
SELECT columns
FROM tables
[WHERE conditions];
VIEW
Example:
CREATE VIEW sup_orders AS
SELECT suppliers.supplier_id, orders.quantity, orders.price
FROM suppliers
INNER JOIN orders
ON suppliers.supplier_id = orders.supplier_id
WHERE suppliers.supplier_name = 'Microsoft';
Update VIEW
Syntax
CREATE OR REPLACE VIEW view_name AS
SELECT columns
FROM table
WHERE conditions;
Example
CREATE or REPLACE VIEW sup_orders AS
SELECT suppliers.supplier_id, orders.quantity, orders.price
FROM suppliers
INNER JOIN orders
ON suppliers.supplier_id = orders.supplier_id
WHERE suppliers.supplier_name = 'Apple';
Drop VIEW
Syntax
DROP VIEW view_name;
Example
DROP VIEW sup_orders;
THANK YOU

More Related Content

PPTX
Lecture 4 sql {basics keys and constraints}
PPTX
Entigrity constraint
PDF
Learn plsql
ODT
PPT
MY SQL
PPTX
Null values, insert, delete and update in database
PDF
Sql commands
PDF
SQL Quick Reference Card
Lecture 4 sql {basics keys and constraints}
Entigrity constraint
Learn plsql
MY SQL
Null values, insert, delete and update in database
Sql commands
SQL Quick Reference Card

What's hot (19)

PPTX
DOCX
Bibashsql
DOCX
Sql reference from w3 schools
PPT
Select To Order By
PPTX
Commands
PPT
PPTX
Oracle: PLSQL Commands
PPTX
Database Management - Lecture 2 - SQL select, insert, update and delete
PDF
Database Systems - SQL - DDL Statements (Chapter 3/2)
PPTX
PPTX
Oracle: DDL
PPT
Mysql Statments
 
PPTX
DATABASE CONSTRAINTS
PPTX
Ppt INFORMATIVE PRACTICES for class 11th chapter 14
PPTX
Oracle: DML
PPTX
SQL Tutorial for Beginners
PDF
Data Definition Language (DDL)
PPTX
06.01 sql select distinct
Bibashsql
Sql reference from w3 schools
Select To Order By
Commands
Oracle: PLSQL Commands
Database Management - Lecture 2 - SQL select, insert, update and delete
Database Systems - SQL - DDL Statements (Chapter 3/2)
Oracle: DDL
Mysql Statments
 
DATABASE CONSTRAINTS
Ppt INFORMATIVE PRACTICES for class 11th chapter 14
Oracle: DML
SQL Tutorial for Beginners
Data Definition Language (DDL)
06.01 sql select distinct
Ad

Similar to Lecture 3 sql {basics ddl commands} (20)

PPTX
Oraclesql
PPTX
SQL - DML and DDL Commands
PPT
Sqlbysandeep
PPTX
MySqL_n.pptx edshdshfbhjbdhcbjdchdchjcdbbjd
PPT
Les09 (using ddl statements to create and manage tables)
PPT
PPTX
Introduction to sql new
PPTX
Basic sql Commands
PPT
PPTX
SQL Class Note By Amit Maity PowerPoint Presentation
PPTX
Module4.4_DDL_DML_DCL_ for beginners .pptx
DOCX
COMPUTERS SQL
DOCX
Unit-1 SQL fundamentals.docx SQL commands used to create table, insert values...
PDF
Sql cheat sheet
PPTX
SQL DATABASE MANAGAEMENT SYSTEM FOR CLASS 12 CBSE
PPT
Using ddl statements to create and manage tables
PPTX
SQL : Structured Query Language
PDF
dbms.pdf
Oraclesql
SQL - DML and DDL Commands
Sqlbysandeep
MySqL_n.pptx edshdshfbhjbdhcbjdchdchjcdbbjd
Les09 (using ddl statements to create and manage tables)
Introduction to sql new
Basic sql Commands
SQL Class Note By Amit Maity PowerPoint Presentation
Module4.4_DDL_DML_DCL_ for beginners .pptx
COMPUTERS SQL
Unit-1 SQL fundamentals.docx SQL commands used to create table, insert values...
Sql cheat sheet
SQL DATABASE MANAGAEMENT SYSTEM FOR CLASS 12 CBSE
Using ddl statements to create and manage tables
SQL : Structured Query Language
dbms.pdf
Ad

Recently uploaded (20)

PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PDF
Pre independence Education in Inndia.pdf
PDF
Insiders guide to clinical Medicine.pdf
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
PDF
Mark Klimek Lecture Notes_240423 revision books _173037.pdf
PPTX
Microbial diseases, their pathogenesis and prophylaxis
PDF
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
PDF
Classroom Observation Tools for Teachers
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PDF
01-Introduction-to-Information-Management.pdf
PPTX
PPH.pptx obstetrics and gynecology in nursing
PDF
VCE English Exam - Section C Student Revision Booklet
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PDF
RMMM.pdf make it easy to upload and study
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
Pre independence Education in Inndia.pdf
Insiders guide to clinical Medicine.pdf
STATICS OF THE RIGID BODIES Hibbelers.pdf
Abdominal Access Techniques with Prof. Dr. R K Mishra
Mark Klimek Lecture Notes_240423 revision books _173037.pdf
Microbial diseases, their pathogenesis and prophylaxis
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
Classroom Observation Tools for Teachers
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
human mycosis Human fungal infections are called human mycosis..pptx
O5-L3 Freight Transport Ops (International) V1.pdf
2.FourierTransform-ShortQuestionswithAnswers.pdf
01-Introduction-to-Information-Management.pdf
PPH.pptx obstetrics and gynecology in nursing
VCE English Exam - Section C Student Revision Booklet
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
RMMM.pdf make it easy to upload and study
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf

Lecture 3 sql {basics ddl commands}

  • 1. LECTURE -3 S T R U C T U R E D Q U E R Y L A N G U A G E D A T A D E F I N I T I O N L A N G U A G E ( D D L ) D e l i v e r e d B y : M r . S h u b h a m S h u k l a
  • 2. DATA DEFINITION LANGUAGE (DDL): Three basic commands: • CREATE • DROP • ALTER
  • 3. CREATE • CREATE TABLE Statement Syntax CREATE TABLE table_name ( column1 datatype [ NULL | NOT NULL ], column2 datatype [ NULL | NOT NULL ], ... column_n datatype [ NULL | NOT NULL ] );
  • 4. Example: CREATE TABLE customers ( customer_id number(10) NOT NULL, customer_name varchar2(50) NOT NULL, city varchar2(50), PRIMARY KEY (customer_id) );
  • 5. CREATE TABLE AS Statement: Create Table - By Copying all columns from another table Syntax CREATE TABLE new_table AS (SELECT * FROM old_table); Create Table - By Copying selected columns from another table Syntax CREATE TABLE new_table AS (SELECT column_1, column2, ... column_n FROM old_table);
  • 6. Create table - By Copying selected columns from multiple tables: Syntax CREATE TABLE new_table AS (SELECT column_1, column2, ... column_n FROM old_table_1, old_table_2, ... old_table_n); Example: CREATE TABLE sup_cust AS (SELECT customers.customer_id, customers.city, Supplier1.Supplier_name FROM customers, Supplier1 WHERE customers.customer_id = Supplier1.Supplier_id);
  • 7. ALTER • ALTER TABLE Statement Add Single column in table Syntax ALTER TABLE table_name ADD column_name column-definition; Example: ALTER TABLE customers ADD customer_name varchar2(45);
  • 8. Add multiple columns in table Syntax ALTER TABLE table_name ADD (column_1 column-definition, column_2 column-definition, ... column_n column_definition); For example: ALTER TABLE customers ADD (customer_name varchar2(45), city varchar2(40));
  • 9. Modify column in table Syntax ALTER TABLE table_name MODIFY column_name column_type; For example: ALTER TABLE customers MODIFY customer_name varchar2(100) not null;
  • 10. Modify Multiple columns in table Syntax ALTER TABLE table_name MODIFY (column_1 column_type, column_2 column_type, ... column_n column_type); For example: ALTER TABLE customers MODIFY (customer_name varchar2(100) not null, city varchar2(75));
  • 11. Drop column in table Syntax ALTER TABLE table_name DROP COLUMN column_name; For example: ALTER TABLE customers DROP COLUMN customer_name;
  • 12. Rename column in table Syntax ALTER TABLE table_name RENAME COLUMN old_name to new_name; For example: ALTER TABLE customers RENAME COLUMN customer_name to cname;
  • 13. Rename table Syntax ALTER TABLE table_name RENAME TO new_table_name; For example: ALTER TABLE customers RENAME TO contacts;
  • 14. DROP DROP TABLE Statement Syntax DROP TABLE [schema_name].table_name [ CASCADE CONSTRAINTS ] [ PURGE ]; CASCADE CONSTRAINTS: Optional. If specified, all referential integrity constraints will be dropped as well. PURGE: Optional. If specified, the table and its dependent objects will be purged from the recycle bin and you will not be able to recover the table. If not specified, the table and its dependent objects are placed in the recycle bin and can be recovered later, if needed. For example: DROP TABLE customers PURGE;
  • 15. VIEW • It is a virtual table that does not physically exist. Rather, it is created by a query joining one or more tables. Create VIEW Syntax CREATE VIEW view_name AS SELECT columns FROM tables [WHERE conditions];
  • 16. VIEW Example: CREATE VIEW sup_orders AS SELECT suppliers.supplier_id, orders.quantity, orders.price FROM suppliers INNER JOIN orders ON suppliers.supplier_id = orders.supplier_id WHERE suppliers.supplier_name = 'Microsoft';
  • 17. Update VIEW Syntax CREATE OR REPLACE VIEW view_name AS SELECT columns FROM table WHERE conditions;
  • 18. Example CREATE or REPLACE VIEW sup_orders AS SELECT suppliers.supplier_id, orders.quantity, orders.price FROM suppliers INNER JOIN orders ON suppliers.supplier_id = orders.supplier_id WHERE suppliers.supplier_name = 'Apple';
  • 19. Drop VIEW Syntax DROP VIEW view_name; Example DROP VIEW sup_orders;