SQL CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.

Slides:



Advertisements
Similar presentations
Murach's MySQL, C1© 2012, Mike Murach & Associates, Inc.Slide 1.
Advertisements

Murach’s Java SE 6, C21© 2007, Mike Murach & Associates, Inc.Slide 1.
Mid-term Exam Answer Keys Week 7 Spring I. Multiple Choices Questions Each may have more than one answer (6 x 4 = 24 points total)
CS 104 Introduction to Computer Science and Graphics Problems Introduction to Database (2) Basic SQL 12/05/2008 Yang Song.
SQL Keys and Constraints Justin Maksim. Key Declaration Key constraint defined within the CREATE TABLE command Key can be declared using either the PRIMARY.
SQL DDL constraints Restrictions on the columns and tables 1SQL DDL Constraints.
CSC 2720 Building Web Applications Database and SQL.
1 Foundations of Software Design Lecture 27: Java Database Programming Marti Hearst Fall 2002.
DAT702.  Standard Query Language  Ability to access and manipulate databases ◦ Retrieve data ◦ Insert, delete, update records ◦ Create and set permissions.
PHP1-1 PHP & SQL Xingquan (Hill) Zhu
CSCI 6962: Server-side Design and Programming
Web Application Development. Define ER model in QSEE Generate SQL Create Database mySQL Write Script to use TableEditor class Process to create A simple.
Web Application Development. Tools to create a simple web- editable database QSEE MySQL (or PHPMyAdmin) PHP TableEditor.
Software Engineering CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
Databases in Visual Studio. Database in VisualStudio An MS SQL database are built in Visual studio The Name can be something like ”(localdb)\Projects”
Constraints  Constraints are used to enforce rules at table level.  Constraints prevent the deletion of a table if there is dependencies.  The following.
Stored Procedures, Triggers, Program Access Dr Lisa Ball 2008.
SQL pepper. Why SQL File I/O is a great deal of code Optimal file organization and indexing is critical and a great deal of code and theory implementation.
15/10/20151 PHP & MySQL 'Slide materials are based on W3Schools PHP tutorial, 'PHP website 'MySQL website.
Introduction to MySQL Lab no. 10 Advance Database Management System.
SQL pepper. Why SQL File I/O is a great deal of code Optimal file organization and indexing is critical and a great deal of code and theory implementation.
CSC 2720 Building Web Applications Database and SQL.
Onsdag The concepts in a relation data model SQL DDL DML.
FEN  Data Definition: CREATE TABLE, ALTER TABLE  Data Manipulation: INSERT, UPDATE, DELETE  Queries: SELECT SQL: Structured Query Language.
1 Structured Query Language (SQL). 2 Contents SQL – I SQL – II SQL – III SQL – IV.
Advanced Database Management System
CS 160: Software Engineering October 6 Class Meeting Department of Computer Science San Jose State University Fall 2014 Instructor: Ron Mak
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.
Chapter 5 MYSQL Database. Introduction to MYSQL MySQL is the world's most popular open-source database. Open source means that the source code, the programming.
Chapter 8 Databases.
SQL: DDL. SQL Statements DDL - data definition language –Defining and modifying data structures (metadata): database, tables, views, etc. DML - data manipulation.
Chapter 25 Databases. Chapter Scope Database concepts Tables and queries SQL statements Managing data in a database Java Foundations, 3rd Edition, Lewis/DePasquale/Chase25.
JDBC Tutorial CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
Oracle & SQL Introduction. Database Concepts Revision DB? DBMS? DB Application? Application Programs? DBS? Examples of DBS? Examples of DBMS? 2Oracle.
Conditions CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
SQL Basics. What is SQL? SQL stands for Structured Query Language. SQL lets you access and manipulate databases.
SQL Structured Query Language 1. Data Definition Language (DDL) is used to manage table and define data structure i.e. CREATE, ALTER, DROP Data Control.
What’s a database? Data stored in a structured format that lends itself to easy manipulation and recall.
Chapter 8 Part 2 SQL-99 Schema Definition, Constraints, Queries, and Views.
SQL John Nowobilski. What is SQL? Structured Query Language Manages Data in Database Management Systems based on the Relational Model Developed in 1970s.
>> Introduction to MySQL. Introduction Structured Query Language (SQL) – Standard Database Language – Manage Data in a DBMS (Database Management System)
There are two types of MySQL instructions (Data Definition Language) DDL: Create database, create table, alter table,,,. (Data Manipulation Language) DML.
Class11 Introduction to relational databases and MySQL MIS 3501, Fall 2015 Brad Greenwood, PhD MBA Department of MIS Fox School of Business Temple University.
ECMM6018 Enterprise Networking For Electronic Commerce Tutorial 6 CGI/Perl and databases.
Drill Consider the following tables with the following fields: Student: FName, LName, StudentID, Age, Yr, Course Grades: ID, P1, P2, P3 1.Display the.
SQL constrains and keys. SORTED RESULTS Sort the results by a specified criterion SELECT columns FROM tables WHERE predicates ORDER BY column ASC/DESC;
Constraints and Views Chap. 3-5 continued (7 th ed. 5-7)
SQL pepper. Why SQL File I/O is a great deal of code Optimal file organization and indexing is critical and a great deal of code and theory implementation.
Databases and SQL CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
CS320 Web and Internet Programming SQL and MySQL Chengyu Sun California State University, Los Angeles.
Understand Data Definition Language (DDL) Database Administration Fundamentals LESSON 1.4.
Introduction to Databases & SQL Ahmet Sacan. What you’ll need Firefox, SQLite plugin Mirdb and Targetscan databases.
JDBC. Database is used to store data permanently. These days almost all Applications needs database to store its data persistently. Below are the most.
Introduction to Database Programming with Python Gary Stewart
 MySQL is a database system used on the web  MySQL is a database system that runs on a server  MySQL is ideal for both small and large applications.
