1 reminder we have covered: –creating tables –converting er diagrams to table definitions today...
Post on 19-Dec-2015
215 views
TRANSCRIPT
![Page 1: 1 Reminder We have covered: –Creating tables –Converting ER diagrams to table definitions Today we’ll talk about: –Altering tables –Inserting and deleting](https://reader036.vdocument.in/reader036/viewer/2022062516/56649d3e5503460f94a170a0/html5/thumbnails/1.jpg)
1
ReminderReminder
• We have covered:
– Creating tables
– Converting ER diagrams to table definitions
• Today we’ll talk about:
– Altering tables
– Inserting and deleting data from tables
– Querying tables
![Page 2: 1 Reminder We have covered: –Creating tables –Converting ER diagrams to table definitions Today we’ll talk about: –Altering tables –Inserting and deleting](https://reader036.vdocument.in/reader036/viewer/2022062516/56649d3e5503460f94a170a0/html5/thumbnails/2.jpg)
2
Table AlterationTable Alteration
![Page 3: 1 Reminder We have covered: –Creating tables –Converting ER diagrams to table definitions Today we’ll talk about: –Altering tables –Inserting and deleting](https://reader036.vdocument.in/reader036/viewer/2022062516/56649d3e5503460f94a170a0/html5/thumbnails/3.jpg)
3
Altering TablesAltering Tables
• Table definitions can be altered after their
creation
– Adding columns
– Changing columns’ definition
– Dropping columns
– Adding or dropping constraints
– And more…
• Use the reserved word ALTER
![Page 4: 1 Reminder We have covered: –Creating tables –Converting ER diagrams to table definitions Today we’ll talk about: –Altering tables –Inserting and deleting](https://reader036.vdocument.in/reader036/viewer/2022062516/56649d3e5503460f94a170a0/html5/thumbnails/4.jpg)
4
Altering Tables (cont.)Altering Tables (cont.)
• Adding a column:ALTER TABLE Employee ADD (
Mname VARCHAR2(20),Birthday DATE
);
• Changing columns’ definition:ALTER TABLE Emplyee Modify (
Mname VARCHAR2(10));
Cannot be NOT NULL unless the
table is empty
![Page 5: 1 Reminder We have covered: –Creating tables –Converting ER diagrams to table definitions Today we’ll talk about: –Altering tables –Inserting and deleting](https://reader036.vdocument.in/reader036/viewer/2022062516/56649d3e5503460f94a170a0/html5/thumbnails/5.jpg)
5
Altering Tables (cont.)Altering Tables (cont.)
• Dropping columns:
ALTER TABLE Employee DROP COLUMN Mname;
Dropping multiple columns:
ALTER TABLE Employee DROP
(Mname, Birthday);
• Adding constraints:
ALTER TABLE Department ADD(
FOREIGN KEY (ManagerId)
REFERENCES Employee(SSN));
![Page 6: 1 Reminder We have covered: –Creating tables –Converting ER diagrams to table definitions Today we’ll talk about: –Altering tables –Inserting and deleting](https://reader036.vdocument.in/reader036/viewer/2022062516/56649d3e5503460f94a170a0/html5/thumbnails/6.jpg)
6
Inserting, deleting, and Inserting, deleting, and updating data in a tableupdating data in a table
![Page 7: 1 Reminder We have covered: –Creating tables –Converting ER diagrams to table definitions Today we’ll talk about: –Altering tables –Inserting and deleting](https://reader036.vdocument.in/reader036/viewer/2022062516/56649d3e5503460f94a170a0/html5/thumbnails/7.jpg)
7
The Employee TableThe Employee Table> Describe Employee
Name Null? Type
-------- -------- ------------
SSN NUMBER
FNAME VARCHAR2(20)
LNAME VARCHAR2(20)
GENDER CHAR(1)
SALARY NOT NULL NUMBER(5)
![Page 8: 1 Reminder We have covered: –Creating tables –Converting ER diagrams to table definitions Today we’ll talk about: –Altering tables –Inserting and deleting](https://reader036.vdocument.in/reader036/viewer/2022062516/56649d3e5503460f94a170a0/html5/thumbnails/8.jpg)
8
Inserting a RowInserting a Row
• To insert a row into the Employee table:
INSERT INTO
Employee(SSN, Fname, Lname, Salary)
VALUES(121, ‘Sara’, ‘Cohen’,10000);
• The remaining columns get default values (or
NULL)
• When will this fail?
![Page 9: 1 Reminder We have covered: –Creating tables –Converting ER diagrams to table definitions Today we’ll talk about: –Altering tables –Inserting and deleting](https://reader036.vdocument.in/reader036/viewer/2022062516/56649d3e5503460f94a170a0/html5/thumbnails/9.jpg)
9
Some More Details…Some More Details…
• The fields don’t have to be specified if
values are specified for all columns and
in the order defined by the table
• Example:
INSERT INTO Employee
VALUES(121, ‘Sara’, ‘Cohen’, `F’, 1000);
![Page 10: 1 Reminder We have covered: –Creating tables –Converting ER diagrams to table definitions Today we’ll talk about: –Altering tables –Inserting and deleting](https://reader036.vdocument.in/reader036/viewer/2022062516/56649d3e5503460f94a170a0/html5/thumbnails/10.jpg)
10
Deleting RowsDeleting Rows• General format:
DELETE FROM Table WHERE Cond;
Deletes all rows satisfying Cond from Table
• For example, to remove the employee with
SSN 121 from the Employee table:
DELETE FROM Employee
WHERE SSN = 121;
![Page 11: 1 Reminder We have covered: –Creating tables –Converting ER diagrams to table definitions Today we’ll talk about: –Altering tables –Inserting and deleting](https://reader036.vdocument.in/reader036/viewer/2022062516/56649d3e5503460f94a170a0/html5/thumbnails/11.jpg)
11
Deleting Rows (cont.)Deleting Rows (cont.)
• To remove all male employees having
a salary greater than 150,000 shekels:
DELETE FROM Employee
WHERE Salary > 150000;
![Page 12: 1 Reminder We have covered: –Creating tables –Converting ER diagrams to table definitions Today we’ll talk about: –Altering tables –Inserting and deleting](https://reader036.vdocument.in/reader036/viewer/2022062516/56649d3e5503460f94a170a0/html5/thumbnails/12.jpg)
12
Updating Rows Updating Rows
• We can update rows in a table• General format:
UPDATE Table SET Field1=value1,,,FieldN=valueNWHERE Cond
• Now we can reduce salaries instead of firing employees:
UPDATE Employee SET Salary = 50000 WHERE Salary > 150000;
![Page 13: 1 Reminder We have covered: –Creating tables –Converting ER diagrams to table definitions Today we’ll talk about: –Altering tables –Inserting and deleting](https://reader036.vdocument.in/reader036/viewer/2022062516/56649d3e5503460f94a170a0/html5/thumbnails/13.jpg)
13
The ORACLE Bulk LoaderThe ORACLE Bulk Loader• A tool that provides easy insertion of large
amounts of rows into tables.• The idea: the values of the inserted rows are
kept in a compact file: the Data file.• We define the structure of the Data file, in
the Control file.
Here we enter the data we want to
insert (employees)
myData.dat
Here we specify the structure of
the data file
myCtrl.ctl
![Page 14: 1 Reminder We have covered: –Creating tables –Converting ER diagrams to table definitions Today we’ll talk about: –Altering tables –Inserting and deleting](https://reader036.vdocument.in/reader036/viewer/2022062516/56649d3e5503460f94a170a0/html5/thumbnails/14.jpg)
14
The Control FileThe Control File
• For example, we can load 3 employees from the file myEmp.dat that contains the following lines:
Shaquile|Oneil|121Magic|Johnson|134Doron|Sheffer|156
• A simple control file:
LOAD DATA
INFILE <dataFile>
[APPEND] INTO TABLE <tableName>
FIELDS TERMINATED BY '<separator>‘
(<list of all attribute names to load>)
![Page 15: 1 Reminder We have covered: –Creating tables –Converting ER diagrams to table definitions Today we’ll talk about: –Altering tables –Inserting and deleting](https://reader036.vdocument.in/reader036/viewer/2022062516/56649d3e5503460f94a170a0/html5/thumbnails/15.jpg)
15
The Control File (cont.)The Control File (cont.)
• <dataFile>:
The name of the data file
• <tableName>:
The name of the table into which the data will be loaded (appended if APPEND is specified, or else the table must be empty)
• <separator>:
A string that separates two field values of a row
• The attributes are separated by commas and enclosed in parentheses
![Page 16: 1 Reminder We have covered: –Creating tables –Converting ER diagrams to table definitions Today we’ll talk about: –Altering tables –Inserting and deleting](https://reader036.vdocument.in/reader036/viewer/2022062516/56649d3e5503460f94a170a0/html5/thumbnails/16.jpg)
16
The Control File (cont.)The Control File (cont.)• Example:
– When the control file is run, this will insert the 3 employees into the employees table
– The attributes that are unspecified will be set to NULL (or default values if they are specified)
LOAD DATA
INFILE myEmployees.dat
INTO TABLE Employees
FIELDS TERMINATED BY '|'
(Fname, Lname, SSN)
Moshe|Cohen|334
Miri|Levi|998
Alon|Hazan|998
myEmployees.dat
myControl.ctl
![Page 17: 1 Reminder We have covered: –Creating tables –Converting ER diagrams to table definitions Today we’ll talk about: –Altering tables –Inserting and deleting](https://reader036.vdocument.in/reader036/viewer/2022062516/56649d3e5503460f94a170a0/html5/thumbnails/17.jpg)
17
The Data FileThe Data File
• The Bulk Loader considers every single line to
represent one row in the table
– Even an empty line! (which will usually result
in an error)
• Spaces are not ignored in the data file!
– thus the rows sara| cohen|121 and sara|
cohen|121 define different functionalities
• The NULL value is implied by the NULL keyword or the
empty string
![Page 18: 1 Reminder We have covered: –Creating tables –Converting ER diagrams to table definitions Today we’ll talk about: –Altering tables –Inserting and deleting](https://reader036.vdocument.in/reader036/viewer/2022062516/56649d3e5503460f94a170a0/html5/thumbnails/18.jpg)
18
The Data File (continued)The Data File (continued)• The control and the data files can be combined into
one .ctl file using the following format:
LOAD DATA
INFILE *
INTO TABLE Employees
FIELDS TERMINATED BY '|'
(Fname, Lname, SSN)
BEGINDATA
Sara|Cohen|121
Benny|Kimelfeld|134
Yaron|Kanza|156
![Page 19: 1 Reminder We have covered: –Creating tables –Converting ER diagrams to table definitions Today we’ll talk about: –Altering tables –Inserting and deleting](https://reader036.vdocument.in/reader036/viewer/2022062516/56649d3e5503460f94a170a0/html5/thumbnails/19.jpg)
19
The Bulk InvocationThe Bulk Invocation
• To invoke the bulk loader, issue the following command directly from the Unix shell:Sqlldr <ctrlFileName>
• Erroneous lines in the data file are ignored and written into <ctrlFileName>.bad, and any other relevant information is written into <ctrlFileName>.log.
• The tables you fill using the Bulk Loader should be created prior to the loader invocation
• Before invoking the Bulk Loader you have to make sure that no SqlPlus sessions are open.
![Page 20: 1 Reminder We have covered: –Creating tables –Converting ER diagrams to table definitions Today we’ll talk about: –Altering tables –Inserting and deleting](https://reader036.vdocument.in/reader036/viewer/2022062516/56649d3e5503460f94a170a0/html5/thumbnails/20.jpg)
20
SQL QueriesSQL Queries
![Page 21: 1 Reminder We have covered: –Creating tables –Converting ER diagrams to table definitions Today we’ll talk about: –Altering tables –Inserting and deleting](https://reader036.vdocument.in/reader036/viewer/2022062516/56649d3e5503460f94a170a0/html5/thumbnails/21.jpg)
21
Query ComponentsQuery Components
• A query can contain the following clauses – select
– from
– where
– group by
– having
– order by
• Only select and from are required
• Order of clauses is always as above
![Page 22: 1 Reminder We have covered: –Creating tables –Converting ER diagrams to table definitions Today we’ll talk about: –Altering tables –Inserting and deleting](https://reader036.vdocument.in/reader036/viewer/2022062516/56649d3e5503460f94a170a0/html5/thumbnails/22.jpg)
22
Very Basic SQL QueryVery Basic SQL QuerySELECT [Distinct] Attributes
FROM relation
•Attributes: A list of attributes onto which the query projects (For example: Eid, Ename).
•Relation: A relation to perform the query on.
•DISTINCT: Optional keyword to delete duplicatesExample:
Select studentID, studentName
From students
![Page 23: 1 Reminder We have covered: –Creating tables –Converting ER diagrams to table definitions Today we’ll talk about: –Altering tables –Inserting and deleting](https://reader036.vdocument.in/reader036/viewer/2022062516/56649d3e5503460f94a170a0/html5/thumbnails/23.jpg)
23
StudentIDStudentDe
pt.
StudentNa
me
StudentAg
e
1123 Math Moshe 25
2245 Computers Mickey 26
55611 Math Menahem 29
Select studentID, studentName
From students
Result:
StudentIDStudentNa
me
1123 Moshe
2245 Mickey
55611 Menahem
![Page 24: 1 Reminder We have covered: –Creating tables –Converting ER diagrams to table definitions Today we’ll talk about: –Altering tables –Inserting and deleting](https://reader036.vdocument.in/reader036/viewer/2022062516/56649d3e5503460f94a170a0/html5/thumbnails/24.jpg)
24
Basic SQL QueryBasic SQL Query
SELECT [Distinct] Attributes
FROM relation
WHERE condition
•Attributes: A list of fields onto which the query projects (For example: Eid, Ename).
•relation: A relation to perform the query on
•condition: A Boolean condition (For example: Eid>21, or Ename=‘Yuval’ )
•DISTINCT: Optional keyword to delete duplicates
![Page 25: 1 Reminder We have covered: –Creating tables –Converting ER diagrams to table definitions Today we’ll talk about: –Altering tables –Inserting and deleting](https://reader036.vdocument.in/reader036/viewer/2022062516/56649d3e5503460f94a170a0/html5/thumbnails/25.jpg)
25
StudentIDStudentDe
pt.
StudentNa
me
StudentAg
e
1123 Math Moshe 25
2245 Computers Mickey 26
55611 Math Menahem 29Select studentID, studentName
From students
Where StudentDept=‘Math’
Result:
StudentIDStudentNa
me
1123 Moshe
55611 Menahem
![Page 26: 1 Reminder We have covered: –Creating tables –Converting ER diagrams to table definitions Today we’ll talk about: –Altering tables –Inserting and deleting](https://reader036.vdocument.in/reader036/viewer/2022062516/56649d3e5503460f94a170a0/html5/thumbnails/26.jpg)
26
Basic SQL QueryBasic SQL Query
SELECT [Distinct] attributes
FROM relation
WHERE condition;
•Notice! The "SELECT" clause defines the operation of projection from the relational model. Selection is defined by the WHERE clause.
![Page 27: 1 Reminder We have covered: –Creating tables –Converting ER diagrams to table definitions Today we’ll talk about: –Altering tables –Inserting and deleting](https://reader036.vdocument.in/reader036/viewer/2022062516/56649d3e5503460f94a170a0/html5/thumbnails/27.jpg)
27
SQL and relational algebraSQL and relational algebra
SELECT Distinct A1,…,An
FROM R1,…,Rm
WHERE C;
A1,…,An (C(R1 x…x Rm))
![Page 28: 1 Reminder We have covered: –Creating tables –Converting ER diagrams to table definitions Today we’ll talk about: –Altering tables –Inserting and deleting](https://reader036.vdocument.in/reader036/viewer/2022062516/56649d3e5503460f94a170a0/html5/thumbnails/28.jpg)
28
Basic SQL QueryBasic SQL Query
SELECT [Distinct] Attributes
FROM relationsList
WHERE condition
•relationsList: A list of relations. The query is performed on the product of these relations (AxBxC…).
![Page 29: 1 Reminder We have covered: –Creating tables –Converting ER diagrams to table definitions Today we’ll talk about: –Altering tables –Inserting and deleting](https://reader036.vdocument.in/reader036/viewer/2022062516/56649d3e5503460f94a170a0/html5/thumbnails/29.jpg)
29
Basic SQL QueryBasic SQL Query
SELECT [Distinct] attributes
FROM relations
WHERE condition;
Important! The evaluation order is:
1. Compute the cross product of the tables in relations.
2. Delete all rows that do not satisfy condition.3. Delete all columns that do not appear in attributes.4. If Distinct is specified eliminate duplicate rows.
![Page 30: 1 Reminder We have covered: –Creating tables –Converting ER diagrams to table definitions Today we’ll talk about: –Altering tables –Inserting and deleting](https://reader036.vdocument.in/reader036/viewer/2022062516/56649d3e5503460f94a170a0/html5/thumbnails/30.jpg)
30
Example Tables UsedExample Tables Used
Reserves
sid bid day
22
58
101
103
10/10/96
11/12/96
Sailors
sid sname rating age
22
31
58
Dustin
Lubber
Rusty
7
8
10
45.0
55.5
35.0
Boats
bid bname color
101
103
Nancy
Gloria
red
green
![Page 31: 1 Reminder We have covered: –Creating tables –Converting ER diagrams to table definitions Today we’ll talk about: –Altering tables –Inserting and deleting](https://reader036.vdocument.in/reader036/viewer/2022062516/56649d3e5503460f94a170a0/html5/thumbnails/31.jpg)
31
What does this compute?What does this compute?
Select sname
from sailors, reserves
Where sailors.sid=reserves.sid
Reserves
sid bid day
22
58
101
103
10/10/96
11/12/96
Sailors
sid sname rating age
22
31
58
Dustin
Lubber
Rusty
7
8
10
45.0
55.5
35.0
![Page 32: 1 Reminder We have covered: –Creating tables –Converting ER diagrams to table definitions Today we’ll talk about: –Altering tables –Inserting and deleting](https://reader036.vdocument.in/reader036/viewer/2022062516/56649d3e5503460f94a170a0/html5/thumbnails/32.jpg)
32
Sailors Reserves
sid sname rating age sid bid day
22 Dustin 7 45.0 22 101 10/10/96
22 Dustin 7 45.0 58 103 11/12/96
31 Lubber 8 55.5 22 101 10/10/96
31 Lubber 8 55.5 58 103 11/12/96
58 Rusty 10 35.0 22 101 10/10/96
58 Rusty 10 35.0 58 103 11/12/96
Stage 1: Sailors x Reserves
![Page 33: 1 Reminder We have covered: –Creating tables –Converting ER diagrams to table definitions Today we’ll talk about: –Altering tables –Inserting and deleting](https://reader036.vdocument.in/reader036/viewer/2022062516/56649d3e5503460f94a170a0/html5/thumbnails/33.jpg)
33
Sailors Reserves
sid sname rating age sid bid day
22 Dustin 7 45.0 22 101 10/10/96
22 Dustin 7 45.0 58 103 11/12/96
31 Lubber 8 55.5 22 101 10/10/96
31 Lubber 8 55.5 58 103 11/12/96
58 Rusty 10 35.0 22 101 10/10/96
58 Rusty 10 35.0 58 103 11/12/96
Stage 2: “where sailors.sid=reserves.sid”
![Page 34: 1 Reminder We have covered: –Creating tables –Converting ER diagrams to table definitions Today we’ll talk about: –Altering tables –Inserting and deleting](https://reader036.vdocument.in/reader036/viewer/2022062516/56649d3e5503460f94a170a0/html5/thumbnails/34.jpg)
34
Sailors Reserves
sid sname rating age sid bid day
22 Dustin 7 45.0 22 101 10/10/96
58 Rusty 10 35.0 58 103 11/12/96
Stage 2: “where sailors.sid=reserves.sid”
![Page 35: 1 Reminder We have covered: –Creating tables –Converting ER diagrams to table definitions Today we’ll talk about: –Altering tables –Inserting and deleting](https://reader036.vdocument.in/reader036/viewer/2022062516/56649d3e5503460f94a170a0/html5/thumbnails/35.jpg)
35
Sailors Reserves
sid sname rating age sid bid day
22 Dustin 7 45.0 22 101 10/10/96
58 Rusty 10 35.0 58 103 11/12/96
Stage 3: “select sname”
![Page 36: 1 Reminder We have covered: –Creating tables –Converting ER diagrams to table definitions Today we’ll talk about: –Altering tables –Inserting and deleting](https://reader036.vdocument.in/reader036/viewer/2022062516/56649d3e5503460f94a170a0/html5/thumbnails/36.jpg)
36
Sailors
sname
Dustin
Rusty
Stage 3: “select sname”
Final answer
![Page 37: 1 Reminder We have covered: –Creating tables –Converting ER diagrams to table definitions Today we’ll talk about: –Altering tables –Inserting and deleting](https://reader036.vdocument.in/reader036/viewer/2022062516/56649d3e5503460f94a170a0/html5/thumbnails/37.jpg)
37
Example QueryExample Query
SELECT DISTINCT sname, age
FROM Sailors
WHERE rating>7;
Q: What does this compute?
A: Distinct names and ages of sailors with rating >7.
Q: Write it in algebra
A: sname, age (rating>7(Sailors))
![Page 38: 1 Reminder We have covered: –Creating tables –Converting ER diagrams to table definitions Today we’ll talk about: –Altering tables –Inserting and deleting](https://reader036.vdocument.in/reader036/viewer/2022062516/56649d3e5503460f94a170a0/html5/thumbnails/38.jpg)
38
Example QueryExample QuerySELECT DISTINCT sname
FROM Sailors, Reserves
WHERE Sailors.sid = Reserves.sid and
bid = 103;
Q: What does this compute?
A: names of sailors who reserved boat 103
Q: Write it in relational algebra
sname
(Sailors.sid = Reserves.sid bid = 103 (Sailors x Reserves))
![Page 39: 1 Reminder We have covered: –Creating tables –Converting ER diagrams to table definitions Today we’ll talk about: –Altering tables –Inserting and deleting](https://reader036.vdocument.in/reader036/viewer/2022062516/56649d3e5503460f94a170a0/html5/thumbnails/39.jpg)
39
Sailors Reserves
sid sname rating age sid bid day
22 Dustin 7 45.0 22 101 10/10/96
22 Dustin 7 45.0 58 103 11/12/96
31 Lubber 8 55.5 22 101 10/10/96
31 Lubber 8 55.5 58 103 11/12/96
58 Rusty 10 35.0 22 101 10/10/96
58 Rusty 10 35.0 58 103 11/12/96
Sailors.sid = Reserves.sid bid = 103
![Page 40: 1 Reminder We have covered: –Creating tables –Converting ER diagrams to table definitions Today we’ll talk about: –Altering tables –Inserting and deleting](https://reader036.vdocument.in/reader036/viewer/2022062516/56649d3e5503460f94a170a0/html5/thumbnails/40.jpg)
40
Sailors Reserves
sid sname rating age sid bid day
22 Dustin 7 45.0 22 101 10/10/96
22 Dustin 7 45.0 58 103 11/12/96
31 Lubber 8 55.5 22 101 10/10/96
31 Lubber 8 55.5 58 103 11/12/96
58 Rusty 10 35.0 22 101 10/10/96
58 Rusty 10 35.0 58 103 11/12/96
Sailors x Reserves
sname
![Page 41: 1 Reminder We have covered: –Creating tables –Converting ER diagrams to table definitions Today we’ll talk about: –Altering tables –Inserting and deleting](https://reader036.vdocument.in/reader036/viewer/2022062516/56649d3e5503460f94a170a0/html5/thumbnails/41.jpg)
41
Range VariablesRange Variables
SELECT S.sname
FROM Sailors S, Reserves R
WHERE S.sid = R.sid and
R.bid = 103;
• Range variables are good style.
• They are necessary if the same relation appears twice in the FROM clause
• Similar to Renaming in Relational Algebra
![Page 42: 1 Reminder We have covered: –Creating tables –Converting ER diagrams to table definitions Today we’ll talk about: –Altering tables –Inserting and deleting](https://reader036.vdocument.in/reader036/viewer/2022062516/56649d3e5503460f94a170a0/html5/thumbnails/42.jpg)
42
Example QueryExample Query
SELECT S.sname
FROM Sailors S, Reserves R
WHERE S.sid = R.sid and
R.bid != 103;
Q: What does this return?Q: What does this return?
A: Names of sailors who reserved a boat A: Names of sailors who reserved a boat other than boat 103 other than boat 103
Notice: sailors who reserved both boat 103 Notice: sailors who reserved both boat 103 andand a different boat will appear! a different boat will appear!
![Page 43: 1 Reminder We have covered: –Creating tables –Converting ER diagrams to table definitions Today we’ll talk about: –Altering tables –Inserting and deleting](https://reader036.vdocument.in/reader036/viewer/2022062516/56649d3e5503460f94a170a0/html5/thumbnails/43.jpg)
43
A Few SELECT OptionsA Few SELECT Options
• Select all columns:SELECT * FROM Sailors;
• Rename selected columns:SELECT S.sname AS Sailors_Name FROM Sailors S;
• Applying functions (e.g., Mathematical manipulations) SELECT (age-5)*2 FROM Sailors S;
![Page 44: 1 Reminder We have covered: –Creating tables –Converting ER diagrams to table definitions Today we’ll talk about: –Altering tables –Inserting and deleting](https://reader036.vdocument.in/reader036/viewer/2022062516/56649d3e5503460f94a170a0/html5/thumbnails/44.jpg)
44
Select operatorsSelect operators
• The aggregate operators available in SQL are:
– COUNT(*)
– COUNT([DISTINCT] A)
– SUM([DISTINCT] A)
– AVG([DISTINCT] A)
– MAX(A)
– MIN(A)
• NULL values are ignored
![Page 45: 1 Reminder We have covered: –Creating tables –Converting ER diagrams to table definitions Today we’ll talk about: –Altering tables –Inserting and deleting](https://reader036.vdocument.in/reader036/viewer/2022062516/56649d3e5503460f94a170a0/html5/thumbnails/45.jpg)
45
ExamplesExamples
SELECT Avg(S.age)
FROM Sailors S, Reserves R
WHERE S.sid = R.sid and R.bid=112
SELECT count(distinct S.sid)
FROM Sailors S, Reserves R
WHERE S.sid = R.sid
SELECT Max(S.age)
FROM Sailors S
![Page 46: 1 Reminder We have covered: –Creating tables –Converting ER diagrams to table definitions Today we’ll talk about: –Altering tables –Inserting and deleting](https://reader036.vdocument.in/reader036/viewer/2022062516/56649d3e5503460f94a170a0/html5/thumbnails/46.jpg)
46
TheThe WHEREWHERE ClauseClause
• Numerical and string comparison:
!=,<>,=, <, >, >=, <=, between(val1 AND val2)
• Logical components: AND, OR
• Null verification: IS NULL, IS NOT NULL
• Checking against a list with IN, NOT IN.
![Page 47: 1 Reminder We have covered: –Creating tables –Converting ER diagrams to table definitions Today we’ll talk about: –Altering tables –Inserting and deleting](https://reader036.vdocument.in/reader036/viewer/2022062516/56649d3e5503460f94a170a0/html5/thumbnails/47.jpg)
47
ExamplesExamples
SELECT sname
FROM Sailors
WHERE age>=40 AND rating IS NOT NULL ;
SELECT sid, sname
FROM sailors
WHERE sid IN (1223, 2334, 3344) or
sname between(‘George’ and ‘Paul’);
![Page 48: 1 Reminder We have covered: –Creating tables –Converting ER diagrams to table definitions Today we’ll talk about: –Altering tables –Inserting and deleting](https://reader036.vdocument.in/reader036/viewer/2022062516/56649d3e5503460f94a170a0/html5/thumbnails/48.jpg)
48
User’s Table ListUser’s Table List
• ORACLE holds tables with some general information
about the tables in your database
• Such Tables are:
– Cat, user_objects
• To see the list of all your tables print:
SELECT * FROM Cat;
• To see the list of all your objects print:
SELECT object_name, timestamp,
object_type FROM user_objects;
![Page 49: 1 Reminder We have covered: –Creating tables –Converting ER diagrams to table definitions Today we’ll talk about: –Altering tables –Inserting and deleting](https://reader036.vdocument.in/reader036/viewer/2022062516/56649d3e5503460f94a170a0/html5/thumbnails/49.jpg)
49
The LIKE OperatorThe LIKE Operator
• A pattern matching operator (regular expression)
• Basic format: colname LIKE pattern– Example:
_ is a single character
% is 0 or more characters
SELECT sid FROM Sailors WHERE sname LIKE ‘R_%y’;
![Page 50: 1 Reminder We have covered: –Creating tables –Converting ER diagrams to table definitions Today we’ll talk about: –Altering tables –Inserting and deleting](https://reader036.vdocument.in/reader036/viewer/2022062516/56649d3e5503460f94a170a0/html5/thumbnails/50.jpg)
50
SQL querySQL query
SELECT S.sid
FROM Sailors S, Reserves R
WHERE S.sid = R.sid;
When would adding DISTINCT give a different result?
![Page 51: 1 Reminder We have covered: –Creating tables –Converting ER diagrams to table definitions Today we’ll talk about: –Altering tables –Inserting and deleting](https://reader036.vdocument.in/reader036/viewer/2022062516/56649d3e5503460f94a170a0/html5/thumbnails/51.jpg)
51
Are any of these the same?Are any of these the same?SELECT S.sid
FROM Sailors S, Reserves R
WHERE S.sid = R.sid;
SELECT DISTINCT R.sid
FROM Sailors S, Reserves R
WHERE S.sid = R.sid;
SELECT R.sid
FROM Reserves R
Sailors
sid sname rating age
Reserves
sid bid day
![Page 52: 1 Reminder We have covered: –Creating tables –Converting ER diagrams to table definitions Today we’ll talk about: –Altering tables –Inserting and deleting](https://reader036.vdocument.in/reader036/viewer/2022062516/56649d3e5503460f94a170a0/html5/thumbnails/52.jpg)
52
Example QueryExample Query
SELECT S.sname
FROM Sailors S, Reserves R1,
Reserves R2.
WHERE S.sid = R1.sid and
R1.sid=R2.sid and R1.bid!=R2.bid;
How would you query sailors who have reserved more than one boat?
![Page 53: 1 Reminder We have covered: –Creating tables –Converting ER diagrams to table definitions Today we’ll talk about: –Altering tables –Inserting and deleting](https://reader036.vdocument.in/reader036/viewer/2022062516/56649d3e5503460f94a170a0/html5/thumbnails/53.jpg)
53
SQL querySQL querySELECT S.sname
FROM Sailors S, Reserves R, Boats B
WHERE S.sid = R.sid and
R.bid = B.bid and
B.color = 'red'
Q: What does this return?A: Names of sailors who have reserved a
red boat.
![Page 54: 1 Reminder We have covered: –Creating tables –Converting ER diagrams to table definitions Today we’ll talk about: –Altering tables –Inserting and deleting](https://reader036.vdocument.in/reader036/viewer/2022062516/56649d3e5503460f94a170a0/html5/thumbnails/54.jpg)
54
SQL querySQL query
SELECT distinct B.color
FROM Sailors S, Reserves R, Boats B
WHERE S.sname = ‘Bob’ and
S.sid = R.sid and R.bid = B.bid
Q: How would you query the colors of boats reserved by Bob?
A:
![Page 55: 1 Reminder We have covered: –Creating tables –Converting ER diagrams to table definitions Today we’ll talk about: –Altering tables –Inserting and deleting](https://reader036.vdocument.in/reader036/viewer/2022062516/56649d3e5503460f94a170a0/html5/thumbnails/55.jpg)
55
Order Of the ResultOrder Of the Result
• The ORDER BY clause can be used to
sort results by one or more columns
• The default sorting is in ascending
order
• Can specify ASC or DESC
![Page 56: 1 Reminder We have covered: –Creating tables –Converting ER diagrams to table definitions Today we’ll talk about: –Altering tables –Inserting and deleting](https://reader036.vdocument.in/reader036/viewer/2022062516/56649d3e5503460f94a170a0/html5/thumbnails/56.jpg)
56
ExampleExample
SELECT sname, rating, age
FROM Sailors S
WHERE age > 50
ORDER BY rating ASC, age DESC
![Page 57: 1 Reminder We have covered: –Creating tables –Converting ER diagrams to table definitions Today we’ll talk about: –Altering tables –Inserting and deleting](https://reader036.vdocument.in/reader036/viewer/2022062516/56649d3e5503460f94a170a0/html5/thumbnails/57.jpg)
57
Other Relational Algebra Other Relational Algebra OperatorsOperators
• So far, we have seen selection,
projection and Cartesian product
• How do we do operators UNION and
MINUS?
![Page 58: 1 Reminder We have covered: –Creating tables –Converting ER diagrams to table definitions Today we’ll talk about: –Altering tables –Inserting and deleting](https://reader036.vdocument.in/reader036/viewer/2022062516/56649d3e5503460f94a170a0/html5/thumbnails/58.jpg)
58
Three SET OperatorsThree SET Operators
• [Query] UNION [Query]
• [Query] MINUS [Query]
• [Query] INTERSECT [QUERY]
• Note: The operators remove duplicates by default!
• How would you express intersect in Relational Algebra?
![Page 59: 1 Reminder We have covered: –Creating tables –Converting ER diagrams to table definitions Today we’ll talk about: –Altering tables –Inserting and deleting](https://reader036.vdocument.in/reader036/viewer/2022062516/56649d3e5503460f94a170a0/html5/thumbnails/59.jpg)
59
What does this return?What does this return?SELECT DISTINCT S.sname
FROM Sailors S, Reserves R, Boats B
WHERE S.sid = R.sid and
R.bid = B.bid and
(B.color = 'red' or
B.color='green')
What would happen if we replaced or by and ?
Then how can we query sailors who have reserved both a green and a red boat?
![Page 60: 1 Reminder We have covered: –Creating tables –Converting ER diagrams to table definitions Today we’ll talk about: –Altering tables –Inserting and deleting](https://reader036.vdocument.in/reader036/viewer/2022062516/56649d3e5503460f94a170a0/html5/thumbnails/60.jpg)
60
Sailors who’ve reserved red Sailors who’ve reserved red oror green boatgreen boat
SELECT S.sname
FROM Sailors S, Boats B, Reserves R
WHERE S.sid = R.sid and R.bid = B.bid and B.color = ‘red’
UNION
SELECT S.sname
FROM Sailors S, Boats B, Reserves R
WHERE S.sid = R.sid and R.bid = B.bid and B.color = ‘green’;
Would INTERSECT here give us sailors who reserved both red and
green boats?
![Page 61: 1 Reminder We have covered: –Creating tables –Converting ER diagrams to table definitions Today we’ll talk about: –Altering tables –Inserting and deleting](https://reader036.vdocument.in/reader036/viewer/2022062516/56649d3e5503460f94a170a0/html5/thumbnails/61.jpg)
61
Sailors who’ve reserved red Sailors who’ve reserved red andand green boatsgreen boats
SELECT S.sname
FROM Sailors S, Reserves R1, Reserves R2
Boats B1, Boats B2
WHERE S.sid = R1.sid and R1.bid = B1.bid and B1.color = ‘red’ and
S.sid = R2.sid and R2.bid = B2.bid and B2.color = ‘green’;
![Page 62: 1 Reminder We have covered: –Creating tables –Converting ER diagrams to table definitions Today we’ll talk about: –Altering tables –Inserting and deleting](https://reader036.vdocument.in/reader036/viewer/2022062516/56649d3e5503460f94a170a0/html5/thumbnails/62.jpg)
62
Multiset (Bag) OperatorsMultiset (Bag) Operators
• Union without removing duplicates:
UNION ALL
SELECT DISTINCT sname
FROM Sailors S
UNION ALL
SELECT DISTINCT sname
FROM Sailors S
![Page 63: 1 Reminder We have covered: –Creating tables –Converting ER diagrams to table definitions Today we’ll talk about: –Altering tables –Inserting and deleting](https://reader036.vdocument.in/reader036/viewer/2022062516/56649d3e5503460f94a170a0/html5/thumbnails/63.jpg)
63
Nested QueriesNested Queries
![Page 64: 1 Reminder We have covered: –Creating tables –Converting ER diagrams to table definitions Today we’ll talk about: –Altering tables –Inserting and deleting](https://reader036.vdocument.in/reader036/viewer/2022062516/56649d3e5503460f94a170a0/html5/thumbnails/64.jpg)
64
Nested queries in WHERENested queries in WHERE
• Equality nested query:
Select R.bid
From Sailors S, Reserves R
Where sid = (select sid from S where
sname=‘George’);
When would this work? When wouldn’t
it?
![Page 65: 1 Reminder We have covered: –Creating tables –Converting ER diagrams to table definitions Today we’ll talk about: –Altering tables –Inserting and deleting](https://reader036.vdocument.in/reader036/viewer/2022062516/56649d3e5503460f94a170a0/html5/thumbnails/65.jpg)
65
Nested queries in WHERENested queries in WHERE
SELECT S.sname
FROM Sailors S
WHERE S.sid IN (SELECT R.sid
FROM Reserves R
WHERE R.bid = 103);
Subqueries with multiple results:
Names of sailors who’ve reserved boat 103
What would happen if we wrote NOT IN?
![Page 66: 1 Reminder We have covered: –Creating tables –Converting ER diagrams to table definitions Today we’ll talk about: –Altering tables –Inserting and deleting](https://reader036.vdocument.in/reader036/viewer/2022062516/56649d3e5503460f94a170a0/html5/thumbnails/66.jpg)
66
What does this produce?What does this produce?
SELECT S.sname
FROM Sailors S
WHERE S.sid NOT IN
(SELECT R.sid
FROM Reserves R
WHERE R.bid IN
(SELECT B.bid
FROM Boats B
WHERE B.color='red'))
![Page 67: 1 Reminder We have covered: –Creating tables –Converting ER diagrams to table definitions Today we’ll talk about: –Altering tables –Inserting and deleting](https://reader036.vdocument.in/reader036/viewer/2022062516/56649d3e5503460f94a170a0/html5/thumbnails/67.jpg)
67
Set-Comparison QueriesSet-Comparison Queries
SELECT *
FROM Sailors S1
WHERE S1.age > ANY (SELECT S2.age
FROM Sailors S2);
Sailors who are not the youngest:
We can also use op ALL (op is >, <, =, >=, <=, or <>).
![Page 68: 1 Reminder We have covered: –Creating tables –Converting ER diagrams to table definitions Today we’ll talk about: –Altering tables –Inserting and deleting](https://reader036.vdocument.in/reader036/viewer/2022062516/56649d3e5503460f94a170a0/html5/thumbnails/68.jpg)
68
Correlated Nested QueriesCorrelated Nested Queries
SELECT S.sid
FROM Sailors S
WHERE EXISTS (SELECT *
FROM Reserves R
WHERE R.bid = 103 and
S.sid = R.sid);
Names of sailors who’ve reserved boat 103:
What would happen if we wrote NOT EXISTS?
S not in subquery, refers to outer loop
![Page 69: 1 Reminder We have covered: –Creating tables –Converting ER diagrams to table definitions Today we’ll talk about: –Altering tables –Inserting and deleting](https://reader036.vdocument.in/reader036/viewer/2022062516/56649d3e5503460f94a170a0/html5/thumbnails/69.jpg)
69
Exists and Not ExistsExists and Not Exists
• Differs from In and Not In by not
matching attributes.
• Exists:
For every tuple in the outer loop, the
inner loop is tested. If the inner loop
produces a result, the outer tuple is
added to the result.