Unit II
Unit II
Unit II
SQL statements & Working with Tables
a) DDL –
DDL stands for Data Definition Language. DDL statements create, modify, and remove database
objects such as tables, indexes, users, etc. Common DDL statements are CREATE, ALTER, and DROP.
1. CREATE – To create database and its objects like (table, index, views, function and triggers).
2. ALTER – Modify the structure of the existing database and its objects.
3. DROP – Remove database and its objects from the database.
4. TRUNCATE – Remove all records from a table.
5. RENAME – To give new name to a database object.
b) DML –
Data Manipulation Language (DML) statements are used for managing data in database. There
are three basic DML commands: INSERT, UPDATE, and DELETE
d) DCL –
DCL stands for Data Control Language.
Privileges are of two types, SYSTEM and OBJECT. Common DCL statements are GRANT &
REVOKE.
1. GRANT – It gives user's access privileges to database.
2. REVOKE – It withdraw access privileges given with the GRANT command.
e) TCL –
Transaction Control Language (TCL) statements are used to manage the changes made by DML
statements. Common TCL statements are COMMIT and ROLLBACK.
1. COMMIT – Commit command is used to permanently save any transaction into database.
2. ROLLBACK - Restore database to original since the last COMMIT.
1) NUMBER (p, s) –
The NUMBER data type is used to store numbers (fixed or floating point). Numbers of virtually
any magnitude may be stored up to 38 digits of precision. The precision, (p) determines the maximum
length of data, whereas scale, (s) determines the number of places to the right of the decimal.
2) CHAR (size) –
This data type is used to store character strings values of fixed length. The size in the brackets
determines the number of characters the cell can hold. The maximum number of characters (i.e. the
size) this data type can hold is 255 characters.
4) VARCHAR2 (size) –
This data type is also used to store variable length alphanumeric data. The size in the brackets
determines the number of characters the cell can hold. The maximum number of characters (i.e. the
size) this data type can hold is 4000 characters.
5) LONG –
This data type is used to store variable length character strings containing up to 2 GB. Only one
LONG value can be defined per table. LONG values cannot be used in subqueries, functions, where
clauses or indexes.
6) DATE –
The DATE data type is used to represent date & time. The standard format is DD-MON-YY as in
11-SEP-85.
The default date for a date is the first day of the current month. Valid dates range from January
1, 4712 BC (Before Christ) to December 31, 9999 AD (Anno Domini).
The RAW / LONG RAW data type is used to store binary data, such as digitized picture or
image. Data loaded into columns of these data types are stored without any further conversion. RAW
data type can have a maximum length of 255 bytes. LONG RAW data type can contain up to 2 GB.
RAW / LONG RAW data cannot be manipulated. RAW / LONG RAW data is always returned as a
hexadecimal character value.
Example –
SQL > CREATE TABLE student
(
RN NUMBER (2) NOT NULL,
NAME VARCHAR2 (10) NOT NULL,
CITY VARCHAR2 (10)
);
Table created.
with the records from the existing table (based on the SELECT Statement).
This example would create a new table called STUD1 that included all columns from the
STUDENT table. If there were records in the STUDENT table, then the new STUD1 table would
be populated with the records returned by the SELECT statement.
This example would create a new table called STUD3 that included RN and NAME columns
from the STUDENT table. If there were records in the STUDENT table, then the new STUD3
table would be populated with the records returned by the SELECT statement.
Example –
SQL> ALTER TABLE student ADD MOBILE_NO NUMBER (10);
Table altered.
This example will add a column called MOBILE_NO to the STUDENT table.
Example –
SQL> ALTER TABLE student MODIFY MOBILE_NO NUMBER (11) UNIQUE;
Table altered.
This example will modify the column called MOBILE_NO to be a data type of NUMBER (11) and
force the column to not allow duplicate values.
Example –
SQL> ALTER TABLE student DROP COLUMN NATIONALITY;
Table altered.
This example will drop the column called NATIONALITY from the table called STUDENT.
Example –
SQL> ALTER TABLE student RENAME COLUMN MOBILE_NO to MNO;
Table altered.
This ALTER TABLE example will rename the column called MOBILE_NO to MNO.
e) Rename table –
Syntax –
ALTER TABLE tbl_name RENAME TO new_tbl_name;
Example –
SQL> ALTER TABLE student RENAME TO stud_info;
Table altered.
This example will rename the STUDENT table to STUD_INFO.
3) RENAME –
Oracle allows renaming of tables. The rename operation is done atomically, which means that
no other thread can access any of the tables while the rename process is running.
Syntax –
RENAME old_tbl_name TO new_tbl_name;
Example –
SQL> RENAME stud_info TO student;
Table renamed.
Note: If you truncate a table, the TRUNCATE TABLE statement cannot be rolled back.
Syntax –
TRUNCATE TABLE tbl_name;
Example –
SQL> TRUNCATE TABLE student;
Table truncated.
This example would truncate the table called STUDENT and remove all records from that table.
5) DROP TABLE –
The DROP TABLE statement allows you to remove or delete a table from the Oracle database.
Syntax –
DROP TABLE tbl_name [CASCADE CONSTRAINTS]
Example –
SQL> DROP TABLE student;
Table dropped.
If there are referential integrity constraints on STUDENT and you do not specify the
CASCADE CONSTRAINTS option, the DROP TABLE statement will return an error and Oracle will
not drop the table.
3) DROP TABLE student CASCADE CONSTRAINTS
Table dropped.
This example would drop the table called STUDENT as well as all referential integrity
constraints.
Example –
SQL> INSERT INTO student VALUES (1, 'AMOL', 'LATUR');
1 row created.
Example –
SQL> INSERT INTO student (RN, NAME) VALUES (3, 'RAHUL');
1 row created.
c) Syntax (Insert values in all columns through column reference: & sign) –
INSERT INTO tbl_name VALUES (&column1, &column2, .. );
Example –
SQL> INSERT INTO student VALUES (&RN, '&NAME', '&CITY');
Enter value for rn: 4
Enter value for name: BALAJI
Enter value for city: AUSA
old 1: INSERT INTO student VALUES(&RN,'&NAME','&CITY')
new 1: INSERT INTO student VALUES(4,'BALAJI','AUSA')
1 row created.
RN NAME CITY
1 AMOL LATUR
2 ATUL LATUR
3 SAMEER
4 BALAJI AUSA
2) UPDATE –
The UPDATE statement is used to modify the existing record(s) in a table.
Syntax –
UPDATE tbl_name
SET column1 = value1, ...
[WHERE condition(s)];
Example –
SQL> SELECT * FROM student;
RN NAME CITY
1 AMOL LATUR
2 ATUL LATUR
3 SAMEER
4 BALAJI AUSA
The following SQL statement will update the CITY to ‘NANDED’ for all records whose RN is 3.
SQL> UPDATE student SET CITY = ‘NANDED’ WHERE RN = 3;
1 row updated.
SQL> SELECT * FROM student;
RN NAME CITY
1 AMOL LATUR
2 ATUL LATUR
3 SAMEER NANDED
4 BALAJI AUSA
3) DELETE –
The DELETE statement is a used to delete a one or more records from a table.
Syntax –
DELETE FROM tbl_name [WHERE condition(s)];
Example –
SQL> SELECT * FROM student;
RN NAME CITY
1 AMOL LATUR
2 ATUL LATUR
3 SAMEER NANDED
4 BALAJI AUSA
The following SQL statement deletes the student ‘BALAJI’ from the STUDENT table.
RN NAME CITY
1 AMOL LATUR
2 ATUL LATUR
3 SAMEER NANDED
Syntax –
SELECT column1, column2, ...
FROM tbl_name
[WHERE condition(s)];
RN NAME CITY
1 AMOL LATUR
2 ATUL LATUR
3 SAMEER NANDED
In this example, we've used * to signify that we wish to select all fields from the STUDENT
table. Conditions are not provided, so it displayed all records from STUDENT table.
b) Selected Fields and All Rows –
Syntax –
SELECT column1, column2, …. FROM tbl_name;
Example –
SQL> SELECT RN, NAME FROM student;
RN NAME
1 AMOL
2 ATUL
3 SAMEER
This example would return only the RN and NAME fields from the STUDENT table. Conditions
are not provided, so it displayed all values for RN and NAME fields from STUDENT table.
Example –
SQL> SELECT * FROM student WHERE CITY = ‘LATUR’;
RN NAME CITY
1 AMOL LATUR
2 ATUL LATUR
In this example, we've used * to signify that we wish to select all fields from the STUDENT table
where the CITY is equal to LATUR.
Example –
SQL> SELECT RN, CITY FROM student WHERE CITY = ‘NANDED’;
RN CITY
3 NANDED
This example would return only the RN and NAME fields from the STUDENT table where the
CITY is equal to NANDED.
* WHERE Clause –
The WHERE clause is used to filter records from a SELECT, UPDATE or DELETE statement. The
WHERE clause is used to extract only those records that fulfill a specified condition(s).
Syntax –
WHERE condition(s);
RN NAME CITY
1 AMOL LATUR
2 ATUL LATUR
3 SAMEER NANDED
RN NAME CITY
2 ATUL LATUR
In this example, we've used the WHERE clause to filter our results from the STUDENT table. The
SELECT statement above would return all rows from the STUDENT table where the RN is 2.
Because the * is used in the SELECT, all fields from the STUDENT table would appear in the
result set.
* DISTINCT Clause
Inside a table, a column often contains many duplicate values; and sometimes you only want to
list the different (distinct) values. The DISTINCT clause is used to return only distinct (different) values.
RN NAME CITY
1 AMOL LATUR
2 ATUL LATUR
3 SAMEER NANDED
CITY
LATUR
NANDED
This above example would return all unique CITY values from the STUDENT table.
RN CITY
1 LATUR
2 LATUR
3 NANDED
Syntax –
column_name AS alias_name
Example –
SQL> SELECT RN AS ROLLNO FROM student;
ROLLNO
1
2
3
ROLL_NO
1
2
3
ROLL NO
1
2
3
Data Constraints: -
Integrity Constraints are used to apply business rules for the database tables.
The constraints available in SQL are Foreign Key, Not Null, Unique, Check.
For Example: To create an employee table with Primary Key constraint, the query would be like.