Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Unit 3 SQL Extra-1-4

Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

66 DATABASE MANAGEMENT SYSTEMS

(4) Find the names of all customers who have a loan, an account, or both at the SBI branch.
{<c> I 31 «c, I> E borrower A 3 b, a «I, b, a> E loan A b = "SBI"»
V 3 a «c, a> E depositor A 3 b, n «a, b, n> E amount A b = "SBI"»)}
(5) Find the names of all customers who have an account at all the branches located in New
Delhi.
{<c> I 3 n «c, n> E customer) A V x,y,z «x,y,z> E branch A y = "New Delhi")
~ 3a,b «a,x,b> E account A <c,a> E depositor»)}
Q. Retrieve the name and address of all employees who work for the 'computer' department.
Ans. {qsv I (3 z) (3 I) (3 m) (EMPLOYEE (qrstuvwxyz) AND
DEPARTMENT (1m no) AND I = 'COMPUTER' AND (m = z»}
2.7 INTRODUCTION TO SQl
Structured Ouery Language (SOL) is a language that provides an interface to relational database
systems.
In common usage SOL also encompasses :
• DML (Data Manipulation Languages) for INSERTs, UPDATEs, DELETEs
• DDL (Data Definition Language) used for creating and modifying tables and other database
structures.
Features of SQL :
(1) SOL can be used by a range of users, including those with little or no programming
expenence.
(2) It is a non procedural language.
(3) It reduces the amount of time required for creating and maintaining systems.
(4) It is an English-Like Language.
Components of SQL : There are following components of SOL.
(1) DDL
It is a set of SOL commands used to create, modify and delete data base structure but not data.
For examples "
(i) CREATE: To create objects in the database.
(ii) ALTER: Alters the structure of the database.
(iii) DROP: Delete objects from the database.
(iv) TRUNCATE : Remov~ all records from a table, including all spaces allocated for the records
are removed.
(v) COMMENT: Add comments to the data dictionary.
(2) DML (Data Manipulation Language)
It is the area of SOL that allows changing data within the database.
For Examples "
(i) INSERT : Insert data into a table.
(ii) UPDATE: Updates existing data within a table.
(iii) DELETE : Deletes all records from a table, the space for the records remain.
(iv) LOCK TABLE: Control concurrency.
(3) DCL (Data Control Language)
It is the components of SOL statements that control access to data and to the database.
RELATIONAL DATA MODEL CONCEPTS 67
For Examples :
(i) COMMIT: Save work done
(ii) SAVE POINT: Identify a point in a transaction to which you can later roll back.
(iii) ROLL BACK: Restore database to original since the last COMMIT.
(iv) GRANT/REVOKE: Grant or take back permissions to or from the oracle users.
(v) SET TRANSACTION: Change transaction options like what rollback segment to use.
(4) DQL (Data Query Language)
It is the component of SQL statement that allows getting data from the database and imposing
ordering upon it.
For example:
(1) SELECT: Retrieve data from the database.
2.7.1 Data Types
• Data types comes in several forms and sizes, allowing the programmer to create tables suited
to the scope of the project.
• CHAR (Size) : This data type is used to store character strings values of fixed length. The
maximum number of characters this data type can hold is 255 characters.
e.g., In case of 'Name Char (15)" then data held in the variable Name is only 15 characters in
length.
• VARCHAR (Size)NARCHAR2 (size) : This data type is used to store variable length
alphanumeric data. The maximum type this can hold is 2000 characters.
• NUMBER (P, S) : The NUMBER data type is used to store numbers. The precision (P),
determines the maximum length of the data, whereas the scale, (S), dertermines the number
of places to the right of the decimal. The maximum precision (P), is 38 digits.
• DATE: This data type is used to represent date and time. The standard format is DD-MM-YY
as in 26-June-07.
The Date time stores date in the 24-hour format. By default, the time in a date field is
12 : 00 : 00 AM, if no time portion is specified.
• LONG: The LONG data type is used to store variable length character strings containing
upto 2 GB.
LONG data can be used to store arrays of binary data in ASCII format.
• RAW/LONG RAW: The RAW/LONG RAW data types is used to store binary data, such as
digitized picture or image.
RAW data type can have a maximum length of 255 bytes.
LONG RAW data type can contain up to 2 GB.
• ROWID (rowid) : The format of the rowid is :
BBBBBBB . RRRR . FFFFF
where BBBBBBB is the block in the database fIle;
RRRR is the row in the block;
FFFFF is the data base fIle.
• Boolean: Valid in PL/SQL but this data type does not exist in oracle 8 i or oracle 9 i.
2.7.2 Types of SOL Commands
(1) The Create table Command:
Syntax: CREATE TABLE table name
68 DATABASE MANAGEMENT SYSTEMS

(Column name datatype (size), column name data type (size),


column name datatype (size»;
Example: Create a Employee Table.
CREATE TABLE Employee (E-ID number (6),
ENAME char (15), ADDRESS varchar (15),
CITY char (15), STATE char (15), PIN CODE number (6»;
Output : Table Created
(2) Create a Student table
CREATE TABLE Student (Roll-No number (15), Name char (15),
Address varchar (15), Sex char (2), City char (15),
Phone number (15), State char (15»;
Output : Table Created
2.7.3 Insertion of Data Into Tables
Once a table is created, the most natural thing to do is load this table with data to be manipulated
later.
Syntax : INSERT INTO table name
(Column name 1, Column name 2 ... )
VALUES (expression, expression);
Example:
INSERT INTO Employee (E-ID, ENAME, ADDRESS, CITY,
STATE, PINCODE) VALUES (115, 'VIJAY', 'C-6 Gupta Road',
'New Delhi', 'DELHI');
Another method: INSERT INTO Employee VALUES
(& E-ID, '& NAME', '& ADDRESS', '& CITY', '& STATE');
Note:
(i) .The character or varchar expression must be enclosed in single quotes (').
(ii) In the insert into SQL statement the columns and values have a one to one relationship.
2.7.4 Select Command
Once data has been inserted into a table, the next most logical operation would be to view what
has been entered. This is achieved by SELECT SQL Verb.
(i) View global table data the syntax is :
SELECT * FROM table name;
e.g., SELECT * FROM Employee;
(ii) Retrieve ID, name, city of the employee
SELECT E-ID, ENAME, CITY FROM Employee;
Selected Columns and Selected Rows :
WHERE Clause
Syntax: SELECT * FROM table name
WHERE search condition;
e.g., (i) SELECT * FROM Employee WHERE
E-ID > 112;
RELATIONAL DATA MODEL CONCEPTS 69

(ii) SELECT Roll-No, Name FROM


Student WHERE Roll No. < = 150;
2.7.5 Elimination of Duplicates from the Select Statement
A table could contain duplicate rows. We can eliminate using select statement.
Syntax: SELECT DISTINCT Column name 1,
Column name 2 FROM table name;
Syntax: SELECT DISTINCT * FROM Table name
Example: (1) Select only unique rows from the table student:
SELECT DISTINCT * FROM Student;
2.7.6 Sorting Data in a Table
Oracle allows data from a table to be viewed in a sorted order. The rows retrieved from the table
will be sorted in either ascending or descending order depending on the condition specified in the
select statement.
Syntax: SELECT * FROM table name ORDER BY Column name 1, Column name 2 [Sort
order]; .
Example : Retrieve all rows from student and display this data sorted on the value contained in
the field Roll-No. in ascending order;
SELECT * FROM Student ORDER BY Roll-No;
Note: Oracle engine sorts in ascending order by default.
Example: For viewing the data in descending sorted order the word desc.
SELECT * FROM Student ORDER BY Roll-No desc;
2.7.7 Creating a Table from a Table
Syntax: CREATE TABLE Table name
[(Column name, Column name)]
AS SELECT column name, column name FROM Table name;
Example: Create a table student 1 from student.
CREATE TABLE Student 1
(SRoll-No, SName, Address, Sex, City, Ph.No, State)
AS SELECT Roll-No, Name, Address, Sex, City, Ph. No., State FROM Student;
2.7.8 Inserting Data Into a Table from Another Table
Syntax: INSERT INTO Table name SELECT column name, column name, FROM table name;
Example : Insert into table student 1 from the table student;
INSERT INTO student 1 SELECT Roll-No, Name, Address, Sex, City, Ph-No, State FROM
Student;
Insertion of a Data Set Into a Table from Another Table
Syntax : INSERT INTO table name
SELECT column name, column name FROM table name
WHERE Column = Expression;
Example: Insert records into the table student 1 from the table student where the field Roll-No
contains the value '115';

You might also like