Lesson 01 - Introduction to SQL


Published on

Published in: Education
  • Be the first to comment

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Lesson 01 - Introduction to SQL

  1. 1. Introduction to SQL Fundamentals Lesson 1 COIS 420 © 2007 Angel G. Diaz
  2. 2. Relational Database System • Basic Database Terminology • Relational Database Concepts • Types of Relationships • Types of Keys • Integrity Rules • Relational Languages © 2007 Angel G. Diaz
  3. 3. Basic TerminologyEntity – a person, place, event, or itemData – facts describing an entityAttributes – characteristics of an entityEntity Set – a collection of related entitiesDatabase – a collection of entity setsRelationship – interaction between entity sets © 2007 Angel G. Diaz
  4. 4. Types of RelationshipsOne-to-one relationship (1:1)Exists between two entity sets X and Y, if an entity in X has onlyone matching entity in Y, and vice versa. For example,Department and Manager.One-to-many relationship (1:M)Exists between two entity sets X and Y, if an entity in X has onlyone matching entity in Y, but an entity in Y has many matchingentities in X. For example, Department and Employee.Many-to-many relationship (M:N)Exists between two entity sets X and Y, if an entity in X has onlymany matching entity in Y, and vice versa. For example, Courseand Student. © 2007 Angel G. Diaz
  5. 5. Database Management System (DBMS)•A software package such as Oracle or IBM DB2.•Manages data and relationships in the database.•Creates a Data Dictionary to store Metadata – data about data.•Manages all day-to-day transactions.•Provides user with data independence at application level.•Transforms logical data requests to match physical data structures.•Secures access through passwords, restricted user access, and encryption.•Provides backup and recovery mechanisms.•Provides export and import utilities.•Allows sharing of data with locking capabilities. © 2007 Angel G. Diaz
  6. 6. Relational ModelRelation – two-dimensional flat tableTuple – a row or a recordAttribute – a column or a fieldDegree – number of attributes in a tableDomain – all possible values of an attributeKey – a minimal set of attributes used touniquely define a row/entity © 2007 Angel G. Diaz
  7. 7. Types of KeysPrimary keyA single attribute used as a unique identifierComposite keyTwo or more attributes used as a unique identifierSecondary keyA non-key attribute used in the search operationSurrogate keyAn added attribute used as a primary keyForeign keyAn attribute that references primary key of a table © 2007 Angel G. Diaz
  8. 8. Integrity RulesEntity integrity: No primary key attribute may have a null value. A primary key value must be unique and not null. A null value is not known, not entered, not defied, or not applicable. A space or a zero is not a null value.Referential integrity: A foreign key value may be null. A foreign key value must exist as a primary key value in another table. © 2007 Angel G. Diaz
  9. 9. Theoretical Relational Languages E.F. Codd suggested two theoretical languages with his proposed relational model in 1970. 1. Relational Algebra. A procedural language. 2. Relational Calculus. A non-procedural language. Relational Operations. Union Intersection Difference Selection Projection Product Join Assignment Division © 2007 Angel G. Diaz
  10. 10. Data Modeling• Database Modeling Techniques• E-R Diagrams• Dependency• Redundancy Problem• Normal forms• Normalization © 2007 Angel G. Diaz
  11. 11. Data ModelingModeling is simplified version of real-life complexobjects.The Entity-Relationship (E-R) model is a very popularmodeling tool.The E-R model provides: •An excellent communication tool. •A simple graphical representation of a database. © 2007 Angel G. Diaz
  12. 12. E-R Diagram•An entity (or entity set) is represented by a rectangle. The name of the entity iswritten within the rectangle in a singular upper-case noun. For example,EMPLOYEE.•The relationship between two entity sets is represented in a diamond-shapedbox. The name of relationship is an active verb in lower-case. For example,supervises.•The types of relationship is called connectivity.•The lower and upper limit of relationship is called cardinality.•The optional relationship is shown with a small circle next to the optionalentity.•The decomposition of a M:N relationship into two 1:M relationships involves athird composite entity, which is shown with a diamond in a rectangle.•The weak entities, which cannot exist by themselves, are shown with double-lined rectangles. © 2007 Angel G. Diaz
  13. 13. Types of AttributesSimple Attribute.Attribute that cannot be subdivided. For example, city, gender, or last name.Composite Attribute.Attribute that can be sub divided. For example, name or mailing address.Single-valued Attribute.Attribute with a single-value per entity. For example, first name or employeenumber.Multi-valued Attribute.Attribute with multiple values per entity. For example, student’s courses. © 2007 Angel G. Diaz
  14. 14. Table with Multi-valued AttributesInvNo InvDate CustNo ItemNo Cust Item Item Qty Name Name Price1001 04/14/00 212 1 Starks Screw $2.25 5 3 Bolt $3.99 5 5 Washer $1.99 91002 04/17/00 225 1 Connors Screw $2.25 2 2 Nut $5.00 31003 04/17/00 239 1 Kapur Screw $2.25 7 2 Nut $5.00 11004 04/18/00 211 4 Garcia Hammer $9.99 5 © 2007 Angel G. Diaz
  15. 15. DependenciesThe primary key describes an entity.The non-key attributes are functionally dependent on he primarykey attribute(s). Types of DependenciesTotal – a non-key attribute is totally dependent on primary keyattributes.Partial – a non-key attribute is dependent on part of the primarykey.Transitive – a non-key attribute is dependent on another non-keyattribute. © 2007 Angel G. Diaz
  16. 16. RedundancyRedundancy means• Repeated data entry.• Repeated changes in many places.• More storage space.• Deletion anomaly – deletion of one entity results indeletion of another entity.• Insertion anomaly – Insertion of one entity depends oninformation about another entity. © 2007 Angel G. Diaz
  17. 17. Normal FormsFirst Normal Form (1NF) •Table’s primary key is defined. •All non-key attributes show functional dependency on the primary key components. •Table does not contain multi-valued attributes.Second Normal Form (2NF) •All 1NF requirements are satisfied. •There is no partial dependency.Third Normal Form (3NF) •All 2NF requirements are fulfilled. •There is no transitive dependency. © 2007 Angel G. Diaz
  18. 18. INVOICE Table in 1NFInvNo InvDate CustNo ItemNo CustName ItemName ItemPrice Qty1001 04/14/00 212 1 Starks Screw $2.25 51001 04/14/00 212 3 Starks Bolt $3.99 51001 04/14/00 212 5 Starks Washer $1.99 91002 04/17/00 225 1 Connors Screw $2.25 21002 04/17/00 225 2 Connors Nut $5.00 31003 04/17/00 239 1 Kapur Screw $2.25 71003 04/17/00 239 2 Kapur Nut $5.00 11004 04/18/00 211 4 Garcia Hammer $9.99 5 Attributes InvDate, CustNo and CustName are partially dependent on InvNo. Attributes ItemName and ItemPrice are partially dependent on ItemNo. Attribute Qty is totally dependent on InvNo and ItemNo. Attribute CustName shows transitive dependency on CustNo. © 2007 Angel G. Diaz
  19. 19. Normalization• A process of decomposing a table into many tables. • A process to increase table’s normal form. • A process to reduce redundancy. The higher the normal form is, the lower the redundancy. © 2007 Angel G. Diaz
  20. 20. 1NF to 2NF(Removal of partial dependencies)•Write each primary key component (with partial dependency on it)on a separate line.•Write composite key on the next line.INVOICE table decomposed into 2NF:INV (InvNo, InvDate, CustNo, CustName)ITEM (ItemNo, ItemName, ItemPrice)INVITEM (InvNo, ItemNo, Qty) © 2007 Angel G. Diaz
  21. 21. 2NF to 3NF (Removal of transitive dependencies)•Move attributes with transitive dependency to a new table.•Keep the primary key of the new table as the foreign key in theexisting table.Database in 3NF: INV (InvNo, InvDate, CustNo) CUSTOMER (CustNo, CustName) ITEM (ItemNo, ItemName, ItemPrice) INVITEM (InvNo, ItemNo, Qty) © 2007 Angel G. Diaz
  22. 22. Database Tools• Personal Databases• Client/Server Databases• Oracle and its Tools• Types of SQL Statements © 2007 Angel G. Diaz
  23. 23. Personal Databases• Usually stored on a client computer• Primarily for single-user applications• Stored on a server for multi-user or shared access• A server is a computer that accepts and services requests from clients. A server’s resources include hard disk, programs, data, or printers.• For example, Microsoft-Access and Visual Fox Pro © 2007 Angel G. Diaz
  24. 24. Characteristics of a Personal Database System• Heavy traffic on network to transport applications and databases between server and clients• Heavy demand on Client computer• Inferior system response time• No automatic table locking, but optimistic locking• Client failure may result in record locking and database corruption• Transaction log kept in client’s memory © 2007 Angel G. Diaz
  25. 25. Client/Server Databases• Usually stored on the server computer• Primarily for multi-user applications• Allows shared-access with locking• An Oracle server accepts and services requests from clients.• The client computer does not contain database and does not perform any processing• For example, Oracle, DB2, SAP, etc. © 2007 Angel G. Diaz
  26. 26. Characteristics of aClient/Server Database System• Low network traffic as only client requests and results are transported• Low demand on client as server processes requests• Automatic locking• If a client fails, other clients not affected• Central Transaction Log kept on server• If a batch transaction fails, entire transaction is rolled back © 2007 Angel G. Diaz
  27. 27. Oracle and its Tools• A Client/Server database system• A object-relational database system• Oracle Corp is the second largest software company in the world ToolsSQL*Plus – environment to write command-line SQL queriesPL/SQL – Oracle’s procedural language extension to SQLQuery Builder – graphical environment for SQL query buildingDeveloper – Form Builder, Report Builder, Graphic BuilderEnterprise Manager – a tool to manage database and users. For example, StorageManager, Instance Manager and Security ManagerOracle Web Application Server – to access Oracle databases through web pages © 2007 Angel G. Diaz
  28. 28. Structured Query Language (SQL) • Universal query language for relational databases • Standardized and accepted by ANSI and ISO • A non-procedural, fourth generation, high-level language Types of SQL statements Data Definition Language (DDL) – creates, changes, removes table’s structure For example, CREATE, ALTER, DROP, RENAME, and TRUNCATE Data Manipulation Language (DML) – inserts, changes and removes rows For example, INSERT, UPDATE, and DELETE Data Retrieval – retrieves data from database, For example, SELECT Transaction Control Language (TCL) – manages logical transactions For example, COMMIT, SAVEPOINT, and ROLLBACK Data Control Language (DCL) – gives and removes rights to objects For example, GRANT and REVOKE © 2007 Angel G. Diaz
  29. 29. End of Lesson 1 © 2007 Angel G. Diaz