Presentation is loading. Please wait.

Presentation is loading. Please wait.

CMPT 258 Database Systems The Relationship Model (Chapter 3)

Similar presentations


Presentation on theme: "CMPT 258 Database Systems The Relationship Model (Chapter 3)"— Presentation transcript:

1 CMPT 258 Database Systems The Relationship Model (Chapter 3)

2 Relational Database: Definitions Relational database: a set of relations with distinct relation names ▫Database: a set of tables with distinct table names

3 Relational Database: Definitions Relation (table): made up of 2 parts: ▫Schema : specifies name of relation (table), plus name and type of each column.  E.G. Students(sid: string, name: string, login: string, age: integer, gpa: real). ▫Instance : a table, with rows and columns.  Can think of a relation as a set of rows or tuples (i.e., all rows are distinct). #Rows = cardinality, #fields = degree

4 Example Instance of Students Relation  Cardinality = 3, degree = 5, all rows distinct  Do all columns in a relation instance have to be distinct? TUPLES (RECORDS, ROWS) FIELDS (ATTRIBUTES, COLUMNS)

5 Relational Query Languages A major strength of the relational model ▫supports simple, powerful querying of data. Structured query language (SQL) Developed by IBM in the 1970s SQL Standard (ANSI) since it is used by many vendors

6 6 The SQL Query Language DDL (Data Definition Language) ▫A subset of SQL that supports the creation, deletion, and modification of tables ▫Commands CREATE, DROP, and ALTER are used for data definition DML (Data Manipulate Language) ▫The SELECT-FROM-WHERE structure of SQL queries SELECT FROM WHERE

7 DDL Create database Create table Alter table Drop table 7

8 Creating Databases Create database db_name; ▫A new database will be created if not exists. ▫e.g. Create database db_university; Use db_name; ▫e.g. Use db_university;

9 Creating tables

10 Creating Tables in SQL Creates the Students relation. ▫Observe that the type (domain) of each field is specified, and enforced by the DBMS whenever tuples are added or modified. As another example, the Enrolled table holds information about courses that students take. CREATE TABLE Students (sid CHAR(20), name CHAR(20), login CHAR(10), age INTEGER, gpa REAL ) CREATE TABLE Enrolled (sid CHAR(20), cid CHAR(20), grade CHAR (2))

11 Data Types and Domains Basic data types ▫Numeric data types  Integer numbers: INTEGER or INT, and SMALLINT  Floating-point (real) numbers: FLOAT and REAL or DOUBLE PRECISION ▫Character-string data types  Fixed length: CHAR(n) or CHARACTER(n)  Varying length : VARCHAR(n) or CHAR VARYING(n), CHARACTER VARYING(n)

12 Data Types and Domains ▫Bit-string data types  Fixed length: BIT(n)  Varying length: BIT VARYING(n) ▫Boolean data type  Values of TRUE or FALSE or NULL (the Boolean data is UNKNOWN ) ▫DATE data type  Ten positions  Components are YEAR, MONTH, and DAY in the form YYYY-MM-DD

13 DML: Adding Tuples Can insert a single tuple using: ▫ INSERT INTO TABLE_NAME [ (col1, col2, col3,...colN)] VALUES (value1, value2,value3,...valueN); INSERT INTO Students (sid, name, login, age, gpa) VALUES (53688, ‘Smith’, ‘smith@ee’, 18, 3.2) We can optionally omit the list of column name in the INTO clause and list the values in the appropriate order.

14 DML: Adding Tuples INSERT INTO Students (sid, name, login, age, gpa) VALUES (53688, ‘Smith’, ‘smith@ee’, 18, 3.2) INSERT INTO Students VALUES (53688, ‘Smith’, ‘smith@ee’, 18, 3.2)

15 Altering Tables ALTER TABLE tbl_name ADD COLUMN col_name domain DROP COLUMN col_name e.g., ALTER TABLE Students ADD COLUMN firstYear integer The schema of Students is altered by adding a new field; every tuple in the current instance is extended with a null value in the new field.

16 DML: updating data within a table Modify attribute values of one or more selected tuples Additional SET clause in the UPDATE command ▫Specifies attributes to be modified and new values UPDATE table_name SET column_name1 = value1, column_name2 = value2,... [WHERE condition]

17 The WHERE Clause Logical comparison operators ▫ =,, >=, and <> Projection attributes ▫Attributes whose values are to be retrieved Selection condition ▫Boolean condition that must be true for any retrieved tuple

18 DML: updating data within a table UPDATE table_name SET column_name1 = value1, column_name2 = value2,... [WHERE condition]

19 DML: updating data within a table

20

21 Exercises Use database university Create another table called Enrolled. ▫Enrolled(sid, cname) Add two tuples each for Joe and Paul Add another column called grade in Enrolled with a default value of ‘B’ Change Joe’s databases grade to ‘A-’ Change Paul’s data structures grade to ‘A’

22 Use AND to combine two or more conditions in the WHERE clause Is it case sensitive in MySQL when comparing strings? What if we want everyone in the databases class to get an ‘A’? What happens when we omit the WHERE clause? Joe dropped Data Structures and he took Algorithms instead and he got a D :( What is S.sid?

23 DML: Deleting Tuples Removes tuples from a relation ▫Includes a WHERE clause to select the tuples to be deleted DELETE FROM table_name [WHERE condition]; What happens when we omit the where clause?

24 DML: Deleting Tuples Can delete all tuples satisfying some condition (e.g., name = Smith): ▫ DELETE FROM Students WHERE name = ‘Smith’ What if nothing satisfies the condition?

25 Destroying Tables DROP TABLE tbl_name; ▫ e.g., DROP TABLE Students; ▫ e.g., DROP TABLE Enrolled; Destroys the relation Students. The schema information and the tuples are deleted.

26 Basic SQL Queries SELECT statement ▫One basic statement for retrieving information from a database

27 Basic SQL Queries To find all 18 year old students, we can write: SELECT * FROM Students WHERE age=18 Specify an asterisk (*) Retrieve all the attribute values of the selected tuples

28 Basic SQL Queries SELECT * FROM Students Missing WHERE clause Indicates no condition on tuple selection

29 Basic SQL Queries To find the names and logins of all 18 year old students : Find all the ids of students and courses in which they got an A. SELECT name, login FROM Students WHERE age=18

30 Querying Multiple Relations Find all the names of students and courses in which they got an A. SELECT S.name, E.cid FROM Students S, Enrolled E WHERE S.sid=E.sid AND E.grade=‘A’ Given the following instances of Enrolled and Students: we get:

31 Ambiguous Attribute Names Same name can be used for two (or more) attributes ▫As long as the attributes are in different relations ▫Must qualify the attribute name with the relation name to prevent ambiguity SELECT S.name, E.cid FROM Students S, Enrolled E WHERE S.sid=E.sid AND E.grade=‘A’

32 CROSS PRODUCT ▫All possible tuple combinations

33 SQL Data Definition Language (DDL) statements are used to define the database structure or schema. Some examples: ▫CREATE - to create objects in the database ▫ALTER - alters the structure of the database ▫DROP - delete objects from the database 33

34 SQL Data Manipulation Language (DML) statements are used for managing data within schema objects. Some examples: ▫SELECT - retrieve data from the a database ▫INSERT - insert data into a table ▫UPDATE - updates existing data within a table ▫DELETE - deletes all records from a table, the space for the records remain 34

35 Readings Chapter 3


Download ppt "CMPT 258 Database Systems The Relationship Model (Chapter 3)"

Similar presentations


Ads by Google