View
1.048
Download
2
Category
Tags:
Preview:
DESCRIPTION
Citation preview
Er. Pradip Kharbuja
Topic 7 : SQL
SQL
Structured Query Language
SQL is a special-purpose programming language designed for managing data held in a Database Management System (DBMS).
Types of SQL Statements
1. Data Manipulation Language (DML)
2. Data Definition Language (DDL)
3. Data Control Language (DCL)
DML
DML is used for inserting, deleting and updating data in a database
DML is also used to retrieve and manipulate data in a database
DML modifies stored data not the database definition or database objects.
DML Statements are :
1. SELECT2. INSERT
3. UPDATE4. DELETE
5. COMMIT6. ROLLBACK
DDL
defines and controls the database structure
can add, change, or delete definitions of tables, fields and their data types
DDL Statements are :
1. CREATE
• table, database, index
2. ALTER
3. DROP
DCL
used to control access to data stored in a database.
DCL Statements
1. GRANT - to allow specified users to perform specified tasks like SELECT, INSERT, DELETE, CREATE, UPDATE, etc.
2. REVOKE - to cancel previously granted or denied permissions.
History of SQL
Developed from IBM’s SYSTEM R
It was developed as the standard database language by 1970s.
First standard published in 1987 by ISO based on work by ANSI
ISO - International Standards Organisation
ANSI – American National Standards Institute
History of SQL [Contd.]
Before 1987, SQL was developed by different vendors
Addition minor revisions in 1989
SQL: 1992 – Major revision ‘inSQL-92’
Introduced new data types like VARCHAR and new operators like UNION, JOIN and NATURAL JOIN
SQL: 1999 - with object-relational features
History of SQL [Contd.]
SQL: 2003 - introduced concept of ‘core SQL’
every implementation of core SQL gammar must meet.
main aim was to overcome the divergence in the language
SQL: 2006 – SQL to be used with XML
SQL: 2008 Minor revisions.
Data Manipulation Language : DML
For retrieving and updating data
SELECT – retrieving
INSERT, UPDATE, DELETE – updating
Difference between INSERT and UPDATE?
Literals
INSERT INTO workers (emp_no, first_name, last_name) VALUES (1, 'Ram', 'Shrestha');
Non-numeric in single quotes
Numeric NOT in quotes
INSERT
INSERT INTO workers (emp_no, first_name, last_name) VALUES (1, 'Ram', 'Shrestha');
INSERT INTO workers VALUES (1, 'Ram', 'Shrestha');
INSERT INTO workers (first_name, last_name) VALUES ('Lawerence', 'Smith');
UPDATE
UPDATE workers SET first_name = 'Emily';
UPDATE workers SET first_name = 'Emily' WHERE last_name = 'Smith';
UPDATE workers SET first_name = 'Emily' WHERE emp_no = 3;
DELETE
DELETE FROM workers;
DELETE FROM workers WHERE first_name = 'Lawerence';
DELETE FROM workers WHERE emp_no = 4;
Datatype
When we create a table or add a field to a table we specify the datatype.
Datatype is a classification that identifies the possible values for the field and operations that can be done on the data.
Datatype is also the way the data in that field is stored in the database.
Datatypes
Datatypes are much affected by different ‘flavours’ of SQL.
1. String
2. Number
3. Date
String Datatypes
1. CHAR - Fixed width character string. Maximum 8,000 characters
2. VARCHAR - Variable width character string. Maximum 8,000 characters
3. TEXT - Variable width character string. Maximum 2GB of text data
Char vs Varchar
Suppose 2 fields : one char 5 length another varchar 5 length
How these two will store 'Ram'?
In CHAR, Ram__
In VARCHAR, Ram
Numeric Datatypes
Integer, e.g. 8, -34, 0, 1000, -23
Numeric or Decimal, e.g. 8.23 with point set
Float or Real, e.g. 8.23 but could also be changed so that point moves when needed
Datetime Types
Date - System defined date
Time – System defind time
Timestamp – Dates and times including fractions of a second
Advantages of SQL
Universal
Easy to use
Fits (more or less) with relational model
Disadvantages of SQL
Does not support all features of relational model
No one standard and the different flavours in SQL
Has had to be extended
Much redundancy – possible to do the same thing many ways
ANY QUESTIONS?
Topic 07: SQL
Recommended