0% found this document useful (0 votes)
84 views27 pages

Lectuer-5 Database Design and Applications: BITS Pilani

This document provides an overview of SQL (Structured Query Language) concepts including: - SQL was designed at IBM labs to be a standard language for querying and managing data in relational database management systems. - Key SQL features include DDL, DML, triggers, and transaction management capabilities. - Common SQL statements like CREATE TABLE, INSERT, DELETE, UPDATE, and SELECT are discussed along with examples of how to use them to define tables, insert/modify data, and retrieve data with conditions, sorting, and other options.

Uploaded by

amit
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
84 views27 pages

Lectuer-5 Database Design and Applications: BITS Pilani

This document provides an overview of SQL (Structured Query Language) concepts including: - SQL was designed at IBM labs to be a standard language for querying and managing data in relational database management systems. - Key SQL features include DDL, DML, triggers, and transaction management capabilities. - Common SQL statements like CREATE TABLE, INSERT, DELETE, UPDATE, and SELECT are discussed along with examples of how to use them to define tables, insert/modify data, and retrieve data with conditions, sorting, and other options.

Uploaded by

amit
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 27

Lectuer-5

Database Design and


Applications
BITS Pilani Ashish Narang
Pilani Campus
BITS Pilani
Pilani Campus

First Semester

2020-21
Contents

• Introduction to SQL

• Features of SQL

• DDL Statements

• DML Statements

• Basic Retrieval Queries

BITS Pilani, Pilani Campus


Introduction to SQL

• SQL was initially called as SEQUEL.

• It was designed at IBM labs. //R systems

• Later on, acronym SEQUEL was changed to SQL.

• Popular commercial query language for relational databases.

• Joint effort of ISO and ANSI led to standardization of SQL.

• All relational DBMS like Oracle, MySQL, IBMs DB2 support


SQL.

BITS Pilani, Pilani Campus


Contd..

• SQL is a high-level declarative language to specify data


retrieval requests for data stored in relational databases.

• SQL is relationally complete.

• SQL is a standard and many vendors implement it in their own


way without deviating from the standard specifications.

BITS Pilani, Pilani Campus


Features of SQL

1. DDL (Data Definition Language) Set of commands to support creation,


deletion and modification of table structures and views.
2. DML (Data Manipulation Language) Set of commands to pose queries, insert
new tuples, and update/delete existing tuples.
3. Embedded SQL: Allows users to call SQL code from host languages like C, C++
& Java.
4. Triggers: Actions executed by the DBMS whenever changes to the database meet
specified conditions.
5. Transaction Management: to perform roll-back / commit actions.
6. Indexes: Indexes can be created to speed up the access to data stored in DB.

BITS Pilani, Pilani Campus


Create Table Command

It creates a new table and its columns.


Syntax:
CREATE TABLE table-name (
column-name datatype [column constraint]
[, column-name datatype [column constraint]
……….]
[,table constraint] ) ;

BITS Pilani, Pilani Campus


Contd..

CREATE TABLE EMP (


Fname VARCHAR(30) NOT NULL,
Minit VARCHAR(30),
Lname VARCHAR(30),
Ssn CHAR(9),
Bdate DATE,
Address VARCHAR (100),
Gender CHAR(1),
Salary DECIMAL(10,2), NOT NULL,
Super_ssn CHAR(9) NOT NULL,
Dno INTEGER NOT NULL
PRIMARY KEY(Ssn));

BITS Pilani, Pilani Campus


Specifying Constraints in SQL

• Basic constraints can be specified in SQL as part of table


creation.

• It includes

-Key constraints

-Referential integrity constraints

-Restrictions on attribute domains and NULLs

BITS Pilani, Pilani Campus


Specifying Attribute Constraints
and Attribute Defaults
• A constraint NOT NULL can be specified if NULL is not permitted
for a particular attribute.

• It is also possible to define a default value for an attribute by


