2 INTRODUCTIONStructured Query Language (SQL) is the standard command set that allows the users to interact with the relational database management systems. All task related to relational data management – creating tables, querying the database for information, modifying the data in database ,deleting them ,granting access to users and so on-can be easily performed using SQL.
3 ADVANTAGESSQL is coded with embedded data . Programmer needs to simply specify what data is required and not how to retrieve it unlike procedural language.Applications written in SQL can be easily ported across system.SQL as a language provides a greater degree of abstraction and is independent of the way it is implemented internally. A query return same result whether optimizing has been done with indexes or not.
4 Language is simple , easy to learn ,can also handle complex situations. Expected results are well defined and language has sound theoretical base and there is no ambiguity about the way a query will interrupt the data and produce the result.SQL can also be used to define data structures , control access to the data , delete , insert and modify occurence of the data.
5 CHARACTERISTICSSQL has rules for embedding SQL statements into a general purpose programming language such as java or COBOL.SQL is a non procedural language .SQL is not case sensitive.SQL has statements for data definition, query and update. Hence it is both a DDL and a DML.SQL usage by its very nature is extremely flexible. There is no need to start SQL statements in a particular column or be finished in a single line.
7 SQL COMMANDSSQL statements can broadly be classified into the following categories :-Data Definition Language (DDL):- The commands used are CREATE, ALTER and DROP. The data definition statements are CREATE TABLE, CREATE VIEW, CREATE INDEX, ALTER TABLE , DROP TABLE , DROP VIEW and DROP INDEX.Data Manipulation Language (DML):- SQL provides three data manipulation statements –INSERT, UPDATE , DELETE.
8 Data Query Language (DQL):- SQL has only one data query statement ,i.e. SELECT. It enables the users to query one or more tables to get the desired information.Data Control Language (DCL):- The DBA has the authority to give and take the privileges to a specific user. The DCL commands are GRANT and REVOKE.
9 Data Administration Statements (DAS):- Two data administration commands are START AUDIT and STOP AUDIT. Note that data administration is different from database administration .Database administration is the overall administration of the database and data administration is only a subset of that.Transaction Control Statements (TCS):- Some of the TCS are COMMIT , ROLLBACK , SAVEPOINT and SET TRANSACTION.
10 DATA TYPESChar (n):- A fixed character length string with user specified length.Varchar (n):- A variable character length string with user specified maximum length n.Int :- An integer.Small Integer :- A small integer.Numeric (p, d):- A fixed point number with user defined precision.Real , double precision:- Floating point and double precision floating point numbers with machine dependent precision.
11 Float (n) :- A floating point number, with precision of at least n digits. Date :- A calendar date containing a ( four digits) year , month and day of the month.Time:- The time of day , in hours, minutes and seconds e.g. Time ’09:30:00’.Number:-Number is used to store numbers (fixed or floating point).
12 Creation Of A Table Create table Client_master ( client_no varchar (06) ,name varchar (20),city varchar (15),pincode number (06),bal_due number (10,2)); desc client_master /*to show all the columns*/
13 Inserting ValuesInsert into client_master values (‘0001’,’Amit’,’Mumbai’,400054,1500);insert into client_master values (‘0002’,’Vandana’, ‘Chennai’,780001,0);RETRIEVING OF DATA FROM TABLE :-SELECT * from client_master; /* to show all the rows of a table*/
14 Client_master Bal_due Client_no Name City Pincode Amit Mumbai 400054 150000001780001ChennaiVandana00025000400057MumbaiPramada0003400056Mumbai0004Basu1000012000Delhi0005RaviMumbai4000500006Rupa
15 QUERIES Find out the names of all the clients. SQL> SELECT name from client_master;List all the clients who are located in ‘Mumbai’.SQL> SELECT *2 from client_master where3 city= ‘Mumbai’ ; ORfrom client_master wherecity in (‘Mumbai’) ; OR
16 SQL> SELECT *from client_master wherecity like ‘M%’ ;Display the information for client no 0001 and 0002.client_no =0001 OR client_no=0002;
17 Delete the record with client 0001 from the client_master table. SQL> DELETE from client_masterwhere client_no=0001;Change the city of client_no 0005 to Mumbai.SQL> UPDATE client_masterSET city=‘Mumbai’ whereclient_no=0005;
18 Sorting of data in table:- SQL> SELECT *from client_masterORDER BY name;Grouping while selecting :-GROUP BY city;
19 AGGREGATE FUNCTIONSCOUNT (*):- produces the number of rows on non NULL field values that the query selected. Ex:-SQL> SELECT COUNT (*)from client_master;SUM():- produces the arithmetic sum of all selected values of a given field. Ex:-SQL> SELECT SUM (bal_due)AVG():- produces the average (mean) of all selected values of a given field. Ex:-
20 SQL> SELECT AVG (bal_due) from client_master;MAX():- produces the largest of all selected values of a given field.MIN():- produces the smallest of all selected values of a given field. Ex:-SQL> SELECT MAX (bal_due),MIN (bal_due) fromclient_master;
21 STRUCTURE OF TABLE PRIMARY KEY:- SQL> ALTER TABLE client_master 2 ADD PRIMARY KEY (client_no);FOREIGN KEY:-SQL> ALTER TABLE departmentFOREIGN KEY (dept_no)references client_master;Adding new columns:-ADD ( address varchar(20));UNIQUE:-2 MODIFY ( pincode varchar(06));
22 NOT NULL:-SQL> ALTER TABLE client_masterMODIFY ( client_no varchar(06));DROP STATEMENT:-SQL> DROP TABLE client_master;Modifying existing table:-2 MODIFY ( client_no varchar(10)); CHECK:-SQL> ALTER TABLE studentADD CHECK (marks>50);
24 SET OPERATIONS Union:- SQL> SELECT name, city from student UNION SELECT sname,city from head;Difference:-SQL> SELECT name from studentMINUS SELECT from head;Intersect:-SQL> SELECT name fromstudent INTERSECT SELECT sname fromhead;
25 VIEWYou can present logical subsets or combinations of data by creating views of tables . A view is a logical table based on a table or another view. A view contains no data of its own but is like a window through which data from tables can be viewed or changed. The view is stored as a SELECT statement in the data dictionary.Uses:-:- To restrict data access.:- To make complex queries easy.:- To provide data independence.:- To present different views of the same data.
26 SYNTAX:-CREATE VIEW viewname asSELECT columnname, columnnamefrom tablename;Ex:-SQL> CREATE VIEW c_viewSELECT name, cityfrom client_master;Removing a View:-SQL> DROP VIEW c_view ;
27 INDEXESAn index is an ordered list of a column or group of columns in a table. An index created on the single column of the table is called simple index. When multiple table columns are included in the index it is called composite index.SYNTAX(SIMPLE):-CREATE INDEX index_nameON table name (column name);Ex:-SQL> CREATE INDEX client_indexON client_master ( pincode);
28 Composite Index:-CREATE INDEX index_nameON table name (column name, columnname);Ex:-SQL> CREATE INDEX client_cindexON client_master (client_no, name);Unique Index:-Ex:-SQL> CREATE UNIQUESTION INDEX c_indexON client_master (state);
29 Dropping Indexes:- An index can be dropped by using DROP INDEX. SYNTAX:-DROP INDEX indexname;Ex:-SQL> DROP INDEX client_index;