CS 142 Lecture Notes: Relational Databases

Slides:



Advertisements
Similar presentations
SQL Lecture 10 Inst: Haya Sammaneh. Example Instance of Students Relation  Cardinality = 3, degree = 5, all rows distinct.
Advertisements

2010/11 : [1]Building Web Applications using MySQL and PHP (W1)MySQL Recap.
Cs3431 Constraints Sections 6.1 – 6.5. cs3431 Example CREATE TABLE Student ( sNum int, sName varchar (20), prof int, CONSTRAINT pk PRIMARY KEY (snum),
CS 142 Lecture Notes: Rails ActiveRecordSlide 1 Model for Student Table SELECT * FROM students; | id | name.
Structured Query Language Review ISYS 650. Language Overview Three major components: –Data definition language, DDL Create, Drop and Alter Tables or Views.
CS 142 Lecture Notes: Relational DatabasesSlide 1 Relation (Table) namebirthgpagrad Anderson Jones Hernandez
Murali Mani SQL DDL and Oracle utilities. Murali Mani Datatypes in SQL INT (or) INTEGER FLOAT (or) REAL DECIMAL (n, m) CHAR (n) VARCHAR (n) DATE, TIME.
Introduction to Information and Computer Science
1 CS428 Web Engineering Lecture 23 MySQL Basics (PHP - VI)
 SQL stands for Structured Query Language.  SQL lets you access and manipulate databases.  SQL is an ANSI (American National Standards Institute) standard.
Constraints  Constraints are used to enforce rules at table level.  Constraints prevent the deletion of a table if there is dependencies.  The following.
Relational Database Management Systems. A set of programs to manage one or more databases Provides means for: Accessing the data Inserting, updating and.
1 Structured Query Language (SQL). 2 Contents SQL – I SQL – II SQL – III SQL – IV.
CS 142 Lecture Notes: Relational DatabasesSlide 1 Relation (Table) namebirthgpagrad Anderson Jones Hernandez
SQL Basics. 5/27/2016Chapter 32 of 19 Naming SQL commands are NOT case sensitive SQL commands are NOT case sensitive But user identifier names ARE case.
CS 142 Lecture Notes: Rails ActiveRecordSlide 1 Model for Student Table SELECT * FROM students; | id | name.
CS 142 Lecture Notes: Rails ActiveRecordSlide 1 Model for Student Table SELECT * FROM students; | id | name.
Dec 8, 2003Murali Mani Constraints B term 2004: lecture 15.
CS 142 Lecture Notes: Rails ActiveRecordSlide 1 Model for Student Table SELECT * FROM students; | id | name.
CS34311 The Relational Model. cs34312 Why Relational Model? Currently the most widely used Vendors: Oracle, Microsoft, IBM Older models still used IBM’s.
SQL Miscellaneous Topics. Views A database view is: – a virtual or logical table based on a query. – a stored query. CREATE VIEW viewname AS query; –CREATE.
Lectures 2&3: Introduction to SQL. Lecture 2: SQL Part I Lecture 2.
SQL constrains and keys. SORTED RESULTS Sort the results by a specified criterion SELECT columns FROM tables WHERE predicates ORDER BY column ASC/DESC;
DATABASES.
Spring 2011 ITCS3160: Database Design and Implementation Hands-on Learning.
Introduction to Databases & SQL Ahmet Sacan. What you’ll need Firefox, SQLite plugin Mirdb and Targetscan databases.
SQL CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
SQL Server for Developers Using SQL Server, DB Design, SQL SELECT, INSERT, UPDATE, DELETE SoftUni Team Technical Trainers Software University
 Database – collection of persistent data  Database Management System (DBMS) – software system that supports creation, population, and querying of a.
Standard language for querying and manipulating data Structured Query Language Many standards out there: ANSI SQL, SQL92 (a.k.a. SQL2), SQL99 (a.k.a. SQL3),
PostgreSQL S511.
Web Systems & Technologies
MySQL S511.
Logical DB Design: ER to Relational
Chapter 3 Introduction to SQL
Lecture#6: Fun with SQL (Part 1)
Insert, Update and the rest…
SQL in Oracle.
Referential Integrity MySQL
CS311 Database Management system
Principles of Software Development
Web Programming Week 3 Old Dominion University
Relation (Table) Row/Tuple/Record Column/Attribute/Field name birth
Lecture#7: Fun with SQL (Part 2)
Referential Integrity
Relation (Table) Row/Tuple/Record Column/Attribute/Field name birth
The Relational Model Relational Data Model
Model for Student Table
MySQL Tables and Relations 101
Relational Databases Relational Algebra (1) Select, project, join.
Translation of ER-diagram into Relational Schema
For student, require values for name and address.
Referential Integrity
File Processing with Excel’s List
CMPT 354: Database System I
CMPT 354: Database System I
Rob Gleasure robgleasure.com
Lectures 3: Introduction to SQL 2
Web Programming Week 3 Old Dominion University
MySQL.
MySQL S511.
Data Definition Language
Lecture 24: Creating a Database and More joins
Model for Student Table
Lectures 2: Introduction to SQL 1
Instructor: SAMIA ARSHAD
MySQL Database System Installation Overview SQL summary
Web Programming Week 3 Old Dominion University
កម្មវិធីបង្រៀន SQL Programming ជាភាសាខ្មែរ Online SQL Training Course
Lecture 22: Creating a Database and More joins
Presentation transcript:

CS 142 Lecture Notes: Relational Databases Relation (Table) Row/Tuple/Record Column/Attribute/Field name birth gpa grad Anderson 1987-10-22 3.9 2009 Jones 1990-4-16 2.4 2012 Hernandez 1989-8-12 3.1 2011 Chen 1990-2-4 3.2 VARCHAR(30) DATE FLOAT INT Column Types CS 142 Lecture Notes: Relational Databases

CS 142 Lecture Notes: Relational Databases Primary Key Unique For Each Row id name birth gpa grad 14 Anderson 1987-10-22 3.9 2009 38 Jones 1990-4-16 2.4 2012 77 Hernandez 1989-8-12 3.1 2011 104 Chen 1990-2-4 3.2 INT VARCHAR(30) DATE FLOAT CS 142 Lecture Notes: Relational Databases

Basic Table Operations CREATE TABLE students ( id INT AUTO_INCREMENT, name VARCHAR(30), birth DATE, gpa FLOAT, grad INT, PRIMARY KEY(id)); INSERT INTO students(name, birth, gpa, grad) VALUES ('Anderson', '1987-10-22', 3.9, 2009); VALUES ('Jones', '1990-4-16', 2.4, 2012); DELETE FROM students WHERE name='Anderson'; DROP TABLE students; CS 142 Lecture Notes: Relational Databases

Query: Display Entire Table id name birth gpa grad 1 Anderson 1987-10-22 3.9 2009 2 Jones 1990-4-16 2.4 2012 3 Hernandez 1989-8-12 3.1 2011 4 Chen 1990-2-4 3.2 SELECT * FROM students; +----+-----------+------------+------+------+ | id | name | birth | gpa | grad | | 1 | Anderson | 1987-10-22 | 3.9 | 2009 | | 2 | Jones | 1990-04-16 | 2.4 | 2012 | | 3 | Hernandez | 1989-08-12 | 3.1 | 2011 | | 4 | Chen | 1990-02-04 | 3.2 | 2011 | CS 142 Lecture Notes: Relational Databases

CS 142 Lecture Notes: Relational Databases Query: Select Columns id name birth gpa grad 1 Anderson 1987-10-22 3.9 2009 2 Jones 1990-4-16 2.4 2012 3 Hernandez 1989-8-12 3.1 2011 4 Chen 1990-2-4 3.2 SELECT name, gpa FROM students; +-----------+------+ | name | gpa | | Anderson | 3.9 | | Jones | 2.4 | | Hernandez | 3.1 | | Chen | 3.2 | CS 142 Lecture Notes: Relational Databases

CS 142 Lecture Notes: Relational Databases Query: Filter Rows id name birth gpa grad 1 Anderson 1987-10-22 3.9 2009 2 Jones 1990-4-16 2.4 2012 3 Hernandez 1989-8-12 3.1 2011 4 Chen 1990-2-4 3.2 SELECT name, gpa FROM students WHERE gpa > 3.0; +-----------+------+ | name | gpa | | Anderson | 3.9 | | Hernandez | 3.1 | | Chen | 3.2 | CS 142 Lecture Notes: Relational Databases

CS 142 Lecture Notes: Relational Databases Query: Sort Output id name birth gpa grad 1 Anderson 1987-10-22 3.9 2009 2 Jones 1990-4-16 2.4 2012 3 Hernandez 1989-8-12 3.1 2011 4 Chen 1990-2-4 3.2 SELECT gpa, name, grad FROM students WHERE gpa > 3.0 ORDER BY gpa DESC; +------+-----------+------+ | gpa | name | grad | | 3.9 | Anderson | 2009 | | 3.2 | Chen | 2011 | | 3.1 | Hernandez | 2011 | CS 142 Lecture Notes: Relational Databases

CS 142 Lecture Notes: Relational Databases Update Value(s) id name birth gpa grad 1 Anderson 1987-10-22 3.9 2009 2 Jones 1990-4-16 2.4 2012 3 Hernandez 1989-8-12 3.1 2011 4 Chen 1990-2-4 3.2 UPDATE students SET gpa = 2.6, grad = 2013 WHERE id = 2 CS 142 Lecture Notes: Relational Databases

CS 142 Lecture Notes: Relational Databases Foreign Key id name birth gpa grad advisor_id 1 Anderson 1987-10-22 3.9 2009 2 Jones 1990-4-16 2.4 2012 3 Hernandez 1989-8-12 3.1 2011 4 Chen 1990-2-4 3.2 id name title 1 Fujimura assocprof 2 Bolosky prof advisors students SELECT s.name, s.gpa FROM students s, advisors p WHERE s.advisor_id = p.id AND p.name = 'Fujimura'; s.id s.name s.birth s.gpa s.grad s.advisor_id p.id p.name p.title 1 Anderson 1987-10-22 3.9 2009 2 Fujimura assocprof Bolosky prof Jones 1990-4-16 2.4 2012 3 Hernandez 1989-8-12 3.1 2011 4 Chen 1990-2-4 3.2 CS 142 Lecture Notes: Relational Databases

CS 142 Lecture Notes: Relational Databases id name birth gpa grad advisor_id 1 Anderson 1987-10-22 3.9 2009 2 Jones 1990-4-16 2.4 2012 3 Hernandez 1989-8-12 3.1 2011 4 Chen 1990-2-4 3.2 id name title 1 Fujimura assocprof 2 Bolosky prof advisors students SELECT s.name, s.gpa FROM students s, advisors p WHERE s.advisor_id = p.id AND p.name = 'Fujimura'; s.id s.name s.birth s.gpa s.grad s.advisor_id p.id p.name p.title 1 Anderson 1987-10-22 3.9 2009 2 Fujimura assocprof Bolosky prof Jones 1990-4-16 2.4 2012 3 Hernandez 1989-8-12 3.1 2011 4 Chen 1990-2-4 3.2 CS 142 Lecture Notes: Relational Databases

CS 142 Lecture Notes: Relational Databases id name birth gpa grad advisor_id 1 Anderson 1987-10-22 3.9 2009 2 Jones 1990-4-16 2.4 2012 3 Hernandez 1989-8-12 3.1 2011 4 Chen 1990-2-4 3.2 id name title 1 Fujimura assocprof 2 Bolosky prof advisors students SELECT s.name, s.gpa FROM students s, advisors p WHERE s.advisor_id = p.id AND p.name = 'Fujimura'; +-----------+------+ | name | gpa | | Jones | 2.4 | | Hernandez | 3.1 | | Chen | 3.2 | CS 142 Lecture Notes: Relational Databases

CS 142 Lecture Notes: Relational Databases id name birth gpa grad 1 Anderson 1987-10-22 3.9 2009 2 Jones 1990-4-16 2.4 2012 3 Hernandez 1989-8-12 3.1 2011 4 Chen 1990-2-4 3.2 course_id student_id 1 3 4 2 students courses_students id number name quarter 1 CS142 Web stuff Winter 2009 2 ART101 Finger painting Fall 2008 3 4 PE204 Mud wrestling courses SELECT s.name, c.quarter FROM students s, courses c, courses_students cs WHERE c.id = cs.course_id AND s.id = cs.student_id AND c.number = 'ART101'; +----------+-------------+ | name | quarter | | Jones | Fall 2008 | | Chen | Fall 2008 | | Anderson | Winter 2009 | CS 142 Lecture Notes: Relational Databases

CS 142 Lecture Notes: Relational Databases