KEMBAR78
Sql basics and DDL statements | PPT
SQL Basics
and
DDL statements
Mohd Tousif
2
SQL
Originally 'Sequel' , part of an IBM project in 70's.
SQL is Structured Query Language.
Programming language used for storing and managing data in RDBMS.
All operations performed in Oracle database are run using SQL
statements.
SQL is a declarative (non-procedural language).
Data Types

CHAR (size) : - Stores character strings values of fixed length. The size in the
braces indicates the number of characters the cell can hold. The maximum
number of characters this data type can hold is 255 characters.

VARCHAR(size) / VARCHAR2(size) : - Stores variable length alpha-numeric
data.

DATE : - Used to represent date and time. Standard format is DD-MON -YY.

NUMBER(p,s) : - Stores numbers.

LONG : - Stores variable length character strings. Only one long data can be
defined per table.

RAW / LONG RAW : - Stores binary data, such as digitized picture or image.
Schema & Table
Schema : Collection of logical structures of data or schema objects.
Table : Basic units of data storage in an Oracle database.
SQL Statements
A statement consists of identifiers , parameters , variables, names , data types and
SQL Reserved words.

DDL (Data Definition Language) :- Commands used to create , modify and delete data
structures not data.

DML (Data Manipulation Language) : - Commands used to allow changing data within
the database.

TCL (Transaction Control Language) :- Commands used to control access to data.

DCL (Data Control Language) : - Commands used to control access to database.

DRL (Data Retrieval Language) : - Commands used to get data from the database.
Data Definition Language
DDL statements are dependent upon the structure of the table. All DDL statements
are auto-committed. DDL statements implicitly commit the preceding commands
and start new transactions.

CREATE – Used to create a new database object (Ex: table, index, synonym)

ALTER - Used for modifying the structure of the object.

DROP – Used to remove an object permanently from the database.

TRUNCATE – Used to empty the table.

RENAME – Used to change the name of the table.
CREATE : - Defines each column of the table uniquely. Each column has a
minimum of three attributes : name , data type and size.
CREATE TABLE <Table_Name> (<ColumnName1> <Data Type> (<size>),
<ColumnName2> <Data Type> (<size>),....);
Rules : -
1. A name can have maximum up to 30 characters.
2.Name should begin with alphabet.
3. A-Z , a-z , 0-9 are allowed
4. _ is allowed
5. Reserved words are not allowed
Ex: CREATE TABLE Student
(sid NUMBER(4),student_name VARCHAR2(30),gender CHAR(1));
ALTER :- To modify the structure of a table.
ALTER TABLE <Table_Name> ADD (<New_Column_Name> <Data_Type> (size));
ALTER TABLE <Table_Name> DROP <Column_Name>;
ALTER TABLE <Table_Name> MODIFY (<Column_Name <New_Data_Type>
(size) );
ALTER TABLE <Table_Name> MODIFY (<Column_Name <New_Data_Type>
(new_size) );
ALTER TABLE <Table_Name> MODIFY (<Column_Name <Data_Type>
(new_size) );
ALTER TABLE <Table_Name> RENAME COLUMN <Column_Name> to
<New_Column_Name>;
Rules : -
Cannot decrease the size of a column if table data exists.
Cannot change the data type when data exists.
Examples:
ALTER TABLE student ADD (admission_date DATE,prev_college_name
VARCHAR2(30));
ALTER TABLE student DROP COLUMN prev_college_name;
ALTER TABLE student MODIFY(student_name CHAR(20));
ALTER TABLE student RENAME student_name TO sname;
DROP : - To remove a table permanently from the database
DROP TABLE <Table_Name>;
Example : DROP TABLE student;
TRUNCATE : - Empties a table completely. Structure of the table will be available
for future reference.
TRUNCATE TABLE <Table_Name>;
Example : TRUNCATE TABLE student;
RENAME : - Changes the name of a table permanently.
RENAME <Table_Name> TO <New_Table_Name>;
Example : RENAME Student TO Univ_Student;
Data Manipulation Language
DML statements are used to modify the data available in the table. DML statements
are not auto-committed . The changes made by DML statements are not
permanently stored in the database.

