Presentation is loading. Please wait.

Presentation is loading. Please wait.

Overview Begin 6:00 Quiz15 mins6:15 Review Table Terms25 mins6:40 Short Break10 mins6:50 SQL: Creating Tables60 mins7:50 Break10 mins8:00 Lab – Creating.

Similar presentations


Presentation on theme: "Overview Begin 6:00 Quiz15 mins6:15 Review Table Terms25 mins6:40 Short Break10 mins6:50 SQL: Creating Tables60 mins7:50 Break10 mins8:00 Lab – Creating."— Presentation transcript:

1 Overview Begin 6:00 Quiz15 mins6:15 Review Table Terms25 mins6:40 Short Break10 mins6:50 SQL: Creating Tables60 mins7:50 Break10 mins8:00 Lab – Creating Tables30 mins8:30 1

2 Key Review Key –all possible combinations of columns Super Key –All possible key combinations that uniquely identify records Primary Key –The super key that we chose to uniquely identify each record Foreign Key –Links the table to another table’s Primary Key 2

3 QUIZ 3

4 Overview Begin 6:00 Quiz15 mins6:15 Review Table Terms25 mins6:40 Short Break10 mins6:50 SQL: Creating Tables60 mins7:50 Break10 mins8:00 Lab – Creating Tables30 mins8:30 4

5 Term Review DBMS Relation / Entity / Table Row / Tuple / Record Column / Attribute Primary Key Foreign Key First Normal Form Domain NULL Values Keys Super Key Composite Key 5

6 66 Elmasri Company Database The company is organized into DEPARTMENTS. Each department has a name, number and an employee who manages the department –We keep track of the start date of the department manager –Departments can have multiple locations

7 77 Elmasri Company Database Each department controls a number of PROJECTs. Each project has a name, number and is located at a single location.

8 88 Elmasri Company Database For each EMPLOYEE, we store the social security number, address, salary, sex, and birthdate. Employees may have a supervisor –DIFFERENT FROM (and no connection to) manager Each employee works for one department but may work on several projects.

9 99 Elmasri Company Database We keep track of the number of hours per week that an employee currently works on each project. Each employee may have a number of DEPENDENTs. –For each dependent, we keep track of their name, sex, birthdate, and relationship to employee.

10 10 Elmasri Company Database What will be the different tables ? What will be the primary keys ? What will be the foreign keys ? An example instance

11 11 Elmasri COMPANY database schema

12 12 Elmasri database with FK: Figure 5.7

13 13 Elmasri Figure 5.6: Relational Instance

