relational database is called Structured Query Language. SQL belongs to the category of fourth Generation Language (4GL). SQL • SQL is not a product. It is a standard language that many products use such as DB2, Oracle, Ingres, Informix, Sybase, SQL Server, and MS-Access have adopted.
• These products are called Relational Data Base
Management Systems. • We tell DBMS What to do. • DBMS decides How to do. SQL (DDL,DML,DCL,TCL)
SQL is concerned with three most common
tasks, needed by any user of a data base management system, namely: Data Definition Language DDL, Data Manipulation Language DML, and Data Control Language DCL. DBMS Data Definition Language (DDL) • Data Definition Language is used to define the database structure or schema. • These statements define the implementation details of the database schema, which are usually hidden from the users. • It simply deals with descriptions of the database schema and is used to create and modify the structure of database objects in database. DDL • CREATE – is used to create the database or its objects (like table, index, views, store procedure and triggers). • DROP – is used to delete objects from the database. • ALTER-is used to alter the structure of the database. • TRUNCATE–is used to remove all records from a table, including all spaces allocated for the records are removed. • COMMENT –is used to add comments to the data dictionary. • RENAME –is used to rename an object existing in the database. DDL • Create table/ Add, update, View.
CREATE TABLE Book
( id INTEGER Title CHAR(30), Author CHAR (20), Publisher CHAR (30), Subject CHAR (20), ) We are telling the DBMS that create a table called Book, in which there are five columns as specified. The DBMS stores this information in a special area called DB Catalog. DML (Data Manipulation Language) • DML statements are used for managing data with in schema object. • SELECT – is used to retrieve data from a database. • INSERT – is used to insert data into a table. • UPDATE – is used to update existing data within a table. • DELETE – is used to delete records from a database table. CRUD Operations/ SQL Queries • C: CREATE • R: Read (SELECT) • U: UPDATE • D: DELETE DCL (Data Control Language) • DCL includes commands such as GRANT and REVOKE which mainly deals with the rights, permissions and other controls of the database system. • GRANT-gives user’s access privileges to database. allow specified users to perform specified tasks. • REVOKE-withdraw user’s access privileges given by using the GRANT command. cancel previously granted or denied permissions. TCL (transaction Control Language) • TCL commands deals with the transaction within the database. • COMMIT– commits a Transaction (Save work done). • ROLLBACK– rollbacks a transaction in case of any error occurs. Restore database to original since the last COMMIT. • SAVEPOINT–sets a save point within a transaction. • SET TRANSACTION–specify characteristics for the transaction. Student Table Name Math Science Language Total Ave Result Ali 68 90 72 Mudasir 95 92 80 Zaigham 29 67 41 Hussnain 45 60 71 Subtain 77 82 64 Hamza 55 76 67 Asad 59 52 41 Moazam 81 56 33 zain 32 31 36 READ/ SELECT • SELECT * FROM Student
SELECT: informs the RDBMS that I want to select (
retrieve) something from student table. * indicates that I want to select all information from the student table. FROM: specifies that the table name would follow now. Student: this is the name of the table from which I want to retrieve data. READ/ SELECT • SELECT * FROM Students WHERE Name = ‘Moazam’ Name Math Science Language Total Ave Result Moazam 81 56 33
• SELECT Name FROM Student WHERE Science
= ‘82’ Name Subtain Drop • If we need to delete this table in the future, a single line will do the job.
DROP TABLE Student
INSERT • INSERT statement will insert one row at a time.
• INSERT INTO Student (Name, Math, Science,
Language) (‘Saad’, 68, 90, 70) Name Math Science Language Total Ave Result Ali 68 90 72 230 Mudasir 95 92 80 267 Zaigham 29 67 41 137 Hussnain 45 60 71 . Subtain 77 82 64 . Hamza 55 76 67 . Asad 59 52 41 . Moazam 81 56 33 . zain 32 31 36 99 Saad 68 90 70 228 UPDATE • The UPDATE statement is used to modify the existing records in a table.
UPDATE Student SET Total = Math + Science +
Language UPDATE
Name Math Science Language Total Ave Result
Ali 68 90 72 230 Mudasir 95 92 80 267 Zaigham 29 67 41 137 Hussnain 45 60 71 . Subtain 77 82 64 . Hamza 55 76 67 . Asad 59 52 41 . Moazam 81 56 33 . zain 32 31 36 99 Saad 68 90 70 UPDATE • UPDATE Student SET Average = Total/3 Name Math Science Language Total Ave Result Ali 68 90 72 230 76.66 Mudasir 95 92 80 267 89.00 Zaigham 29 67 41 137 45.66 Hussnain 45 60 71 . . Subtain 77 82 64 . . Hamza 55 76 67 . . Asad 59 52 41 . . Moazam 81 56 33 . . zain 32 31 36 99 33 UPDATE UPDATE Student SET Result = ‘Pass’ WHERE Math > 34 AND Science > 34 AND Language > 34. Name Math Science Language Total Ave Result Ali 68 90 72 230 76.66 Pass Mudasir 95 92 80 267 89.00 Pass Zaigham 29 67 41 137 45.66 Hussnain 45 60 71 . . Pass Subtain 77 82 64 . . Pass Hamza 55 76 67 . . Pass Asad 59 52 41 . . Pass Moazam 81 56 33 . . zain 32 31 36 99 33 DELETE DELETE FROM Student WHERE result = ‘ Fail’