Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 14

QUERIES IN DATABASE

Lecture 4

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 the Name of all students stored in Database Student? DBMS provides a specialized language, called the Query Language in which queries can be posed.

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.

Database Languages and Interfaces


provide appropriate languages and interfaces for each category of users.

DBMS Languages
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).

Queries
Find all courses that Mary takes SELECT C.name FROM Students S, Takes T, Courses C WHERE S.name=Mary and S.ssn = T.ssn and T.cid = C.cid
What happens behind the scene ?
Query processor figures out how to answer the query efficiently.

Queries, behind the scene


Declarative SQL query Imperative query execution plan:
sname

SELECT C.name FROM Students S, Takes T, Courses C WHERE S.name=Mary and S.ssn = T.ssn and T.cid = C.cid
sid=sid name=Mary

cid=cid

Students

Takes

Courses

The optimizer chooses the best execution plan for a query

Data Definition Language (DDL)


is a computer language for defining data structures. The term was first introduced in relation to the Codasyl database model, where the schema of the database was written in a Data Definition Language describing the records, fields, and "sets" making up the user Data Model. Initially it referred to a subset of SQL, but is now used in a generic sense to refer to any formal language for describing data or information structures, like XML schemas.

Data Definition Language (DDL)


CREATE statements Create - To make a new database, table, index, or stored query. A CREATE statement in SQL creates an object inside of a relational database management system (RDBMS). CREATE TABLE CREATE [TEMPORARY] TABLE [table name] ( [column definitions] ) [table parameters]. Column Definitions: A comma-separated list consisting of any of the following Column definition: [column name] [data type] {NULL | NOT NULL} {column options} 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 few sample columns would be: CREATE TABLE employees ( id INTEGER PRIMARY KEY, first_name CHAR(50) NOT NULL, last_name CHAR(75) NULL, dateofbirth DATE NOT NULL );

Data Definition Language (DDL)


DROP statements Drop - To destroy an existing database, table, index, or view.
A DROP statement in SQL removes an object from a relational database management system (RDBMS).

The typical usage is simply DROP objecttype objectname. For example, the command to drop a table named employees would be: 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) data from a table while leaving the table itself in the database, whereas a DROP statement would remove the entire table from the database.

Data Definition Language (DDL)


ALTER statements Alter - To modify an existing database object. An ALTER statement in SQL changes the properties of an object inside 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 named bubbles for an existing table named sink would be: ALTER TABLE sink ADD bubbles INTEGER; ALTER TABLE sink DROP COLUMN bubbles;

Data Manipulation Language (DML)


Data Manipulation Language (DML) is a family of computer languages used by computer programs and/or database users to insert, 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 or outside it, depending on the context. SELECT ... INTO INSERT UPDATE DELETE

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

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, standalone DML (casual end users) user-friendly interface for DML (nave users)

Data Control Language (DCL)


DCL Commands are used to control the access to the Database. GRANT - gives user's access privileges to database REVOKE - withdraw access privileges given with the GRANT command

You might also like