lec-4 queries in database

14
QUERIES IN DATABASE Lecture 4

Upload: nohel01

Post on 02-Apr-2018

221 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Lec-4 Queries in Database

7/27/2019 Lec-4 Queries in Database

http://slidepdf.com/reader/full/lec-4-queries-in-database 1/14

QUERIES IN DATABASE

Lecture 4

Page 2: Lec-4 Queries in Database

7/27/2019 Lec-4 Queries in Database

http://slidepdf.com/reader/full/lec-4-queries-in-database 2/14

Queries

The information can be obtained from the

Database by asking some direct questions

known as Queries. Such questions involves the

data stored in the DBMS. e.g. What are theName of all students stored in Database

Student?

DBMS provides a specialized language, called

the Query Language in which queries can be

posed.

Page 3: Lec-4 Queries in Database

7/27/2019 Lec-4 Queries in Database

http://slidepdf.com/reader/full/lec-4-queries-in-database 3/14

Database Languages

• DDL – Data Definition Language

• SDL – Storage Definition Language

• VDL – View Definition Language

• DML – Data Manipulation Language

(For data manipulations like

insertion, deletion, update,retrieval, etc.)

• DCL - Data Control language.

Page 4: Lec-4 Queries in Database

7/27/2019 Lec-4 Queries in Database

http://slidepdf.com/reader/full/lec-4-queries-in-database 4/14

Database Languages and Interfaces 

Data Definition Language (DDL) Used by the DBA and database designers to

specify the conceptual schema of a database. In many DBMSs, the DDL is also

used to define internal and external schemas (views). In some DBMSs, separate

storage definition language (SDL) and view definition language (VDL) are

used to define internal and external schemas.In General storage definition

language (SDL) and view definition language (VDL) are the part of DDL.

Data Manipulation Language (DML) Used to specify database retrievals and

updates (insertion, deletion, modifications)

- DML commands (data sublanguage) can be embedded in a general-purpose

 programming language (host language).

- Alternatively, stand-alone DML commands can be applied directly (query

language).

 provide appropriate languages and interfaces for each category of users.

DBMS Languages

Page 5: Lec-4 Queries in Database

7/27/2019 Lec-4 Queries in Database

http://slidepdf.com/reader/full/lec-4-queries-in-database 5/14

Queries

• Find all courses that “Mary” takes 

• What happens behind the scene ?• Query processor figures out how to answer

the query efficiently.

SELECT C.name

FROM Students S, Takes T, Courses CWHERE S.name=“Mary” andS.ssn = T.ssn and T.cid = C.cid

Page 6: Lec-4 Queries in Database

7/27/2019 Lec-4 Queries in Database

http://slidepdf.com/reader/full/lec-4-queries-in-database 6/14

Queries, behind the scene

 Imperative query execution plan: SELECT C.name

FROM Students S, Takes T, Courses C

WHERE S.name=“Mary” and

S.ssn = T.ssn and T.cid = C.cid

Declarative SQL query  

Students Takes

sid=sid

sname

name=“Mary”

cid=cid

Courses

The optimizer chooses the best execution plan for a query

Page 7: Lec-4 Queries in Database

7/27/2019 Lec-4 Queries in Database

http://slidepdf.com/reader/full/lec-4-queries-in-database 7/14

Data Definition Language (DDL)

• is a computer language for defining data structures.

• The term was first introduced in relation to theCodasyl database model, where the schema of thedatabase was written in a Data Definition Languagedescribing the records, fields, and "sets" making upthe user Data Model.

• Initially it referred to a subset of SQL, but is now

used in a generic sense to refer to any formallanguage for describing data or informationstructures, like XML schemas.

Page 8: Lec-4 Queries in Database

7/27/2019 Lec-4 Queries in Database

http://slidepdf.com/reader/full/lec-4-queries-in-database 8/14

Data Definition Language (DDL)• CREATE statements

Create - To make a new database, table, index, or stored query. A CREATEstatement in SQL creates an object inside of a relational databasemanagement system (RDBMS).