appending the clause DEFAULT <value> to an attribute definition.
CREATE TABLE EMP
(...,
Dno INT NOT NULL DEFAULT 1,

BITS Pilani, Pilani Campus


Contd..

• Another type of constraint can restrict attribute or domain


values using CHECK clause following an attribute.
• Example
CREATE TABLE DEPARTMENT
(...,
Dno INT NOT NULL CHECK(Dno>0 AND
Dno<21);

BITS Pilani, Pilani Campus


Specifying Key and Referential
Integrity Constraints
• The PRIMARY KEY clause specifies one or more attributes
that make up the primary key of a relation. If a primary key
has single attribute, the clause can follow attribute directly.
• Example
CREATE TABLE DEPARTMENT
(...,
Dno INT Primary Key;

BITS Pilani, Pilani Campus


Contd..

• Referential integrity constraint is specified using FOREGIN KEY


clause.
• Example
CREATE TABLE EMPLOYEE
(...,
Dno INT NOT NULL,
FOREIGN KEY(Dno) REFERENCES
DEPARTMENT(Dnumber));

BITS Pilani, Pilani Campus


Contd..

Schema of a Company Database

BITS Pilani, Pilani Campus


Populated Company Database

BITS Pilani, Pilani Campus


Insert Command

• Insert is used to add a single tuple to a relation.


• We must specify relation name and list of values for the tuple.
• The values should be listed in the same order in which the
corresponding attributes were specified in the CREATE
TABLE command.
• Example
INSERT INTO EMPLOYEE
VALUES (‘Richard’,‘K’, ‘Marini’, ‘653298653’,
‘1962-12-30’, ‘98 Oak Forest, Kat,TX’,
‘M’, 37000, ‘653298653’,4);

BITS Pilani, Pilani Campus


Contd..

• Another way to write insert query.

INSERT INTO EMPLOYEE(Fname, Lname, Dno, Ssn)


Values (‘Richard’, ‘Marini’, 4,
‘653298653’)

BITS Pilani, Pilani Campus


Delete Command

• Delete is used to remove tuples from a relation.


• Depending on the number of tuples selected by the condition
in the Where clause, Zero, one or more tuples can be deleted
by single delete command
Example 1:
DELETE FROM EMPLOYEE
WHERE Lname=‘Brown’;
Example 2:
DELETE FROM EMPLOYEE
WHERE Dno=5;

BITS Pilani, Pilani Campus


Update Command

• Update command is used to modify attribute values of one or


more selected tuples.
• Where clause selects the tuples to be modified.
Example 1:
UPDATE EMPLOYEE
SET Lname=‘Brown’;
WHERE Ssn=‘123456789’;
Example 2:
UPDATE EMPLOYEE
SET Salary=salary*1.1
WHERE Dno=5;

BITS Pilani, Pilani Campus


Basic Queries in SQL

• Basic form of the SQL SELECT statement is called a mapping


or a SELECT-FROM-WHERE block

SELECT <attribute list>


FROM <table list>
WHERE <condition> ;

– <attribute list> is a list of attribute names whose values are to be


retrieved by the query
– <table list> is a list of the relation names required to process the
query
– <condition> is a conditional (Boolean) expression that identifies
the tuples to be retrieved by the query

BITS Pilani, Pilani Campus


USE OF *

• To retrieve all the attribute values of the selected tuples, a * is


used, which stands for all the attributes
Examples:

SELECT *
FROM EMP

BITS Pilani, Pilani Campus


UNSPECIFIED WHERE-Clause

• A missing WHERE-clause indicates no condition; hence, all


tuples of the relations in the FROM-clause are selected
– This is equivalent to the condition WHERE TRUE

• Query: Retrieve the Ssn values for all employees.


SELECT Ssn
FROM EMP ;

BITS Pilani, Pilani Campus


Simple SQL Queries

• Example of a simple query on one relation

Query: Retrieve the birthdate and Address of the employee


whose name is 'John B. Smith'.

SELECT Bdate, Address


FROM EMPLOYEE
WHERE Fname='John' AND Minit='B’
AND Lname='Smith’ ;

BITS Pilani, Pilani Campus


USE OF DISTINCT

• SQL does not treat a relation as a set; duplicate tuples can


appear
• To eliminate duplicate tuples in a query result, the keyword
DISTINCT is used
• For example, the result of Q1 may have duplicate Salary
values whereas Q1A does not have any duplicate values

Q1: SELECT Salary


FROM EMP ;

Q1A: SELECT DISTINCT Salary


FROM EMP ;

BITS Pilani, Pilani Campus


USE OF LIKE

• LIKE operator can be used for string pattern matching.


Example 1: Retrieve all employees whose address is in
Houston, Texas.
SELECT Fname,Lname
FROM EMP
WHERE Address like ‘%Houston,TX%’;
Example 2: Find all employees who were born during 1950
SELECT Fname,Lname
FROM EMP
WHERE Bdate like ‘_ _ 5 _ _ _ _ _ _ _’;

BITS Pilani, Pilani Campus


USE OF BETWEEN

• BETWEEN operator can be used to check a value in a interval.


Example: Retrieve all employees in department 5 whose salary
is between 30,000 and 40,000.
SELECT *
FROM EMP
WHERE (salary BETWEEN 30000 AND 40000)
AND Dno=5;

BITS Pilani, Pilani Campus


USE OF ORDER BY

• SQL allows the user to order the tuples in a result of a query


by the values of one or more attributes that appear in query
result.
Example: Retrieve all employees in department 5 order by first
name
SELECT *
FROM EMP
WHERE Dno=5
ORDER BY Fname;
• DESC clause can be used to reverse the order

BITS Pilani, Pilani Campus

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy