topic 07 : sql

22
Er. Pradip Kharbuja Topic 7 : SQL

Upload: pradip-kharbuja

Post on 18-Jan-2015

1.060 views

Category:

Education


2 download

DESCRIPTION

 

TRANSCRIPT

Page 1: Topic 07 : SQL

Er. Pradip Kharbuja

Topic 7 : SQL

Page 2: Topic 07 : 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)

Page 3: Topic 07 : SQL

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

Page 4: Topic 07 : SQL

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

Page 5: Topic 07 : SQL

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.

Page 6: Topic 07 : SQL

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

Page 7: Topic 07 : SQL

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

Page 8: Topic 07 : SQL

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.

Page 9: Topic 07 : SQL

Data Manipulation Language : DML

For retrieving and updating data

SELECT – retrieving

INSERT, UPDATE, DELETE – updating

Difference between INSERT and UPDATE?

Page 10: Topic 07 : SQL

Literals

INSERT INTO workers (emp_no, first_name, last_name) VALUES (1, 'Ram', 'Shrestha');

Non-numeric in single quotes

Numeric NOT in quotes

Page 11: Topic 07 : SQL

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');

Page 12: Topic 07 : SQL

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;

Page 13: Topic 07 : SQL

DELETE

DELETE FROM workers;

DELETE FROM workers WHERE first_name = 'Lawerence';

DELETE FROM workers WHERE emp_no = 4;

Page 14: Topic 07 : SQL

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.

Page 15: Topic 07 : SQL

Datatypes

Datatypes are much affected by different ‘flavours’ of SQL.

1. String

2. Number

3. Date

Page 16: Topic 07 : SQL

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

Page 17: Topic 07 : SQL

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

Page 18: Topic 07 : SQL

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

Page 19: Topic 07 : SQL

Datetime Types

Date - System defined date

Time – System defind time

Timestamp – Dates and times including fractions of a second

Page 20: Topic 07 : SQL

Advantages of SQL

Universal

Easy to use

Fits (more or less) with relational model

Page 21: Topic 07 : SQL

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

Page 22: Topic 07 : SQL

ANY QUESTIONS?

Topic 07: SQL