Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
19 views

7.Data Manipulation Using SQL

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views

7.Data Manipulation Using SQL

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 25

• Data manipulation using SQL

DDL
DML
Introduction to data manipulation with SQL
• The name SQL is derived from Structured Query Language.
• SQL is now the standard language for commercial relational
DBMSs.

• Both as a DDL and DML language.


DDL (Data Definition Language): define the schema of the
database.
DML (Data Manipulation Language): provides commands
to manipulate the database (query, insert, update, delete).
Introduction to data manipulation with SQL
• Based on relational algebra, but not entirely identical.
Relations  Tables
Tuples  Rows
Attributes  Columns
• Duplicates are not automatically removed.
This is for practical reasons. Duplicate eliminations are
inefficient in implementation.
• The order of rows in a table is irrelevant.
Basic DDL Commands in SQL
• CREATE: to define new tables (to define relation schemas)
• DROP: to delete table definitions (to delete relation schemas)
• ALTER: to change the definitions of existing tables (to change
relation schema)
• Other features as DDL
Specify referential integrity constraints (FKs)
Specify user-defined attributes constraints
Basic DML Commands in SQL
• INSERT: to add new rows to table
• UPDATE: to change the “state” (the value) of rows.
• DELETE: to remove rows
• SELECT: a query command that uses relation algebra like
expressions
SQL Commands Are Sequential
• Commands are executed in the order they are encountered.
• DDL commands are not like C/Java declarations.
• DDL and DML commands can be mixed
For example, you can define a table, fill it up with contents,
and delete a columns.
That is, table definitions (relation schema) can be changed
during the lifespan of a database.
• The ability of doing so does imply it is a good practice.
• It is best the schema/design of a database is well
thought through before its use
Create talbe
CREATE TABLE DEPARTMENT (
Dname VARCHAR(10) NOT NULL,
Dnumber INTEGER Default 0,
Mgr_ssn CHAR(9),
Mgr_Sartdate CHAR(9),
PRIMARY KEY (Dnumber),
UNIQUE (Dname),
FOREIGN KEY (Mgr_ssn)
REFERENCES EMPLOYEE (Ssn));
Statements
Simple Retrieval
SELECT ENAME FROM EMPLOYEE;

Inserting tuples
INSERT INTO S VALUES
(‘S1’,’GEMUNU’,20,’COLOMBO’)

Removing Duplications
SELECT DISTINCT ENAME FROM EMPLOYEE;

Not null
SELECT * FROM WHERE IS NOT NULL;
Statements
• An ALTER command must be used to change the schema of
EMPLOYEE, after the “create table DEPARTMENT,” to add a
FK.

alter table EMPLOYEE


Substring pattern matching
Get all employees whose name has ‘A’ as its second character.

SELECT ENAME
FROM EMPLOYEES
WHERE ENAME LIKE ‘_A%’;

% - replace an arbitrary number of zero or more characters.


- -replace a single character
Rename of attributes

SELECT ENAME AS EMP_NAME


FROM EMPLOYEE
WHERE ENAME LIKE ‘_A%’;

The new name EMP_NAME will appear as column header in the


query result.
General form of SELECT
SELECT [DISTINCT] attributes

FROM tables

[WHERE condition]

[GROUP BY attributes]

[HAVING condition]

[ORDER BY attributes]
Retrieval with ordering
Select employees number and city where employees in Kandy, in
descending order of the status.
SELECT S#, CITY
FROM
WHERE CITY =‘KANDY’
ORDER BY CITY DESC;

DESC – if we want the result in descending order.


ASC – if we want the result in ascending order, this is optional.
Constrains in SQL
• Basic constrains can be applied in SQL as a part of the table
creations.
Key and referential integrity constraints
Restrictions on attributes domain(Domain constraints)
Constrains on individual tuples with in a relation.(Entity
integrity constrains)
Constrains in SQL
• A constraints NOT NULL can be specified if NULL is not
permitted for a particular attribute.
• Can be define a default value for a n attribute by appending a
clause DEFAULT <value>.
• Can restrict the attribute or domain values using the CHECK
clause following an attribute or domain definition.
Constrains in SQL
STATUS NUMBER NOT NULL CHECK (STATUS>0 AND STATUS<50);

CHECK DOMIAN sup_status AS NUMBER CHECK (sup_status >0


AND sup_status<50);
Constrains in SQL
CREATE TABLE SPJ
(S# char(2) NOT NULL;
P# char(2) NOT NULL DEFAULT ‘P1’
J# char(2) NOT NULL;
CONSTRAINT pk_con PRIMARY KEY (S#, P#, J#), CONSTRAINT
fk_con1 FORIGN KEY (S#) REFERENCES S (S#)
ON UPDATE NO ACTION
ON DELECT CASCADE,
Constrains in SQL
CONSTRAINT fk_con2 FORIGN KEY(P#)
REFERENCES P (P#)
ON UPDATE CASCADE
ON DELECT SET DEFAULT

CONSTRAINT fk_con3 FORIGN KEY(J#)


REFERENCES J (J#)
ON UPDATE CASCADE
ON DELECT CASCADE);
Constrains in SQL
• THE OPTION FOR on update and ON DELECT are
CASCADE,
SET NULL,
SET DEFAULT, AND NO ACTION.
• Set null can use only if the FK not part of the PK.
• DDL
• DML
• Create
• Delete
• Drop
• Add
• Alter
• SQL
• The name SQL is derived from Structured Query Language.

• The Data Definition Language consist with CREATE TABLE,


ALTER TABLE, DROP TABLE CREATE TABLE AND DROP TABLE.

• The CREATE TABLE command is used to specify a new relation


by giving name and attributes.

• The Data Manipulation Language are INSERT, DELECT, UPDATE


AND SELECT.
1. Consider the following relations:
S (S#, SNAME, STATUS, CITY)
SP (S#, P#, QTY)
P (P#, PNAME, COLOR, WEIGHT, CITY)
Give an expression in SQL for each of queries below:
(i) Get supplier names for supplier who supply at least one red
part
(ii) Get supplier names for supplier who do not supply part P2.
• Consider the following relational schemas:

EMPLOYEE (EMPLOYEE_NAME, STREET, CITY)


WORKS (EMPLOYEE_NAME, COMPANYNAME, SALARY)
COMPANY (COMPANY_NAME, CITY)
a) Specify the table definitions in SQL.
b)(i)Find the names of all employees who work for first Bank Corporation.
(ii) Find the names and company names of all employees sorted in ascending
order of company name and descending order of employee names of that
company.
(iii) Change the city of First Bank Corporation to ‘New Delhi’
• Answer each of the following questions briefly. The questions are based
on the following relational schema:
 Emp(eid: integer, ename: string, age: integer, salary: real)
 Works(eid: integer, did: integer, pcttime: integer)
 Dept(did: integer, dname: string, budget: real, managerid: integer)
1. Give an example of a foreign key constraint that involves the Dept relation.
What are the options for enforcing this constraint when a user attempts to
delete a Dept tuple?
2.Write the SQL statements required to create the preceding relations,
including appropriate versions of all primary and foreign key integrity
constraints.
3. Define the Dept relation in SQL so that every department is guaranteed to
have a manager.
4. Write an SQL statement to add John Doe as an employee with eid = 101,
age = 32 and salary = 15, 000.

You might also like