SQL CSCI 201 Principles of Software Development

Similar documents
Databases CSCI 201 Principles of Software Development

Abstract Classes Interfaces CSCI 201 Principles of Software Development

Abstract Classes Interfaces CSCI 201 Principles of Software Development

Create a simple database with MySQL

Draft. Students Table. FName LName StudentID College Year. Justin Ennen Science Senior. Dan Bass Management Junior

CSC 215 PROJECT 2 DR. GODFREY C. MUGANDA

Databases and SQL Lab EECS 448

Referential integrity

Advanced SQL. Nov 21, CS445 Pacific University 1

Networks and Web for Health Informatics (HINF 6220)

CMPT 354: Database System I. Lecture 2. Relational Model

Database Management Systems. Chapter 5

Introduction to MySQL /MariaDB and SQL Basics. Read Chapter 3!

CSCI Design of Database Systems, Fall 2016

Concurrency CSCI 201 Principles of Software Development

Introduction to Database Systems CSE 344

MySQL. A practical introduction to database design

Previously everyone in the class used the mysql account: Username: csci340user Password: csci340pass

Database Systems CSE 414

Previously everyone in the class used the mysql account: Username: csci340user Password: csci340pass

Database Management Systems. Chapter 5

Database Systems CSE 414

Basic SQL. Basic SQL. Basic SQL

Chapter 4: SQL Basics

Sleeping Barber CSCI 201 Principles of Software Development

Unit 27 Web Server Scripting Extended Diploma in ICT

GlobAl EDITION. Database Concepts SEVENTH EDITION. David M. Kroenke David J. Auer

Introduction to Database Systems CSE 344

Database Systems. S. Adams. Dilbert. Available: Hans-Petter Halvorsen

Web Database Programming

PHP: Cookies, Sessions, Databases. CS174. Chris Pollett. Sep 24, 2008.

School of Computing and Information Technology Session: Spring CSCI835 Database Systems (Bridging Subject) Sample class test 23 July 2018

NEED FOR SPEED: BEST PRACTICES FOR MYSQL PERFORMANCE TUNING JANIS GRIFFIN PERFORMANCE EVANGELIST / SENIOR DBA

CS143: Relational Model

Introduction to Database Systems CSE 344

Accessing databases in Java using JDBC

CSCI-UA: Database Design & Web Implementation. Professor Evan Sandhaus

CSCI-UA: Database Design & Web Implementation. Professor Evan Sandhaus Lecture #23: SQLite

COMP519: Web Programming Autumn 2015

CS 327E Lecture 5. Shirley Cohen. February 8, 2016

COSC344 Database Theory and Applications. Lecture 6 SQL Data Manipulation Language (1)

CS2300: File Structures and Introduction to Database Systems

JavaScript CSCI 201 Principles of Software Development

Spring % of course grade


CSE 344 FEBRUARY 14 TH INDEXING

MySQL Data Modeling Tutorial

Web Database Programming

Hash table example. B+ Tree Index by Example Recall binary trees from CSE 143! Clustered vs Unclustered. Example

MySQL Installation Guide (Windows)

A practical introduction to database design

SQL language. Jaroslav Porubän, Miroslav Biňas, Milan Nosáľ (c)

Basic SQL. Dr Fawaz Alarfaj. ACKNOWLEDGEMENT Slides are adopted from: Elmasri & Navathe, Fundamentals of Database Systems MySQL Documentation

3/3/2008. Announcements. A Table with a View (continued) Fields (Attributes) and Primary Keys. Video. Keys Primary & Foreign Primary/Foreign Key

SQL: Concepts. Todd Bacastow IST 210: Organization of Data 2/17/ IST 210

MySQL Introduction. By Prof. B.A.Khivsara

Relational Database Management Systems for Epidemiologists: SQL Part II

UBC Graduate Information System (GIS)

Networking Code CSCI 201 Principles of Software Development

Introduction to Database Systems CSE 414. Lecture 26: More Indexes and Operator Costs

Polymorphism CSCI 201 Principles of Software Development

MySQL Installation Guide (Windows)

Concurrent Computing CSCI 201 Principles of Software Development

Database Views & Stored Procedures. Hans-Petter Halvorsen, M.Sc.

Chapter 8. Joined Relations. Joined Relations. SQL-99: Schema Definition, Basic Constraints, and Queries

Database Systems. phpmyadmin Tutorial

Database Systems CSci 4380 Final Exam May 19, 2016 SOLUTIONS

Introduction to ERwin

