Presentation is loading. Please wait.

Presentation is loading. Please wait.

DB Review.

Similar presentations


Presentation on theme: "DB Review."— Presentation transcript:

1 DB Review

2 Database A database is a collection of data
A database management system (DBMS) is software designed to assist in maintaining and utilizing a large collection of data

3 Why Database is better than a file system
Define the structure: I/O efficiency Ability to query(SQL) High availability Distributed

4 Relational Database Tables Relationships
Primary Key Fields (different Types: Int / Boolean / date / file etc..) Relationships One to many Many to one Many to many The database is kept on the disk, so anything you create will be there next time you log on.

5 Table ID(PK) Country Name 1 England Yaron Gam 2 USA Kate Omaily 3
Jack Jack

6 Problematic table Prime Minister Country Name Gordon England Yaron Gam
Bush USA Kate Omaily Jack Jack

7 What is the solution Country Name Israel Yaron Gam USA Kate Omaily
There is a One-to-One relationship between Country and Prime minister, Country Name Israel Yaron Gam USA Kate Omaily Yael Nacsh Naftali spitzer Prime Minister Country Golda Israel Bush USA

8 Rules of thumb Avoid duplication
One-to-One relationship should be in the same table with other fields.

9 SQL Structured Query Language
is a database computer language designed for the retrieval and management of data in relational database management systems (RDBMS) Database schema creation Modification Database object access control management.

10

11 Create table The basic format of the CREATE TABLE command is:
CREATE TABLE TableName( Column1 DataType1 ColConstraint, … ColumnN DataTypeN ColConstraint, TableConstraint1, … TableConstraintM );

12 Example CREATE TABLE Employee( ID INTEGER NOT NULL, Fname VARCHAR(20),
Lname VARCHAR(20), Gender CHAR(1), Salary INTEGER NOT NULL, Dept INTEGER );

13 If you type \d Employee you get:
Column Type Modifiers id integer not null fname character varying(20) lname character varying(20) gender character(1) salary integer not null dept integer

14 Constraints Different types of constraints:
* Not Null * Default Values * Unique * Primary Key * Foreign Key * Check Condition

15 Foreign Key Dept Name ManID 12 Sales 988 45 Repair 876 Department ID
FName LName Gender Sallary Dept 02334 Larry Bird M 230000 12 04556 Magic Johnson 270000 45 Foreign Key Dept Name ManID 12 Sales 988 45 Repair 876 Department

16 CREATE TABLE Employee ( ID INTEGER primary key, Fname VARCHAR(20),
Lname VARCHAR(20), Gender CHAR(1), Salary INTEGER NOT NULL, DeptNumber INTEGER REFERENCES Department ); CREATE TABLE Department( DeptNumber INTEGER PRIMARY KEY, Name VARCHAR(20), ManagerId INTEGER );

17 Deleting a table Syntax: DROP TABLE <tableName>;
Mind the order of dropping when there are foreign key constraints.

18 Insert Column Type Modifiers ----------- ------------ ------------
id integer not null Fname character varying(20) gender character(1) deptnumber integer

19 Insert syntax Syntax: insert into tablename (field1,field2…)
values (v1,’v2’,…) Examples: insert into employee (id,fname,gender,deptnumber) values(122,'Goldman','M',12); insert into employee (id,deptnumber) values(123,13);

20 Delete Syntax: DELETE FROM Table WHERE Condition; Example:
DELETE FROM Employee WHERE id = 121; WHERE Salary > ;

21 Update Syntax: UPDATE Table SET Field1=value1,,,FieldN=valueN
WHERE Condition Example: UPDATE Employee SET Salary = WHERE Salary > ;

22 Queries Syntax: SELECT [Distinct] fields FROM tables WHERE condition
condition: A Boolean condition (For example: age>21, or name=‘Yuval’ ). Only rows which return ‘true’ for this condition will appear in the result SELECT [Distinct] fields FROM tables WHERE condition ORDER BY field ASC/DESC

23 StudentID StudentDept. StudentName StudentAge 1123 Math Moshe 25 2245 Computers Mickey 26 55611 Menahem 29 Select studentID, studentName From students Where StudentDept=‘Math’ StudentID StudentName 1123 Moshe 55611 Menahem

24 The where clause 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.

25 More examples 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’); SELECT sid FROM Sailors WHERE sname LIKE ‘R%’;

26 How do we compute All sailors who have reserved a boat
sid sname rating age 22 31 58 Dustin Lubber Rusty 7 8 10 45.0 55.5 35.0 Reserves sid bid day 22 58 101 103 10/10/96 11/12/96

27 Join Select sname from sailors, reserves
Where sailors.sid=reserves.sid When there is more than one table in the from we compute a cross product

28 Stage 1: Sailors x Reserves
sid sname rating age bid day 22 Dustin 7 45.0 101 10/10/96 58 103 11/12/96 31 Lubber 8 55.5 Rusty 10 35.0

29 Stage 2: “where sailors.sid=reserves.sid”
sname rating age bid day 22 Dustin 7 45.0 101 10/10/96 58 103 11/12/96 31 Lubber 8 55.5 Rusty 10 35.0

30 Stage 2: “where sailors.sid=reserves.sid”
sname rating age bid day 22 Dustin 7 45.0 101 10/10/96 58 Rusty 10 35.0 103 11/12/96

31 Stage 3: “select sname” Sailors Reserves sid sname rating age bid day 22 Dustin 7 45.0 101 10/10/96 58 Rusty 10 35.0 103 11/12/96

32 Stage 3: “select sname” Sailors sname Dustin Rusty Final answer

33 Order Compute the cross product of the tables
Delete all rows that do not satisfy condition. Delete all columns that do not appear in fields. If Distinct is specified eliminate duplicate rows.

34 Aggregation The aggregate operators available in SQL are:
COUNT(*) COUNT([DISTINCT] A) SUM([DISTINCT] A) AVG([DISTINCT] A) MAX(A) MIN(A) SELECT Max(S.age) FROM Sailors S

35 Nested quires Query inside a query
Select First_Name from Internet_grades where Grade >= (Select Max(Grade) from Infi_grades);


Download ppt "DB Review."

Similar presentations


Ads by Google