Unit II New
Unit II New
Unit II
SQL statements & Working with Tables
a) DDL –
DDL stands for Data Definition Language. Data Definition Language is the part of SQL that
allows a database user to create and restructure database objects. These commands are normally not
used by a general user. They are normally used by the Database Administrator (DBA).
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. DML
commands are not auto-committed. It means changes made by DML command are not permanent to
database, it can be rolled back.
There are three basic DML commands: INSERT, UPDATE, and DELETE
c) DQL –
DQL stands for Data Query Language. A query is a request for information from a database. It is
the component of SQL statement that allows getting data from the database and imposing ordering
upon it. It includes the SELECT command.
This command is the heart of SQL. When SELECT is fired against a table or tables the result is
compiled into a further temporary table, which is displayed or perhaps received by the program i.e.
front end.
d) DCL –
DCL stands for Data Control Language. Data Control Language is used to control privilege in
Database. To perform any operation in the database, such as for creating tables, sequences or views
we need privileges.
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.
Prepared By , Mr N.A. Mhetre
COCSIT , Latur
B.Sc.CS/B.Sc.SE/BCASY Unit II – SQL statements & Working with Tables
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.
If the scale is omitted then the default is zero. If the precision is omitted, values are stored with
their original precision up to the maximum of 38 digits.
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.
ORACLE compares CHAR values using blank-padded comparison semantics i.e. if a value is
inserted in a cell of CHAR data type is shorter than the size it is defined for then it will be padded with
spaces on the right until it reaches the size characters in length.
3) VARCHAR (size) –
This data type is 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 2000 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.
Note: A difference between CHAR data type & VARCHAR / VARCHAR2 data types are, ORACLE
compares VARCHAR / VARCHAR2 values using non-padded comparison semantics i.e. the inserted
values will not be padded with spaces. But, CHAR is much faster than VARCHAR, and VARCHAR2.
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.
Syntax –
CREATE TABLE tbl_name
(
column1 datatype [ NULL | NOT NULL ],
column2 datatype [ NULL | NOT NULL ],
...
column_n datatype [ NULL | NOT NULL ]
);
Example –
CREATE TABLE student
(
RN NUMBER (2) NOT NULL,
NAME VARCHAR2 (10) NOT NULL,
CITY VARCHAR2 (10)
);
Table created.
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 –
ALTER TABLE student ADD MOBILE_NO NUMBER (10);
Table altered.
This example will add a column called MOBILE_NO to the STUDENT table.
Example –
ALTER TABLE student ADD
(NATIONALITY VARCHAR2 (10), DOB DATE);
Table altered.
This example will add two columns, NATIONALITY as a VARCHAR2 (10) field and DOB as a DATE
field to the STUDENT table.
Example –
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.
This example will modify both the CITY and NATIONALITY columns.
Example –
ALTER TABLE student DROP COLUMN NATIONALITY;
Table altered.
This example will drop the column called NATIONALITY from the table called STUDENT.
Example –
ALTER TABLE student RENAME COLUMN MOBILE_NO to MNO;
Table altered.
This ALTER TABLE example will rename the column called MOBILE_NO to MNO.
g) Rename table –
Syntax –
ALTER TABLE tbl_name RENAME TO new_tbl_name;
Example –
ALTER TABLE student RENAME TO stud_info;
Table altered.
3) RENAME –
Oracle allows renaming of tables. The rename operation is done atomically, which means that
Syntax –
RENAME old_tbl_name TO new_tbl_name;
Example –
RENAME stud_info TO student;
Table renamed.
4) TRUNCATE TABLE –
The TRUNCATE TABLE statement is used to remove all records from a table. It performs the
same function as a DELETE statement without a WHERE clause.
Note: If you truncate a table, the TRUNCATE TABLE statement cannot be rolled back.
Syntax –
TRUNCATE TABLE tbl_name;
Example –
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 –
1) 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.
Example –
INSERT INTO student VALUES (1, 'AMOL', 'LATUR');
1 row created.
Example –
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 –
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 RAHUL
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 –
SELECT * FROM student;
RN NAME CITY
1 AMOL LATUR
2 ATUL LATUR
3 RAHUL
4 BALAJI AUSA
The following SQL statement will update the CITY to ‘NANDED’ for all records whose RN is 3.
UPDATE student SET CITY = ‘NANDED’ WHERE RN = 3;
1 row updated.
RN NAME CITY
1 AMOL LATUR
2 ATUL LATUR
3 RAHUL 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 –
SELECT * FROM student;
RN NAME CITY
1 AMOL LATUR
2 ATUL LATUR
3 RAHUL 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 RAHUL NANDED
Select
The SELECT statement is used to retrieve records from table.
Syntax –
SELECT column1, column2, ...
FROM tbl_name
[WHERE condition(s)];
RN NAME CITY
1 AMOL LATUR
2 ATUL LATUR
3 RAHUL 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 –
SELECT RN, NAME FROM student;
RN NAME
1 AMOL
2 ATUL
3 RAHUL
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 –
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 –
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);
Example –
SELECT * FROM student;
RN NAME CITY
1 AMOL LATUR
2 ATUL LATUR
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.
The DISTINCT clause can only be used with SELECT statements.
Syntax –
SELECT DISTINCT column1, column2, .. FROM tbl_name
[WHERE condition(s)];
Example –
SELECT * FROM student;
RN NAME CITY
1 AMOL LATUR
2 ATUL LATUR
Prepared By , Mr N.A. Mhetre
COCSIT , Latur
B.Sc.CS/B.Sc.SE/BCASY Unit II – SQL statements & Working with Tables
3 RAHUL 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 –
1) 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.