ÇALIŞMA TEST SORULARI

Data Base Lab. The Microsoft SQL Server Management Studio Part-3- By :Eng.Alaa I.Haniy.

Creating databases using SQL Server Management Studio Express

Exploring Microsoft Office Access Chapter 2: Relational Databases and Multi-Table Queries

More MySQL ELEVEN Walkthrough examples Walkthrough 1: Bulk loading SESSION

Selections. Lecture 4 Sections Robb T. Koether. Hampden-Sydney College. Wed, Jan 22, 2014

UBC Graduate Information System (GIS)

FIT 100 More Microsoft Access and Relational Databases Creating Views with SQL

The DBMS accepts requests for data from the application program and instructs the operating system to transfer the appropriate data.

Review. Objec,ves. Example Students Table. Database Overview 3/8/17. PostgreSQL DB Elas,csearch. Databases

ASSIGNMENT NO 2. Objectives: To understand and demonstrate DDL statements on various SQL objects

CSCI-UA: Database Design & Web Implementation. Professor Evan Sandhaus Lecture #17: MySQL Gets Done

Downloaded from

Referential Integrity and Other Table Constraints Ray Lockwood

School of Computing and Information Technology. Examination Paper Autumn 2016

SET UNIQUE_CHECKS=0; SET FOREIGN_KEY_CHECKS=0;

JSP CSCI 201 Principles of Software Development

Database Setup - Local

COP4540 TUTORIAL PROFESSOR: DR SHU-CHING CHEN TA: H S IN-YU HA

Using OpenESQL to Map COBOL to DBMS Data Types

Exception Handling CSCI 201 Principles of Software Development

CS 338 Basic SQL Part II

Database Systems CSE 414

Structures. Lecture 15 COP 3014 Spring April 16, 2018

SQL: A COMMERCIAL DATABASE LANGUAGE. Data Change Statements,

Database in Everyday Life by Assoc. Prof. Dr. Churee Techawut adapted into English by Dr. Prakarn Unachak

Outline. Textbook Chapter 6. Note 1. CSIE30600/CSIEB0290 Database Systems Basic SQL 2

Department of Computer Science University of Cyprus. EPL342 Databases. Lab 2

SQL Server and SQL Structured Query Language

CSIE30600 Database Systems Basic SQL 2. Outline

Server-Configuration-2-MySQL-1-HW.docx CSCI 3343 Initials P a g e 1

Creating the Data Layer

Transcription:

SQL CSCI 201 Principles of Software Development Jeffrey Miller, Ph.D. jeffrey.miller@usc.edu

Outline SELECT Statements Try It USC CSCI 201L

SELECT Statements SELECT statements are probably the most commonly used SQL statement A SELECT statement returns a table Consider the following database 3/23

SELECT Example 1 SELECT * FROM WHERE studentid > 2; 4/23

SELECT Example 1 SELECT * FROM WHERE studentid > 2; 5/23

SELECT Example 1 SELECT * FROM WHERE studentid > 2; 6/23

SELECT Example 1 SELECT * FROM WHERE studentid > 2; 7/23

SELECT Example 2 SELECT * FROM s, g WHERE fname = Howard ; 8/23

SELECT Example 2 SELECT * FROM s, g WHERE fname = Howard ; 9/23

SELECT Example 2 SELECT * FROM s, g WHERE fname = Howard ; 10/23

SELECT Example 2 SELECT * FROM s, g WHERE fname = Howard ; 11/23

SELECT Example 3 SELECT s.fname, g.lettergrade FROM s, g WHERE fname = Howard AND s.studentid=g.studentid; 12/23

SELECT Example 3 SELECT s.fname, g.lettergrade FROM s, g WHERE fname = Howard AND s.studentid=g.studentid; 13/23

SELECT Example 3 SELECT s.fname, g.lettergrade FROM s, g WHERE fname = Howard AND s.studentid=g.studentid; 14/23

SELECT Example 3 SELECT s.fname, g.lettergrade FROM s, g WHERE fname = Howard AND s.studentid=g.studentid; 15/23

SELECT Example 3 SELECT s.fname, g.lettergrade FROM s, g WHERE fname = Howard AND s.studentid=g.studentid; fname lettergrade Howard A- Howard B 16/23

Outline SELECT Statements Try It USC CSCI 201L

Database Creation Create the following database. 3 3 2 A 5 5 3 B- 18/23

