DB Review
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
Why Database is better than a file system Define the structure: I/O efficiency Ability to query(SQL) High availability Distributed
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.
Table ID(PK) Country Name 1 England Yaron Gam 2 USA Kate Omaily 3 Jack Jack
Problematic table Prime Minister Country Name Gordon England Yaron Gam Bush USA Kate Omaily Jack Jack
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
Rules of thumb Avoid duplication One-to-One relationship should be in the same table with other fields.
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.
Create table The basic format of the CREATE TABLE command is: CREATE TABLE TableName( Column1 DataType1 ColConstraint, … ColumnN DataTypeN ColConstraint, TableConstraint1, … TableConstraintM );
Example CREATE TABLE Employee( ID INTEGER NOT NULL, Fname VARCHAR(20), Lname VARCHAR(20), Gender CHAR(1), Salary INTEGER NOT NULL, Dept INTEGER );
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
Constraints Different types of constraints: * Not Null * Default Values * Unique * Primary Key * Foreign Key * Check Condition
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
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 );
Deleting a table Syntax: DROP TABLE <tableName>; Mind the order of dropping when there are foreign key constraints.
Insert Column Type Modifiers ----------- ------------ ------------ ----------- ------------ ------------ id integer not null Fname character varying(20) gender character(1) deptnumber integer
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);
Delete Syntax: DELETE FROM Table WHERE Condition; Example: DELETE FROM Employee WHERE id = 121; WHERE Salary > 100000;
Update Syntax: UPDATE Table SET Field1=value1,,,FieldN=valueN WHERE Condition Example: UPDATE Employee SET Salary = 100000 WHERE Salary > 100000;
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
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
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.
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%’;
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
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
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
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
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
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
Stage 3: “select sname” Sailors sname Dustin Rusty Final answer
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.
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
Nested quires Query inside a query Select First_Name from Internet_grades where Grade >= (Select Max(Grade) from Infi_grades);