14 14 In class problem: relational schemas Database keeps track of student enrollment in courses and the books adopted for each course: STUDENT(SSN, Name, Major, Bdate) COURSE(Course#, Cname, Dept) ENROLL(SSN, Course#, Quarter, Grade) BOOK_ADOPTION(Course#, Quarter, Book_ISBN) TEXT(Book_ISBN, Book_Title, Publisher, Author)

15 15 In class problem: relational schemas Why is Quarter part of P.Key in Enroll ? Suppose Book_ISBN was part of P.K. in the BOOK_ADOPTION table –How would we interpret that ? Draw relational schema specifying foreign keys.

16 10 Min Break 16

17 Overview Begin 6:00 Quiz15 mins6:15 Review Table Terms25 mins6:40 Short Break10 mins6:50 SQL: Creating Tables60 mins7:50 Break10 mins8:00 Lab – Creating Tables30 mins8:30 17

18 18 QBE/ SQL QBE : Query By Example –Visual approach to writing queries –Used in MS-Access & SQL Server SQL: Structured Query Language –Developed by IBM in the 1970s –Need for a standard since it is used by many vendors –Various revisions over the years

19 19 SQL DDL and DML DDL: data definition language –Creating tables –Altering tables –Deleting (dropping) tables DML: data manipulation language – Retrieving (selecting) data – Inserting, deleting, updating data

20 20 SQL DDL and DML Our plan of action: – SQL DDL – SQL DML – SQL DDL again

21 21 Creating Tables in SQL CREATE TABLE: Creates a new relation by giving it a name, and specifying each of its attributes and their data types (domain) (INTEGER, FLOAT, DECIMAL(i,j), CHAR(n), VARCHAR(n), DATE).

22 22 Creating Tables in SQL DBMS enforces domain: like type checking CREATE TABLE Students( name VARCHAR(20), st_num CHAR(2), class INTEGER, major VARCHAR(10))

23 23 CREATE TABLE NOT NULL can be specified on an attribute Can specify DEFAULT Can specify primary key, secondary keys, and foreign keys. (Secondary key is a key used for sorting data – sometimes called an INDEX)

24 24 CREATE TABLE CREATE TABLE DEPT ( Dname varchar(10) Not Null, DnumberintegerNot Null Default 1 Primary Key MgrSSNchar(9) Not Null References EMP(SSN), MgrStartDtdate);

25 25 Elmasri Figure 8.1: CREATE TABLE

26 26 Creating tables in MSSQL CREATE TABLE MyTable( SSN char(9) Primary Key, LName varchar(10) Not Null); Duplicate values not allowed in SSN –Why? Null value not allowed if Not Null SQL Server Online Books for CREATE TABLE

27 27 Composite P. Keys in MSSQL CREATE TABLE MyTable2 ( SSN char(9), LNAME varchar(10), CONSTRAINT [PK_MyFKTable] Primary Key ([SSN],[Lname])); Is this valid? –{ “001”, “Johnson” } –{ “001”, “Klemenz” } –{ “002”, “Johnson” }

28 28 Foreign Keys in MS SQL CREATE TABLE MyFKTable ( DName varchar(10) Not Null, DNumber integer Not NullPrimary Key, MgrSSN char(9) REFERENCES JUNK45(SSN)); Non-valid values : string for DNUMBER Foreign key violation

29 29 Integrity Constraints Integrity Constraint: condition that must be true for any instance of the database: – are specified when schema is defined. – are checked when relations are modified.

30 30 Integrity Constraints Legal Instance: of a relation is one that satisfies all specified Integrity Constraints – DBMS should not allow illegal instances. Since the DBMS checks Integrity Constraints, stored data better captures real-world meaning. – Also avoids data entry errors

31 31 Integrity constraints Domain Constraint: value has to be from domain Key Constraint: key has to be unique Entity Integrity Constraint: no part of a key can be null. Referential Integrity Constraint: foreign key value must be either null or match the primary key value.

32 Design Considerations Why are integrity checks good? Why are integrity checks bad? 32

33 33 Referential Integrity Violations If change made to referencing table which leads to referential integrity violation, what to do? –Eg: added non-existent employee’s dependent Have to reject If change made to referenced table which leads to referential integrity violation, what to do? –Eg: employee 123 leaving the company, what to do about 123’s dependent information ? In this case, SQL provides different options – i.e. can specify what to do.

34 34 REF. INTEGRITY OPTIONS ON DELETE/UPDATE If there is a referential integrity violation on a delete or update in referenced table, SQL supports NO ACTION/REJECT: this is default SET DEFAULT: default value spec. earlier SET NULL : not possible if NOT NULL CASCADE: will delete or update in the referencing table We look at our own example –Similar to Elmasri Fig. 7.1b example

35 35 REF. INTEGRITY OPTIONS ON DELETE/UPDATE CREATE TABLE DEPT (DNamevarchar(10) Not Null Unique, DNumber integer Not Null Primary Key, MgrSSNchar(9) Not Null References EMP(SSN) ON DELETE NO ACTION ON UPDATE CASCADE, MgrStartDtchar(9));

36 36 REF. INTEGRITY OPTIONS ON DELETE/UPDATE CREATE TABLE EMP ( EName varchar(30) Not Null, ESSNchar(9) Primary Key, BirthDtdate, DNOint default 1 References Dept(Dnumber) On Delete Set Default On Update Cascade, SuperSSNchar(9) References EMP(SSN) On Delete SET Null On Update Cascade);

37 37 Date/Time Data Types SQL has DATE, TIME, and TIMESTAMP data types DATE: Made up of year-month-day in the format yyyy-mm-dd TIME: Made up of hour:minute:second in the format hh:mm:ss TIMESTAMP: Has both DATE and TIME components

38 38 Modifying and Deleting Tables Alter TableAlter LinkAlter Link Modifies tables Drop TableDrop LinkDrop Link Deletes tables Please read the SQL Server books online for more info

39 10 Min Break 39

40 Overview Begin 6:00 Quiz15 mins6:15 Review Table Terms25 mins6:40 Short Break10 mins6:50 SQL: Creating Tables60 mins7:50 Break10 mins8:00 Lab – Creating Tables30 mins8:30 40

41 41 Lab Install MS SQL Create a database Create a table Show how to manually add data Add primary key to table Show how to add multiple primary keys to table Add an “auto-number” column (Right click in design view) Relationships and link the tables Show Script database to show how to create / view the SQL


Download ppt "Overview Begin 6:00 Quiz15 mins6:15 Review Table Terms25 mins6:40 Short Break10 mins6:50 SQL: Creating Tables60 mins7:50 Break10 mins8:00 Lab – Creating."

Similar presentations


Ads by Google