Sample Script 1 DROP DATABASE if exists ; 2 CREATE DATABASE ; 3 USE ; 4 CREATE TABLE ( 5 studentid int(11) primary key not null auto_increment, 6 fname varchar(50) not null, 7 lname varchar(50) not null 8 ); 9 INSERT INTO (fname, lname) VALUES ('Sheldon', 'Cooper'); 10 INSERT INTO (fname, lname) VALUES ('Leonard', 'Hofstadter'); 11 CREATE TABLE ( 12 classid int(11) primary key not null auto_increment, 13 prefix varchar(5) not null, 14 num int(4) not null 15 ); 16 INSERT INTO (prefix, num) VALUES ('CSCI', 103); 17 INSERT INTO (prefix, num) VALUES ('CSCI', 104); 18 CREATE TABLE Grade ( 19 gradeid int(11) primary key not null auto_increment, 20 classid int(11) not null, 21 studentid int(11) not null, 22 lettergrade varchar(2) not null, 23 FOREIGN KEY fk1(classid) REFERENCES class(classid), 24 FOREIGN KEY fk2(studentid) REFERENCES student(studentid) 25 ); 26 INSERT INTO Grade (studentid, classid, lettergrade) VALUES (1, 1, 'A'); 27 INSERT INTO Grade (studentid, classid, lettergrade) VALUES (1, 2, 'A'); 28 INSERT INTO Grade (studentid, classid, lettergrade) VALUES (2, 1, 'A'); 29 INSERT INTO Grade (studentid, classid, lettergrade) VALUES (2, 2, B'); Grade 2 2 1 A 3 1 2 A 4 2 2 B USC CSCI 201L 19/23

SQL Queries Write SQL code to do the following Print out all of the classes offered Print out all of the students without the studentid value Print out the name, grade, and class for all students in all of the classes Print out the name and grade for each student in CSCI 201 Print out the classes and grades for Sheldon Cooper USC CSCI 201L 20/23

SQL Queries Solution Write SQL code to do the following Print out all of the classes offered SELECT * FROM class; Print out all of the students without the studentid value SELECT fname, lname FROM student; Print out the name, grade, and class for all students in all of the classes SELECT s.fname, s.lname, c.prefix, c.num, g.lettergrade FROM student s, grade g, class c WHERE c.classid=g.classid AND s.studentid=g.studentid ORDER BY s.fname, s.lname, c.prefix, c.num; Print out the name and grade for each student in CSCI 103 SELECT s.fname, s.lname, g.lettergrade FROM student s, grade g, class c WHERE s.studentid=g.studentid AND g.classid=c.classid AND c.prefix='csci' AND c.num=103; Print out the class and grades for Sheldon Cooper SELECT c.prefix, c.num, g.lettergrade FROM student s, grade g, class c WHERE c.classid=g.classid AND s.studentid=g.studentid AND s.fname= Sheldon' AND s.lname= Cooper'; USC CSCI 201L 21/23

SQL Statements Write SQL code to do the following Insert CSCI 170 into the database Change Howard s grade in CSCI 201 to a C+ Add Amy Farrah Fowler into the table Give Amy a grade of A for CSCI 103 Give Amy a grade of B+ for CSCI 170 Note: To be able to perform an UPDATE in MySQL Workbench, you must disable Safe Updates by going to Edit->Preferences->SQL Editor and unchecking the Safe Updates box. USC CSCI 201L 22/23

SQL Queries Solution Write SQL code to do the following Insert CSCI 170 into the database INSERT INTO class (prefix, num) VALUES ( CSCI, 170); Change Howard s grade in EE 101 to a C+ UPDATE grade g, student s, class c SET g.lettergrade='c+' WHERE s.studentid=g.studentid AND s.fname='howard' AND s.lname='wolowitz' AND g.classid=c.classid AND c.prefix='csci' AND c.num='201'; Add Amy Farrah Fowler into the table INSERT INTO student (fname, lname) VALUES ('Amy', 'Farrah Fowler'); Give Amy a grade of A for CSCI 103 INSERT INTO grade (classid, studentid, lettergrade) VALUES ((SELECT classid FROM class WHERE prefix='csci' AND num=103), (SELECT studentid FROM student WHERE fname='amy' AND lname='farrah Fowler'), 'A'); Give Amy a grade of B+ for CSCI 170 INSERT INTO grade (classid, studentid, lettergrade) VALUES ((SELECT classid FROM class WHERE prefix='csci' AND num=170), (SELECT studentid FROM student WHERE fname='amy' AND lname='farrah Fowler'), B+'); USC CSCI 201L 23/23