SQL
SQL
SQL
It has 9 commands common to all RDBMS
CREATE, DROP, ALTER for Tables. INSERT, UPDATE, DELETE for records. GRANT, REVOKE for permission. SELECT for query.
(American National Standard Institute). Also pronounced as SEQUEL. It is command language for communication with ORACLE/MSSQL Server. When an SQL statement is entered it is stored in the part of memory called SQL Buffer and remains there until a new SQL statement is entered.
Features of SQL
It can be used by a range of users including
those with little or no programming experience. It is a non-procedural language. It reduces the amount of time required for creating and maintaining system. It is English like language
Rules of SQL
SQL statements starts with a verb (SQL action word)
eg. SELECT. Each verb is followed by number of clauses eg. FROM, WHERE, HAVING. A space separates clause eg. DROP TABLE. Comma (,)separates parameters without clause. A semicolon (;) is used to end SQL statement. Characters and date literals must be enclosed in single quotes. Numeric values can be represented by simple values 0.39, -34, 01991. Scientific notations 2E5 = 2 * 105
SQL Delimiters
Addition + Exponential ** Subtraction Concatenation || Multiplication * Comment -- , /**/ Division / Relational =,>,< Expression or List () Terminator ; Item Separator , Character String delimiter Quote Identifier
Components of SQL
DDL (Data Definition Language) CREATE, ALTER, DROP, TRUNCATE, COMMENT. DML (Data Manipulation Language) INSERT, UPDATE, DELETE, CALL, LOCK TABLE DCL (Data Control Language) COMMIT- Save the work. SAVE POINT Identify a point in a transaction to which you can later on rollback. ROLLBACK restore database to the original since the last COMMIT. SET TRANSACTION Change transaction option like what rollback segment to use. GRANT/ REVOKE Grant or take back permission to or from the users.
DQL (Data Query Language) SELECT retrieve data from the database
Data types
CHAR (size) store characters maximum upto 255. VARCHAR (size) / VARCHAR2(size) store
alphanumeric data maximum upto 2000 / 4000. DATE To represent Date and Time DD-MM-YY. NUMBER(P,S) Precision (P) maximum length of data , Scale (S) determines number of places to the right of decimal. LONG Variable length character strings containing upto 2 GB. CLOB/ BLOB Character Large Objects/ Binary Large objects to store images, pictures, sounds, video, txt files etc.
CREATE
Syntax CREATE TABLE <Table name>
INSERT
Syntax INSERT into Table (<column1>,
<column2>) values (data1 , data2); INSERT into Table_name VALUES ( value1, value2,value3,..) Example:
insert into Bank_sys (Branch_no, name) values ('1018' , 'Pune');
EXAMPLE
insert into Bank_sys values (1018 , 'Pune');
('1018' , 'Pune');
SELECT
Syntax SELECT * from Tablename; Example:
Example of select
P_Id 1 2 3 LastName Hansen Svendson Pettersen FirstName Ola Tove Kari Address Timoteivn 10 Borgvn 23 Storgt 20 City Sandnes Sandnes Stavanger
Persons table We want to select the content of the columns named last name and first name From the persons table Queries :- Select LastName, FirstName from persons
LastName
Hansen Svendson Pettersen
FirstName
Ola Tove Kari
Now we want to select only distinct values from the column named City From the table persons Queries:- Select Distinct City from Persons
City
Sandnes Stavanger
P_Id 1 2
select * from Bank_sys order by name ; select * from Bank_sys order by name desc; select * from Bank_sys order by name asc;
Svendson
Tove
Borgvn 23
Sandnes
P_Id 2 3 4 1
condition and second condition is true. OR operator displays a record if either the first condition or the second condition true.
OR Operator Example
SELECT * FROM Persons WHERE FirstName='Tove OR FirstName='Ola'
P_Id 1 2 LastName Hansen Svendson FirstName Ola Tove Address Timoteivn 10 Borgvn 23 City Sandnes Sandnes
P_Id 2
LastName Svendson
FirstName Tove
Address Borgvn 23
City Sandnes
DELETE
Syntax DELETE FROM <TableName> Example:
UPDATE table
Syntax UPDATE <TableName> SET
Example
Update persons table
P_Id 1 2 3 4 5 LastName gupta sharma suri gupta bedi FirstName sumit gitika neena rohit kiran Address 54, ashok vihar 1234/8, rohini 678/14, gurgaon 126/21, noida City Delhi Delhi Haryana Uttar pradesh
Query:Update persons Set Address= 54/9 Ashoka appartment, rohini, City= delhi Where lastname = bedi and firstname= kiran
output
P_Id 1 LastName gupta FirstName sumit Address 54, ashok vihar 1234/8, rohini 678/14, gurgaon 126/21, noida City Delhi
2 3
sharma suri
gitika neena
Delhi Haryana
gupta
rohit
Uttar pradesh
bedi
kiran
Delhi
ALTER Table
Syntax ALTER Table <Tablename> ADD
Problem
Create table
Create the following table
Client- client_no, name, Address, City, State. Product_master- p_no, pname, sellprice, costprice. Sales_Master- salesman_name, salesman_no, address, city, state, remarks, salary.
Query
Find out names of all the clients. Retrieve the entire content of the client table. Retrieve the list of name, city and state of all
the clients. Find the name of salesman who have salary equal to 3000 Rupees. Find the name of clients who are located in Mumbai.
Example
Display all details of student.
r. List details of student who having highest marks in class. List details of student whose name that have A and M. Display name and marks of student.