Databases and 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

CS 104 Introduction to Computer Science and Graphics Problems Introduction to Database (2) Basic SQL 12/05/2008 Yang Song.
CS320 Web and Internet Programming SQL and MySQL Chengyu Sun California State University, Los Angeles.
A Guide to MySQL 3. 2 Objectives Start MySQL and learn how to use the MySQL Reference Manual Create a database Change (activate) a database Create tables.
CSC 2720 Building Web Applications Database and SQL.
1 Foundations of Software Design Lecture 27: Java Database Programming Marti Hearst Fall 2002.
Kirkwood Center for Continuing Education Introduction to PHP and MySQL By Fred McClurg, Copyright © 2010 All Rights Reserved. 1.
DAT702.  Standard Query Language  Ability to access and manipulate databases ◦ Retrieve data ◦ Insert, delete, update records ◦ Create and set permissions.
Prepared by Jennifer Kreie, New Mexico State UniversityHosted by the University of Arkansas Microsoft Enterprise Consortium SQL Fundamentals Introduction.
Databases Dan Otero Alex Loddengaard
Phil Brewster  One of the first steps – identify the proper data types  Decide how data (in columns) should be stored and used.
Advance Computer Programming Java Database Connectivity (JDBC) – In order to connect a Java application to a database, you need to use a JDBC driver. –
CSCI 6962: Server-side Design and Programming
Chapter 9 SQL and RDBMS Part C. SQL Copyright 2005 Radian Publishing Co.
1 Overview of Databases. 2 Content Databases Example: Access Structure Query language (SQL)
CIS 270—Application Development II Chapter 25—Accessing Databases with JDBC.
Database Technical Session By: Prof. Adarsh Patel.
Introduction to SQL Steve Perry
Web Server Administration Chapter 7 Installing and Testing a Programming Environment.
1 11/3/05CS360 Windows Programming Databases and Data Representation.
© 2009 Pearson Education, Inc. Publishing as Prentice Hall 1 UNIT 6: Chapter 7: Introduction to SQL Modern Database Management 9 th Edition Jeffrey A.
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.
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.
Database Fred Durao What is a database? A database is any organized collection of data. Some examples of databases you may encounter in.
SQL Structured Query Language Programming Course.
Kirkwood Center for Continuing Education Introduction to PHP and MySQL By Fred McClurg, Copyright © 2015, Fred McClurg, All Rights.
FEN  Data Definition: CREATE TABLE, ALTER TABLE  Data Manipulation: INSERT, UPDATE, DELETE  Queries: SELECT SQL: Structured Query Language.
Lecture2: Database Environment Prepared by L. Nouf Almujally 1 Ref. Chapter2 Lecture2.
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.
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.
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.
Course FAQ’s I do not have any knowledge on SQL concepts or Database Testing. Will this course helps me to get through all the concepts? What kind of.
Visual Programing SQL Overview Section 1.
SQL John Nowobilski. What is SQL? Structured Query Language Manages Data in Database Management Systems based on the Relational Model Developed in 1970s.
CS453: Databases and State in Web Applications (Part 2) Prof. Tom Horton.
DATABASE CONNECTIVITY TO MYSQL. Introduction =>A real life application needs to manipulate data stored in a Database. =>A database is a collection of.
Database Design And Implementation. Done so far… Started a design of your own data model In Software Engineering, recognised the processes that occur.
Introduction to Databases Three File Processing Systems DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 1-2.
ECMM6018 Enterprise Networking For Electronic Commerce Tutorial 6 CGI/Perl and databases.
Distribution of Marks For Second Semester Internal Sessional Evaluation External Evaluation Assignment /Project QuizzesClass Attendance Mid-Term Test Total.
Chapter 3: Relational Databases
CSCI 3327 Visual Basic Chapter 13: Databases and LINQ UTPA – Fall 2011.
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.
Introduction to Core Database Concepts Getting started with Databases and Structure Query Language (SQL)
CS 440 Database Management Systems Stored procedures & OR mapping 1.
CS320 Web and Internet Programming SQL and MySQL Chengyu Sun California State University, Los Angeles.
Introduction to MySQL  Working with MySQL and MySQL Workbench.
uses of DB systems DB environment DB structure Codd’s rules current common RDBMs implementations.
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.
SQL CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
Introduction to Database Programming with Python Gary Stewart
Fundamental of Database Systems
CS320 Web and Internet Programming SQL and MySQL
SQL in Oracle.
CS311 Database Management system
Principles of Software Development
Database.
CMPT 354: Database System I
SQL pepper.
CS3220 Web and Internet Programming SQL and MySQL
CS3220 Web and Internet Programming SQL and MySQL
CS4540 Special Topics in Web Development SQL and MS SQL
Presentation transcript:

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

Outline USC CSCI 201L2/12 ▪D▪Databases ▪S▪SQL ▪T▪Try It!

Databases ▪Database systems store data and provide a means of accessing, updating, manipulating, and analyzing data ▪A Database Management Systems (DBMS) is designed for programmers, not casual users ›MySQL ›Oracle ›Microsoft SQL Server ›IBM DB2 ›PostgreSQL USC CSCI 201L3/12 Databases

Relational Databases ▪Relational database management systems (RDBMS) provide three things ›Structure – the representation of the data ›Integrity – constraints on the data ›Language – means for accessing and manipulating data USC CSCI 201L4/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-

Outline USC CSCI 201L5/12 ▪D▪Databases ▪S▪SQL ▪T▪Try It!

SQL ▪The Structured Query Language (SQL) is the primary language supported by DBMSs for accessing and manipulating data ▪Some MySQL SQL statements you should know ›SHOW ›CREATE DATABASE ›USE ›CREATE TABLE ›INSERT ›UPDATE ›SELECT ›DELETE ›DROP ▪If you are not familiar with SQL, there are many good tutorials online and our textbook has a good chapter reference as well USC CSCI 201L6/12 Databases

SQL Scripts ▪After installing a DBMS, you will be able to run SQL scripts ▪SQL scripts are files that contain a collection of SQL statements that can be used for recreating databases and populating them with initial or testing data ›This is often used in the testing phase of software engineering to test different scenarios without requiring the QA engineers to insert all of the data manually USC CSCI 201L7/12 Databases

Sample Script USC CSCI 201L8/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 201L9/12 JDBC

Outline USC CSCI 201L10/12 ▪D▪Databases ▪S▪SQL ▪T▪Try it!

SQL Queries ▪Write SQL code based on the database schema provided on slide 7 to do the following ›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 ›Change Leonard’s grade in EE 101 to a C+ ›Add Amy Farrah Fowler into the Student table ›Give Amy grades of A for CSCI 103 USC CSCI 201L11/12 Databases

SQL Queries Solution ▪Write SQL code to do the following ›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; ›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'); USC CSCI 201L12/12 Databases