INSERT :- To insert a new row / record into a table

UPDATE :- To modify the existing data in the table.

DELETE :- To remove the data available in the table.

MERGE :- To merge two rows or two tables.
INSERT : - Loads the data into the table
INSERT INTO <Table_Name> VALUES (<expression1>,<expression2>,........);
INSERT INTO <Table_Name> (<Column_Name1>) VALUES (<expression1>);
INSERT INTO <Table_Name> VALUES (<&expr1>,'<&expr2>',........);
INSERT INTO <Table_Name> (<Column_Name1>) VALUES (<&expr1>);
Examples :
INSERT INTO student VALUES (10,'xyz','M','12-oct-95');
INSERT INTO student (sno,sname) VALUES (11,'abc');
INSERT INTO student VALUES (&sno,'&sname','&gender','&doj');
INSERT INTO student (sno,sname) VALUES (&sno,'&sname');
UPDATE : - Changes the data values in a table.
UPDATE <Table_Name> SET <Column_Name>=<Expression1>;
UPDATE <Table_Name> SET <Column_Name>=<Expression1> WHERE <condition>;
Examples :
UPDATE Student SET doj='13-jun-95';
UPDATE Student SET doj='13-jun-95',gender='F' WHERE sid=11;
DELETE : - Deletes rows from a table and returns the number of records deleted.
DELETE FROM <Table_Name>;
DELETE FROM <Table_Name> WHERE <condition>;
Examples :
DELETE FROM student;
DELETE FROM student WHERE sid=11;
18
Data Retrieval Language
SELECT : - To view data from a table.
1.To view all the columns information from a table
SELECT * FROM <Table_Name>;
2. To view all the columns information of a specific column from a table
SELECT <Column_Name> from <Table_Name>;
3. To view all the columns information from a table when a specific condition is
satisfied
SELECT * FROM <Table_Name> WHERE <condition>;
4. To view all the columns information of a specific column from a table when a
specific condition is satisfied
SELECT <Column_Name> from <Table_Name> WHERE
<condition>;
Examples :
SELECT * FROM student;
SELECT sid,sname FROM student;
SELECT * FROM student WHERE sid=1;
SELECT sid,sname FROM student WHERE sid=1;
20
SQL Operators
Operators are symbols which have a special meaning within SQL
and PL/SQL statements.

Arithmetic Operators

Relational Operators

Logical Operators

SET Operators

Range Searching Operators

Pattern Matching Operators

Boolean Operators
21
Oracle allows Arithmetic Operators to be used while viewing records from
a table or while performing data manipulation operations.
+ Addition
- Subtraction
* Multiplication
/ Division
% Modulus / Remainder
Example :
SELECT 153*14/15 FROM dual;
SELECT sid,sid+10 FROM student;
Arithmetic Operators
22
The relational operators determines the comparisons within two or more
values.
= Equal
!= Not Equal
> Greater than
< Less than
>= Greater than or equal
<= Less than or equal
Example:
SELECT * FROM student WHERE sid < 20;
SELECT * FROM student WHERE dob != '10-jan-85';
Relational Operators
23
Logical Operators
Operators are used whenever multiple conditions need to be satisfied
AND
OR
NOT
SELECT * FROM student WHERE gender='M' AND dob='15-jan-86';
SELECT * FROM student WHERE gender='M' OR dob='15-jan-86';
24
SET Operators
To retrieve information from multiple tables when there are same
number of columns available in the queries
UNION
UNION ALL
INTERSECT
MINUS
25
Range Searching Operator
To retrieve information within a specified range (including the boundary
values)
BETWEEN
NOT BETWEEN
Example :
SELECT * FROM student WHERE dob BETWEEN '01-jan-85' AND '30-
jun-85';
26
Pattern Matching Operators
LIKE
NOT LIKE
IN
NOT IN
IS NULL
IS NOT NULL
Example:
SELECT * FROM student WHERE sname LIKE '_A%';
SELECT * FROM student WHERE sno IN (15,75,85);
SELECT * FROM student WHERE dob IS NOT NULL;
27
Boolean Operators
EXISTS
NOT EXISTS
ANY
ALL

