Presentation is loading. Please wait.

Presentation is loading. Please wait.

SQL. Internet technologies – Ohad © Database  A database is a collection of data  A database management system (DBMS) is software designed to assist.

Similar presentations


Presentation on theme: "SQL. Internet technologies – Ohad © Database  A database is a collection of data  A database management system (DBMS) is software designed to assist."— Presentation transcript:

1 SQL

2 Internet technologies – Ohad © 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 Internet technologies – Ohad © Why Database is better than a file system  Simple, efficient queries(SQL)  concurrency control ( few threads changing the same data)  data modeling and structure  Disk accesses frequent, pretty slow

4 Internet technologies – Ohad © Relational Database  Tables  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 Internet technologies – Ohad © DB planning goals  Efficient space  Consistency  Restrictions  Data mining ability

6 Internet technologies – Ohad © Problematic table Prime MinisterCountryName GordonEnglandYaron Gam BushUSAKate Omaily ObamaUSAJack

7 Internet technologies – Ohad © What is the solution  There is a One-to-One relationship between Country and Prime minister, CountryName IsraelYaron Gam USAKate Omaily IsraelYael Nacsh IsraelNaftali spitzer Prime MinisterCountry GoldaIsrael BushUSA

8 Internet technologies – Ohad © Rules of thumb  Avoid duplication  One-to-One relationship should be in the same table with other fields.

9 Internet technologies – Ohad © 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 Internet technologies – Ohad ©

11 CS Lab  In order to connect to it, print the following command in your shell: psql –hdbserver public  \q exit psql  \h [command]help about ‘command’  \d [name] describe table/index/… called ‘name’  \dtlist tables  \di list indexes  \dv list views  \df list functions  \i fileName

12 Internet technologies – Ohad © Create table The basic format of the CREATE TABLE command is: CREATE TABLE TableName( Column1 DataType1 ColConstraint, … ColumnN DataTypeN ColConstraint, TableConstraint1, … TableConstraintM );

13 Internet technologies – Ohad © Example CREATE TABLE Employee( ID INTEGER NOT NULL, Fname VARCHAR(20), Lname VARCHAR(20), Gender CHAR(1), Salary INTEGER NOT NULL, Dept INTEGER );

14 Internet technologies – Ohad © If you type \d Employee you get: Column Type Modifiers ----------- ------------ ------------ idinteger not null fnamecharacter varying(20) lnamecharacter varying(20) gender character(1) salary integer not null deptinteger

15 Internet technologies – Ohad © Constraints  Different types of constraints:  * Not Null* Default Values  * Unique * Primary Key  * Foreign Key* Check Condition

16 Internet technologies – Ohad © Foreign Key IDFNameLNameGenderSallaryDept 02334LarryBirdM23000012 04556MagicJohnsonM27000045 Foreign Key DeptNameManID 12Sales988 45Repair876 Department

17 Internet technologies – Ohad © 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 );

18 Internet technologies – Ohad © Deleting a table  Syntax: DROP TABLE ;  Mind the order of dropping when there are foreign key constraints.

19 Internet technologies – Ohad © Insert Column Type Modifiers ----------- ------------ ------------ idinteger not null Fnamecharacter varying(20) gender character(1) deptnumberinteger

20 Internet technologies – Ohad © 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);

21 Internet technologies – Ohad © Delete  Syntax: DELETE FROM Table WHERE Condition;  Example:  DELETE FROM Employee WHERE id = 121; DELETE FROM Employee WHERE Salary > 100000;

22 Internet technologies – Ohad © Update  Syntax: UPDATE Table SET Field1=value1,,,FieldN=valueN WHERE Condition  Example: UPDATE Employee SET Salary = 100000 WHERE Salary > 100000;

23 Internet technologies – Ohad © Queries  Syntax:  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

24 Internet technologies – Ohad © StudentIDStudentDept.StudentNameStudentAge 1123MathMoshe25 2245ComputersMickey26 55611MathMenahem29 Select studentID, studentName From students Where StudentDept=‘Math’ StudentIDStudentName 1123Moshe 55611Menahem

25 Internet technologies – Ohad © 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.

26 Internet technologies – Ohad © 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%’;

27 Internet technologies – Ohad © Reserves sidbidday 22 58 101 103 10/10/96 11/12/96 Sailors sidsnameratingage 22 31 58 Dustin Lubber Rusty 7 8 10 45.0 55.5 35.0 How do we computer All sailors who have reserved a boat

28 Internet technologies – Ohad © Join Select sname from sailors, reserves Where sailors.sid=reserves.sid When there is more than one table in there from part we computer a cross product

29 Internet technologies – Ohad © SailorsReserves sidsnameratingagesidbidday 22Dustin745.02210110/10/96 22Dustin745.05810311/12/96 31Lubber855.52210110/10/96 31Lubber855.55810311/12/96 58Rusty1035.02210110/10/96 58Rusty1035.05810311/12/96 Stage 1: Sailors x Reserves

30 Internet technologies – Ohad © SailorsReserves sidsnameratingagesidbidday 22Dustin745.02210110/10/96 22Dustin745.05810311/12/96 31Lubber855.52210110/10/96 31Lubber855.55810311/12/96 58Rusty1035.02210110/10/96 58Rusty1035.05810311/12/96 Stage 2: “where sailors.sid=reserves.sid”

31 Internet technologies – Ohad © SailorsReserves sidsnameratingagesidbidday 22Dustin745.02210110/10/96 58Rusty1035.05810311/12/96 Stage 2: “where sailors.sid=reserves.sid”

32 Internet technologies – Ohad © SailorsReserves sidsnameratingagesidbidday 22Dustin745.02210110/10/96 58Rusty1035.05810311/12/96 Stage 3: “select sname”

33 Internet technologies – Ohad © Sailors sname Dustin Rusty Stage 3: “select sname” Final answer

34 Internet technologies – Ohad © Order 1. Compute the cross product of the tables 2. Delete all rows that do not satisfy condition. 3. Delete all columns that do not appear in fields. 4. If Distinct is specified eliminate duplicate rows.

35 Internet technologies – Ohad © 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

36 Internet technologies – Ohad © Nested quires  Query inside a query Select First_Name from Internet_grades where Grade >= (Select Max(Grade) from Infi_grades);


Download ppt "SQL. Internet technologies – Ohad © Database  A database is a collection of data  A database management system (DBMS) is software designed to assist."

Similar presentations


Ads by Google