• CREATE TABLECREATE [TEMPORARY] TABLE [table name] ( [column definitions] ) [table parameters].

• Column Definitions: A comma-separated list consisting of any of thefollowingColumn definition: [column name] [data type] {NULL | NOT NULL} {columnoptions} 

• Primary key definition: PRIMARY KEY ( [comma separated column list] )

• CONSTRAINTS: {CONSTRAINT} [constraint definition] 

• For example, the command to create a table named employees with a fewsample columns would be: CREATE TABLE employees ( id INTEGER PRIMARY KEY, first_nameCHAR(50) NOT NULL, last_name CHAR(75) NULL, dateofbirth DATE NOT NULL );

Page 9: Lec-4 Queries in Database

7/27/2019 Lec-4 Queries in Database

http://slidepdf.com/reader/full/lec-4-queries-in-database 9/14

Data Definition Language (DDL)

• DROP statementsDrop - To destroy an existing database, table, index, or view.

• A DROP statement in SQL removes an object from a relationaldatabase management system (RDBMS).

• The typical usage is simply DROP objecttype objectname.For example, the command to drop a table named employees wouldbe: DROP TABLE employees;

• The DROP statement is distinct from the DELETE and (non-standard)

TRUNCATE statements, in that they do not remove the table itself.For example, a DELETE statement might delete some (or all) datafrom a table while leaving the table itself in the database, whereas aDROP statement would remove the entire table from the database.

Page 10: Lec-4 Queries in Database

7/27/2019 Lec-4 Queries in Database

http://slidepdf.com/reader/full/lec-4-queries-in-database 10/14

Data Definition Language (DDL)

• ALTER statementsAlter - To modify an existing database object.

• An ALTER statement in SQL changes the properties of an objectinside of a relational database management system (RDBMS).

• The typical usage is ALTER objecttype objectname  parameters.For example, the command to add (then remove) a column namedbubbles for an existing table named sink would be: 

ALTER  TABLE sink ADD bubbles INTEGER;ALTER  TABLE sink DROP COLUMN bubbles;

Page 11: Lec-4 Queries in Database

7/27/2019 Lec-4 Queries in Database

http://slidepdf.com/reader/full/lec-4-queries-in-database 11/14

Data Manipulation Language(DML)

• Data Manipulation Language (DML) is a family of computerlanguages used by computer programs and/or database users toinsert, delete and update data in a database. Read-only querying, i.e.SELECT, of this data may be considered to be either part of DML oroutside it, depending on the context.

• SELECT ... INTO • INSERT • UPDATE • DELETE 

Page 12: Lec-4 Queries in Database

7/27/2019 Lec-4 Queries in Database

http://slidepdf.com/reader/full/lec-4-queries-in-database 12/14

Types of DML

-Procedural DML:

• Also called record-at-a-time (record-oriented) or low-level DML

• Must be embedded in a programming language.

• Searches for and retrieves individual database records and uses looping

and other constructs of the host programming language to retrieve multiple

records.

-Declarative or non-procedural DML:• Also called set-at-a-time (set-oriented) or high-level DML.

• Can be used as a stand-alone query language or can be embedded in a

 programming language.

• Searches for and retrieves information from multiple related database

records in a single command.

- host language: general-purpose language

- data sublanguage: DML

Page 13: Lec-4 Queries in Database

7/27/2019 Lec-4 Queries in Database

http://slidepdf.com/reader/full/lec-4-queries-in-database 13/14

DML

• High-level DML: set-oriented, declarative

• Low-level DML: record-oriented, procedural

• Types of DML• data sublangauge: DML embedded in a general

purpose language (for DBAs)

• query language: high-level, interactive, stand-

alone DML (casual end users)

• user-friendly interface for DML (naïve users)

Page 14: Lec-4 Queries in Database

7/27/2019 Lec-4 Queries in Database

http://slidepdf.com/reader/full/lec-4-queries-in-database 14/14

Data Control Language (DCL)

DCL Commands are used to control the access to

the Database.

• GRANT - gives user's access privileges todatabase

• REVOKE - withdraw access privileges given with

the GRANT command