CCT395, Week 6 Implementing a Database with SQL and a Case Study Exercise This presentation is licensed under Creative Commons Attribution License, v.
MySQL S511.
Databases.
CS320 Web and Internet Programming SQL and MySQL
Insert, Update and the rest…
SQL in Oracle.
Session 4 PHP & MySQL.
Principles of Software Development
SQL Code for Byrnetube video
Data Management Innovations 2017 High level overview of DB
MySQL.
MySQL S511.
CS3220 Web and Internet Programming SQL and MySQL
CS3220 Web and Internet Programming SQL and MySQL
JDBC II IS
Presentation transcript:

SQL CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L

Outline USC CSCI 201L2/12 ▪SQL ▪Try It!

Relational Databases ▪Write the SQL code to create the following database, named StudentGrades USC CSCI 201L3/12 Databases Tables are called relations Columns are called attributes Rows are called tuples Connections are called relationships classIDprefixnumber 1CSCI103 2CSCI104 3CSCI201 4EE101 5EE102 studentIDfnamelname 1SheldonCooper 2LeonardHofstadter 3HowardWolowitz 4RajeshKoothrappali gradeIDclassIDstudentIDletterGrade 111A 223A- 332A 433B 553B-

Sample Script USC CSCI 201L4/12 Network Serialization 1 DROP DATABASE if exists StudentGrades; 2 CREATE DATABASE StudentGrades; 3 USE StudentGrades; 4 CREATE TABLE Student ( 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 Student (fname, lname) VALUES ('Sheldon', 'Cooper'); 10 INSERT INTO Student (fname, lname) VALUES ('Leonard', 'Hofstadter'); 11 CREATE TABLE Class ( 12 classID int(11) primary key not null auto_increment, 13 prefix varchar(5) not null, 14 number int(4) not null 15 ); 16 INSERT INTO Class (prefix, number) VALUES ('CSCI', 103); 17 INSERT INTO Class (prefix, number) 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, 'A');

Executing SQL ▪There are a few ways to execute SQL statements, which typically depend on the DBMS ▪MySQL has the MySQL Workbench and command line options ▪We often want to be able to access databases from within programs though ›This can be to insert, update, delete, or query the data ▪The Java Database Connectivity (JDBC) drivers allow us to embed SQL in our Java code and execute those statements on a database programmatically USC CSCI 201L5/12 JDBC

SQL Statements ▪Write SQL code to do the following ›Insert CSCI 170 into the database ›Change Leonard’s grade in EE 101 to a C+ ›Add Amy Farrah Fowler into the Student table ›Give Amy a grade of A for CSCI 103 ›Give Amy a grade of B+ for CSCI 170 USC CSCI 201L6/12 Databases

SQL Queries Solution ▪Write SQL code to do the following ›Insert CSCI 170 into the database INSERT INTO class (prefix, number) VALUES (‘CSCI’, 170); ›Change Leonard’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='Leonard' AND s.lname='Hofstadter' AND g.classID=c.classID AND c.prefix='EE' AND c.number='101'; ›Add Amy Farrah Fowler into the Student 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 number=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 number=170), (SELECT studentID FROM student WHERE fname='Amy' AND lname='Farrah Fowler'), ‘B+'); USC CSCI 201L7/12 Databases

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 and grade for each student in CSCI 103 ›Print out the class and grades for Sheldon Cooper ›Print out the name, grade, and class for all students in all of the classes USC CSCI 201L8/12 Databases

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 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.number=201; ›Print out the class and grades for Sheldon Cooper SELECT c.prefix, c.number, 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'; ›Print out the name, grade, and class for all students in all of the classes SELECT s.fname, s.lname, c.prefix, c.number, 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.number; USC CSCI 201L9/12 Databases