CS122 Using Relational Databases and SQL Huiping Guo Department of Computer Science California State University, Los Angeles 1. Introduction
1. Introduction CS122_W Course administration r Lectures m Lec: Monday 4:20 – 6:00pm, ET A210 m Lab: Wednesday 3:30 – 6:00pm, ET A210 r Instructor: Huiping Guo m m Phone: (323) m Office: ET-A318 m Office hours: M: 10: :00 PM W: 10:30 – 12:30 r Course webpage m CS122/CS122_W16.htm CS122/CS122_W16.htm
1. Introduction CS122_W Course administration (Cont.) r Textbook “SQL Essentials” by Gary Randolph and Jeffrey Griffin Franklin Beedle & Associates, r Resources m SQL Tutorial m MySQL Tutorial Lynda
1. Introduction CS122_W Course administration (Cont.) r Grading policy m Homework (individual): (20%) m Lab(group): (15%) m In-class project(group): (10%) m Attendance (5%) m Midterm (20%): 02/10 m Final exam (30%): Monday, 03/14, 4:30 – 7:00pm r Group up! m Form groups of 2 students m me the name of your group partner ASAP! (one /group) m Put CS122 group in the subject line r Final letter grade m A A m B B m C NC < 60
Note: r The grading policy is for all students in the class r No extra homework r No make up exams m DO NOT take this class if you cannot attend the final exam! You ’ ll be graded based ONLY on your performance! m NOT on your request 1. Introduction CS122_W16 1-5
Course administration (Cont.) r Note: m Assignment and lab submissions: Through CSNS Make sure your files are READABLE! Please submit your homework/lab before the due time The submission button will be disabled after the due time I don ’ t accept submissions! 1. Introduction CS122_W16 1-6
1. Introduction CS122_W Course administration (Cont.) r CSNS webpage m m Every student enrolled in the class has an account m both of your username and password are your CIN. m When you first sign in, you will be asked to choose a different username and password. m If you already used the system in other classes, your username and password are the same as before. r Make sure you give the correct address
1. Introduction CS122_W Course administration (Cont.) r CSNS: how to modify the uploaded file m You cannot delete the uploaded file m Modify the file and upload it with the same name + a version number to CSNS r How to know your grade m You’ll receive an if your address in CSNS is correct m Log into CSNS to check
Course administration (Cont.) r Important dates m Thursday, Feb 18 Drop Deadline r Academic integrity m DON’T COPY OTHER’S WORK! m DON’T GIVE YOUR SOLUTIONS TO ANYONE! m If plagiarism is found, ALL parties involved will get F 1. Introduction CS122_W16 1-9
Other policies r DO NOT talk in class during lectures r After lecture, leave the classroom m Students are not allowed to stay in the classroom alone without the presence of the instructor r Please turn off your cell phone! 1. Introduction CS122_W
New teaching strategies r Course Redesign With Technology m Funded by the Chancellor’s Office r The class will be partially flipped m Some lectures will be recorded and the videos will be put on the class website m You’re SUPPOSDED to watch the video before attending the class m You need to work on some projects in class r Please take this survey m se/edit?surveyId= se/edit?surveyId= Introduction CS122_W
Introduction
1. Introduction CS122_W What is a database? r A large, integrated collection of data m Bank m Company m E-commerce m Government m University m ……….
1. Introduction CS122_W Why use a database? r Efficient data management access r Concurrent access and crash recovery r Data integrity and security r Data administration r Data independence r Reduced application development time
1. Introduction CS122_W Database models r Physical model m How data is stored on a disk r Logical model m How data is organized m Three models Hierarchical Network Relational
1. Introduction CS122_W Relational model r Most popular relational DBMS m Oracle, Sybase, MySQL, SQL Server, Access m In this model, a database is a collection of tables m The tables are related to each other
1. Introduction CS122_W Field (column, Attribute) Record (tuple, row) Relational Database Concepts: Table Table (Relation) Table: Titles
1. Introduction CS122_W Relational Database Concepts r Candidate Key m Also called keys m Uniquely identify a record No two rows have the same candidate key m Maybe multiple candidate keys in a table r Primary key m Select a candidate key as the primary key m Only one primary key in a table
1. Introduction CS122_W Relational Database Concepts r Candidate keys m titleID, Title, UPC r Primary key m Any ONE of the candidate keys
1. Introduction CS122_W Data type r Determine what kind of data a field can store r Common data types m Text m Numeric Byte integer, long integer, single, double, decimal date/time, boolean Special
1. Introduction CS122_W Schema r “Definition” of a database m Names of the tables m Attributes and attribute types in each table m Constraints on each tables m Dependencies between tables r To be covered in CS422
1. Introduction CS122_W Introduction to SQL r Structured Query Language r Open ANSI standard m Supported by most databases m Some variation in implementation r A skill that is used by many people in many environments m Programmers m Database Administrators m Managers
1. Introduction CS122_W What Does SQL Do? r Views information from relational database m Single or Multiple Tables m Tools to Calculate and Summarize r Manipulates information in relational database m Insert Records m Update Records m Delete Records m Operates on entire recordset with single command r Defines relational database m Create Database, Tables, Primary and Foreign Keys
1. Introduction CS122_W SQL queries r Basic format: SELECT filed_name1, field_name2,… FROM table_name1, tabel_name2… [WHERE conditions ] r If you select ALL attributes of a table SELECT * FROM table_name [WHERE conditions]
1. Introduction CS122_W SQL examples r List all the attributes of all titles SECLECT * FROM Titles r List the title and upc of all titles SECLECT title, upc FROM Titles r List the title and upc of the titles in the ‘metal’ genre SECLECT title, upc FROM Titles WHERE genre = ‘metal’
Using MySQL
1. Introduction CS122_W Introduction to MySQL r MySQL is a free client-server DBMS r MySQL Sever m cs1.calstatela.edu r MySQL client m Graphical User Interface tool MySQL Workbench, MYSQL client… m Command line Connect to the server directly under the command window We’ll use SSH Secure Shell ClientSSH Secure Shell Client
1. Introduction CS122_W Use MySQL on the CS server 1. Log into the CS server using SSH 2. Connect to the MySQL server 3. Play with MySQL
1. Introduction CS122_W Log into the cs sever r Use SSH to log in m Launch SSH Secure Shell Client m Click Quick Connect m Type the following info Host name: cs1.calstatela.edu User name: your assigned user id you’ll be asked to input password –Password is case sensitive
1. Introduction CS122_W Log into the cs sever r After logging in, change your password m The password is for the cs server! m You need to input the current password m The password you typed won’t show up!
1. Introduction CS122_W Connect to the MySQL server r $mysql –p m You will be asked to input the password. m The password is the original password assigned to you, not the password you just changed to. m The password you type DOESN’T show up
1. Introduction CS122_W Connect to the MySQL server r Change your password mysql> SET PASSWORD = PASSWORD (‘ your new password'); Note:You had better set the same password for the CS server and the MySQL server
1. Introduction CS122_W Choose the database r Each student has only one database m The name of the database is the same as your username r You don’t have the privilege to create new databases r The database doesn’t contain any tables
1. Introduction CS122_W Choose the database r After connected to MySQL sever, you need to choose the database m Mysql> Use database_name;
1. Introduction CS122_W Transfer the script files to the server r You’ll use two script files m Lyric.sql: for your labs and in-class projects m Books.sql: for your homework m The two files are used to create tables in your database m You need to transfer the two files to the cs server before using the files to create the tables
1. Introduction CS122_W Transfer the script files to the server (Cont.) r How? 1. Go to the course web page mFind the two files: Lyric.sql, Books.sql mRight click a file, chose “Save Links As” mChoose any folder you want folder, say c:\temp mYou need to memorize the folder name
1. Introduction CS122_W Transfer the script files to the server (Cont.) 2. Launch SSH Secure File Transfer Client click
1. Introduction CS122_W Transfer the script files to the server (Cont.) 3. On the top-left window, choose the folder where the two files are
1. Introduction CS122_W Transfer the script files to the server (Cont.) 4. Drag files to the top-right window
1. Introduction CS122_W Run script files r Go back to the secure shell client window r Command m mysql>use hpguo; m mysql> source lyric.sql;
Run script files r To do assignments r Use the same database r Run Books.sql r All tables in Lyric will be removed. r You need to run lyric.sql again to do labs or in-class projects. 1. Introduction CS122_W
1. Introduction CS122_W Some MySQL commands r Help m ? or \h or help; r Reconnect to the server m Connect; r Display sever info m Status r Quite MySQL m \q or quit; or exit;
1. Introduction CS122_W Some MySQL commands r Show databases m Show databases; r use database m Use dbname; r Show tables m Show tables; r Show table schema m Desc tablename;
1. Introduction CS122_W Exercise r Design the following queries: 1. Find all the information of sales person 2. Find the name and web address of all artists 3. Find the names of all artists who are in Canada