0% found this document useful (0 votes)
15 views

Chapter 7 SQL

Uploaded by

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

Chapter 7 SQL

Uploaded by

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

Chapter 7

Structured Query Language (SQL)

Database Systems: Design, Implementation, and Management


7th Edition

Peter Rob & Carlos Coronel


INTRODUCTION

 SQL functions fit into two broad categories:


 Data definition language (DDL). SQL includes commands to
create database objects such as tables, indexes, and views, as well
as commands to define access rights to those database objects.
Some common data definition commands you will learn are listed
in Table 7.1.
 Data manipulation language (DML). SQL includes commands to
insert, update, delete, and retrieve data within the database tables.
The data manipulation commands you will learn in this chapter
are listed in Table 7.2.
DDL
DML
Chapter 7, pg : 253
CREATE TABLE

CREATE TABLE tablename (


column1 data type [constraint] ,
column2 data type [constraint] ,
PRIMARY KEY (column1 [, column2]) ,
FOREIGN KEY (column1 [, column2]) ,
REFERENCES tablename ,
CONSTRAINT constraint );
CREATE TABLE VENDOR
CREATE TABLE PRODUCT

);
DML COMMAND

 The basic SQL data manipulation commands:-


 INSERT
 SELECT
 COMMIT - permanently saves all changes—such as rows added, attributes modified, and
rows deleted—made to any table in the database.
 UPDATE
 ROLLBACK - If you have not yet used the COMMIT command to store the changes
permanently in the database, you can restore the database to its previous condition with the
ROLLBACK command. ROLLBACK undoes any changes since the last COMMIT command
and brings all of the data back to the values that existed before the changes were made.
 DELETE
INSERT – ADDING TABLE ROWS

 INSERT INTO tablename VALUES (value1, value2, …, valuen);