Sql basics and DDL statements

  • 1.
  • 2.
    2 SQL Originally 'Sequel' ,part of an IBM project in 70's. SQL is Structured Query Language. Programming language used for storing and managing data in RDBMS. All operations performed in Oracle database are run using SQL statements. SQL is a declarative (non-procedural language).
  • 3.
    Data Types  CHAR (size): - Stores character strings values of fixed length. The size in the braces indicates the number of characters the cell can hold. The maximum number of characters this data type can hold is 255 characters.  VARCHAR(size) / VARCHAR2(size) : - Stores variable length alpha-numeric data.  DATE : - Used to represent date and time. Standard format is DD-MON -YY.  NUMBER(p,s) : - Stores numbers.  LONG : - Stores variable length character strings. Only one long data can be defined per table.  RAW / LONG RAW : - Stores binary data, such as digitized picture or image.
  • 4.
    Schema & Table Schema: Collection of logical structures of data or schema objects. Table : Basic units of data storage in an Oracle database.
  • 5.
    SQL Statements A statementconsists of identifiers , parameters , variables, names , data types and SQL Reserved words.  DDL (Data Definition Language) :- Commands used to create , modify and delete data structures not data.  DML (Data Manipulation Language) : - Commands used to allow changing data within the database.  TCL (Transaction Control Language) :- Commands used to control access to data.  DCL (Data Control Language) : - Commands used to control access to database.  DRL (Data Retrieval Language) : - Commands used to get data from the database.
  • 6.
    Data Definition Language DDLstatements are dependent upon the structure of the table. All DDL statements are auto-committed. DDL statements implicitly commit the preceding commands and start new transactions.  CREATE – Used to create a new database object (Ex: table, index, synonym)  ALTER - Used for modifying the structure of the object.  DROP – Used to remove an object permanently from the database.  TRUNCATE – Used to empty the table.  RENAME – Used to change the name of the table.
  • 7.
    CREATE : -Defines each column of the table uniquely. Each column has a minimum of three attributes : name , data type and size. CREATE TABLE <Table_Name> (<ColumnName1> <Data Type> (<size>), <ColumnName2> <Data Type> (<size>),....); Rules : - 1. A name can have maximum up to 30 characters. 2.Name should begin with alphabet. 3. A-Z , a-z , 0-9 are allowed 4. _ is allowed 5. Reserved words are not allowed Ex: CREATE TABLE Student (sid NUMBER(4),student_name VARCHAR2(30),gender CHAR(1));
  • 8.
    ALTER :- Tomodify the structure of a table. ALTER TABLE <Table_Name> ADD (<New_Column_Name> <Data_Type> (size)); ALTER TABLE <Table_Name> DROP <Column_Name>; ALTER TABLE <Table_Name> MODIFY (<Column_Name <New_Data_Type> (size) ); ALTER TABLE <Table_Name> MODIFY (<Column_Name <New_Data_Type> (new_size) ); ALTER TABLE <Table_Name> MODIFY (<Column_Name <Data_Type> (new_size) ); ALTER TABLE <Table_Name> RENAME COLUMN <Column_Name> to <New_Column_Name>; Rules : - Cannot decrease the size of a column if table data exists. Cannot change the data type when data exists.
  • 9.
    Examples: ALTER TABLE studentADD (admission_date DATE,prev_college_name VARCHAR2(30)); ALTER TABLE student DROP COLUMN prev_college_name; ALTER TABLE student MODIFY(student_name CHAR(20)); ALTER TABLE student RENAME student_name TO sname;
  • 10.
    DROP : -To remove a table permanently from the database DROP TABLE <Table_Name>; Example : DROP TABLE student;
  • 11.
    TRUNCATE : -Empties a table completely. Structure of the table will be available for future reference. TRUNCATE TABLE <Table_Name>; Example : TRUNCATE TABLE student;
  • 12.
    RENAME : -Changes the name of a table permanently. RENAME <Table_Name> TO <New_Table_Name>; Example : RENAME Student TO Univ_Student;
  • 13.
    Data Manipulation Language DMLstatements are used to modify the data available in the table. DML statements are not auto-committed . The changes made by DML statements are not permanently stored in the database.  INSERT :- To insert a new row / record into a table  UPDATE :- To modify the existing data in the table.  DELETE :- To remove the data available in the table.  MERGE :- To merge two rows or two tables.
  • 14.
    INSERT : -Loads the data into the table INSERT INTO <Table_Name> VALUES (<expression1>,<expression2>,........); INSERT INTO <Table_Name> (<Column_Name1>) VALUES (<expression1>); INSERT INTO <Table_Name> VALUES (<&expr1>,'<&expr2>',........); INSERT INTO <Table_Name> (<Column_Name1>) VALUES (<&expr1>);
  • 15.
    Examples : INSERT INTOstudent VALUES (10,'xyz','M','12-oct-95'); INSERT INTO student (sno,sname) VALUES (11,'abc'); INSERT INTO student VALUES (&sno,'&sname','&gender','&doj'); INSERT INTO student (sno,sname) VALUES (&sno,'&sname');
  • 16.
    UPDATE : -Changes the data values in a table. UPDATE <Table_Name> SET <Column_Name>=<Expression1>; UPDATE <Table_Name> SET <Column_Name>=<Expression1> WHERE <condition>; Examples : UPDATE Student SET doj='13-jun-95'; UPDATE Student SET doj='13-jun-95',gender='F' WHERE sid=11;
  • 17.
    DELETE : -Deletes rows from a table and returns the number of records deleted. DELETE FROM <Table_Name>; DELETE FROM <Table_Name> WHERE <condition>; Examples : DELETE FROM student; DELETE FROM student WHERE sid=11;
  • 18.
    18 Data Retrieval Language SELECT: - To view data from a table. 1.To view all the columns information from a table SELECT * FROM <Table_Name>; 2. To view all the columns information of a specific column from a table SELECT <Column_Name> from <Table_Name>; 3. To view all the columns information from a table when a specific condition is satisfied SELECT * FROM <Table_Name> WHERE <condition>; 4. To view all the columns information of a specific column from a table when a specific condition is satisfied SELECT <Column_Name> from <Table_Name> WHERE <condition>;
  • 19.
    Examples : SELECT *FROM student; SELECT sid,sname FROM student; SELECT * FROM student WHERE sid=1; SELECT sid,sname FROM student WHERE sid=1;
  • 20.
    20 SQL Operators Operators aresymbols which have a special meaning within SQL and PL/SQL statements.  Arithmetic Operators  Relational Operators  Logical Operators  SET Operators  Range Searching Operators  Pattern Matching Operators  Boolean Operators
  • 21.
    21 Oracle allows ArithmeticOperators to be used while viewing records from a table or while performing data manipulation operations. + Addition - Subtraction * Multiplication / Division % Modulus / Remainder Example : SELECT 153*14/15 FROM dual; SELECT sid,sid+10 FROM student; Arithmetic Operators
  • 22.
    22 The relational operatorsdetermines the comparisons within two or more values. = Equal != Not Equal > Greater than < Less than >= Greater than or equal <= Less than or equal Example: SELECT * FROM student WHERE sid < 20; SELECT * FROM student WHERE dob != '10-jan-85'; Relational Operators
  • 23.
    23 Logical Operators Operators areused whenever multiple conditions need to be satisfied AND OR NOT SELECT * FROM student WHERE gender='M' AND dob='15-jan-86'; SELECT * FROM student WHERE gender='M' OR dob='15-jan-86';
  • 24.
    24 SET Operators To retrieveinformation from multiple tables when there are same number of columns available in the queries UNION UNION ALL INTERSECT MINUS
  • 25.
    25 Range Searching Operator Toretrieve information within a specified range (including the boundary values) BETWEEN NOT BETWEEN Example : SELECT * FROM student WHERE dob BETWEEN '01-jan-85' AND '30- jun-85';
  • 26.
    26 Pattern Matching Operators LIKE NOTLIKE IN NOT IN IS NULL IS NOT NULL Example: SELECT * FROM student WHERE sname LIKE '_A%'; SELECT * FROM student WHERE sno IN (15,75,85); SELECT * FROM student WHERE dob IS NOT NULL;
  • 27.