INSERT INTO VENDOR VALUES (21225,'Bryson, Inc.','Smithson', '615‘, '223-
3234','TN','Y');
INSERT INTO VENDOR VALUES (21226,'Superloo, Inc.','Flushing','904', '215-
8995','FL','N');
INSERT INTO PRODUCT VALUES ('11QER/31','Power painter, 15 psi., 3-nozzle', '03-
Nov-15',8,5,109.99,0.00,25595);
INSERT INTO PRODUCT VALUES ('13-Q2/P2','7.25-in. pwr. saw blade', '13-Dec-
15',32,15,14.99, 0.05, 21344);
INSERT INTO PRODUCT VALUES ('BRT-345','Titanium drill bit','18-Oct-15', 75,
10, 4.50, 0.06, NULL);
SELECT – LISTING TABLE ROWS

 To see the contents of the table, use the COMMAND;-

SELECT columnlist FROM tablename;


 The columnlist represents one or more attributes, separated by commas. You could use the asterisk ( * ) to list all
attributes.
 SELECT * FROM PRODUCT;

 SELECT P_CODE, P_DESCRIPT, P_INDATE, P_QOH, P_MIN, P_PRICE, P_DISCOUNT, V_CODE FROM
PRODUCT;
SELECTING ROWS WITH
CONDITIONAL RESTRICTIONS
 SELECT P_DESCRIPT, P_INDATE, P_PRICE, V_CODE FROM PRODUCT
WHERE V_CODE = 21344;
SELECTING ROWS WITH
CONDITIONAL RESTRICTIONS
SELECTING ROWS WITH
CONDITIONAL RESTRICTIONS
 SELECT P_DESCRIPT, P_QOH, P_PRICE, V_CODE FROM PRODUCT
WHERE V_CODE <> 21344;
SELECTING ROWS WITH
CONDITIONAL RESTRICTIONS
 SELECT P_DESCRIPT, P_QOH, P_MIN, P_PRICE FROM PRODUCT
WHERE P_PRICE <= 10;
SELECTING ROWS WITH
CONDITIONAL RESTRICTIONS
 SELECT P_DESCRIPT, P_INDATE, P_PRICE, V_CODE

FROM PRODUCT
WHERE V_CODE = 21344 OR V_CODE = 24288;
SELECTING ROWS WITH
CONDITIONAL RESTRICTIONS
 SELECT P_DESCRIPT, P_INDATE, P_PRICE, V_CODE

FROM PRODUCT
WHERE P_PRICE < 50 AND P_INDATE > '15-Jan-2016';
COPYING PARTS OF TABLES
 SQL provides another way to rapidly create a new table based
on selected columns and rows of an existing table.
 In this case, the new table will copy the attribute names, data
characteristics, and rows of the original table.
 CREATE TABLE PART AS

SELECT P_CODE AS PART_CODE,


P_DESCRIPT AS PART_DESCRIPT,
P_PRICE AS PART_PRICE,
V_CODE
FROM PRODUCT;
COPYING PARTS OF TABLES

 The SQL command just shown creates a new PART table with
PART_CODE, PART_ DESCRIPT, PART_PRICE, and
V_CODE columns.
 In addition, all of the data rows for the selected columns will be
copied automatically.
 However, note that no entity integrity (primary key) or
referential integrity (foreign key) rules are automatically
applied to the new table.
 In the next slide, you will learn how to define the PK to enforce
entity integrity and the FK to enforce referential integrity.
COPYING PARTS OF TABLES
ALTER TABLE PART ADD PRIMARY KEY
(PART_CODE);
ALTER TABLE PART ADD FOREIGN KEY (V_CODE)
REFERENCES VENDOR;
Alternatively, if neither the PART table’s primary key nor its
foreign key has been designated, you can incorporate both
changes at once:
ALTER TABLE PART ADD PRIMARY KEY
(PART_CODE) ADD FOREIGN KEY (V_CODE)
REFERENCES VENDOR;
UPDATE – UPDATING TABLE
ROWS

 UPDATE tablename SET columnname = expression, columnname =


expression WHERE conditionlist ;
 For example, if you want to change P_INDATE from December 13, 2015, to
January 18, 2016, in the second row of the PRODUCT table (see Figure 7.3),
use the primary key (13-Q2/P2) to locate the correct row. Therefore, type:

UPDATE PRODUCT SET P_INDATE = '18-JAN-2016' WHERE P_CODE = '13-


Q2/P2';

 If more than one attribute is to be updated in the row, separate the corrections
with commas:

UPDATE PRODUCT SET P_INDATE = '18-JAN-2016', P_PRICE = 17.99,


P_MIN = 10 WHERE P_CODE = '13-Q2/P2';
DELETE – DELETING TABLE
ROWS
 DELETE FROM tablename WHERE conditionlist ;

DELETE FROM PRODUCT WHERE P_CODE = 'BRT-345';

DELETE FROM PRODUCT WHERE P_MIN = 5;


ORDERING A LISTING
 With a little extra complexity, we can get our output in order
by some particular attribute(s)
 SELECT columnlist FROM tablelist WHERE conditionlist
ORDER BY columnlist ASC | DESC;
 Although you have the option of declaring the order type—
ascending or descending— the default order is ascending.
 For example, if you want the contents of the PRODUCT table
to be listed by P_PRICE in ascending order, use the following
commands:
 SELECT P_CODE, P_DESCRIPT, P_QOH, P_PRICE FROM
PRODUCT ORDER BY P_PRICE;
ORDERING A LISTING
 With a little extra complexity, we can get our output in order by
some particular attribute(s)
 SELECT columnlist FROM tablelist WHERE conditionlist
ORDER BY columnlist ASC | DESC;
 Although you have the option of declaring the order type—
ascending or descending— the default order is ascending.
 For example, if you want the contents of the PRODUCT table to be
listed by P_PRICE in ascending order, use the following commands:
 SELECT P_CODE, P_DESCRIPT, P_QOH, P_PRICE FROM
PRODUCT ORDER BY P_PRICE;
 SELECT P_CODE, P_DESCRIPT, P_INDATE, P_PRICE FROM
PRODUCT ORDER BY P_PRICE DESC;
DESC
JOINING TABLES
 As you know, an important task with relational databases is
relating info from more than one table
 SELECT P_DESCRIPT, P_PRICE, V_NAME, V_CONTACT,
V_AREACODE, V_PHONE FROM PRODUCT, VENDOR
WHERE PRODUCT.V_CODE = VENDOR.V_CODE;
JOINING TABLES
JOINING TABLES WITH AN
ALIAS
 The aliases P and V are used to label the PRODUCT and
VENDOR tables in the next command sequence.
 Any legal table name may be used as an alias. (Also notice
that there are no table name prefixes because the attribute
listing contains no duplicate names in the SELECT
statement.)
 SELECT P_DESCRIPT, P_PRICE, V_NAME,
V_CONTACT, V_AREACODE, V_PHONE FROM
PRODUCT P, VENDOR V WHERE P.V_CODE =
V.V_CODE ORDER BY P_PRICE;
QUERIES

 Special Operators

 IS NULL - used to check whether an attribute value is null

 LIKE - used to check for similar character strings.

 IN - used to check whether an attribute value matches a value


contained within a (sub)set of listed values.
 EXISTS - used to check whether an attribute has a value. In effect,
EXISTS is the opposite of IS NULL.
 Can also be used to check if a subquery returns any rows
QUERIES

 Special Operators

IS NULL is used to check whether an attribute value is null.


SELECT INDEX, DEPT, CLASS, TIME
FROM SECTION
WHERE ROOM IS NULL;
QUERIES
 Special Operators
LIKE is used to check for similar character strings.

SELECT * FROM CATALOG_CLASS WHERE TITLE LIKE


‘%Network%’;

e.g. WHERE TITLE LIKE ‘%Network%’ finds values whose title


includes the substring ‘Network’
QUERIES

 Special Operators
IN is used to check whether an attribute value matches a value contained
within a (sub)set of listed values.

SELECT * FROM ENROLLMENT


WHERE INDEX IN (66415, 66421);

EXISTS is used to check whether an attribute has value.

SELECT * FROM SECTIONs


WHERE PROFESSOR EXISTS;
END OF SLIDES

You might also like