Download presentation
Presentation is loading. Please wait.
Published byClara Daniel Modified over 8 years ago
1
CS122 Using Relational Databases and SQL Huiping Guo Department of Computer Science California State University, Los Angeles 1. Introduction
2
1. Introduction CS122_W16 1-2 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 Email : hpguo@calstatela.eduhpguo@calstatela.edu m Phone: (323) 343-6673 m Office: ET-A318 m Office hours: M: 10:00 - 12:00 PM W: 10:30 – 12:30 r Course webpage m www.calstatela.edu/faculty/hpguo/Teaching/Winter16/ CS122/CS122_W16.htm www.calstatela.edu/faculty/hpguo/Teaching/Winter16/ CS122/CS122_W16.htm
3
1. Introduction CS122_W16 1-3 Course administration (Cont.) r Textbook “SQL Essentials” by Gary Randolph and Jeffrey Griffin Franklin Beedle & Associates, r Resources m SQL Tutorial http://www.w3schools.com/sql/ www.1KeyData.com http://www.xlinesoft.com/interactive_sql_tutorial/ m MySQL Tutorial Lynda http://www.tutorialspoint.com/mysql/ http://www.tizag.com/mysqlTutorial/
4
1. Introduction CS122_W16 1-4 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 Email me the name of your group partner ASAP! (one email/group) m Put CS122 group in the subject line r Final letter grade m A 90-100 A- 85-89 m B+ 80-84 B 70-79 m C 60-69 NC < 60
5
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
6
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 email submissions! 1. Introduction CS122_W16 1-6
7
1. Introduction CS122_W16 1-7 Course administration (Cont.) r CSNS webpage m http://csns.calstatela.edu/index.html http://csns.calstatela.edu/index.html 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 email address
8
1. Introduction CS122_W16 1-8 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 email if your email address in CSNS is correct m Log into CSNS to check
9
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
10
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_W16 1-10
11
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 http://csns.calstatela.edu/department/cs/survey/respon se/edit?surveyId=5275336 http://csns.calstatela.edu/department/cs/survey/respon se/edit?surveyId=5275336 1. Introduction CS122_W16 1-11
12
Introduction
13
1. Introduction CS122_W16 1-13 What is a database? r A large, integrated collection of data m Bank m Company m E-commerce m Government m University m ……….
14
1. Introduction CS122_W16 1-14 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
15
1. Introduction CS122_W16 1-15 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
16
1. Introduction CS122_W16 1-16 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
17
1. Introduction CS122_W16 1-17 Field (column, Attribute) Record (tuple, row) Relational Database Concepts: Table Table (Relation) Table: Titles
18
1. Introduction CS122_W16 1-18 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
19
1. Introduction CS122_W16 1-19 Relational Database Concepts r Candidate keys m titleID, Title, UPC r Primary key m Any ONE of the candidate keys
20
1. Introduction CS122_W16 1-20 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
21
1. Introduction CS122_W16 1-21 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
22
1. Introduction CS122_W16 1-22 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
23
1. Introduction CS122_W16 1-23 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
24
1. Introduction CS122_W16 1-24 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]
25
1. Introduction CS122_W16 1-25 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’
26
Using MySQL
27
1. Introduction CS122_W16 1-27 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
28
1. Introduction CS122_W16 1-28 Use MySQL on the CS server 1. Log into the CS server using SSH 2. Connect to the MySQL server 3. Play with MySQL
29
1. Introduction CS122_W16 1-29 1. 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
30
1. Introduction CS122_W16 1-30 1. 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!
31
1. Introduction CS122_W16 1-31 2. 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
32
1. Introduction CS122_W16 1-32 2. 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
33
1. Introduction CS122_W16 1-33 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
34
1. Introduction CS122_W16 1-34 Choose the database r After connected to MySQL sever, you need to choose the database m Mysql> Use database_name;
35
1. Introduction CS122_W16 1-35 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
36
1. Introduction CS122_W16 1-36 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
37
1. Introduction CS122_W16 1-37 Transfer the script files to the server (Cont.) 2. Launch SSH Secure File Transfer Client click
38
1. Introduction CS122_W16 1-38 Transfer the script files to the server (Cont.) 3. On the top-left window, choose the folder where the two files are
39
1. Introduction CS122_W16 1-39 Transfer the script files to the server (Cont.) 4. Drag files to the top-right window
40
1. Introduction CS122_W16 1-40 Run script files r Go back to the secure shell client window r Command m mysql>use hpguo; m mysql> source lyric.sql;
41
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_W16 1-41
42
1. Introduction CS122_W16 1-42 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;
43
1. Introduction CS122_W16 1-43 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;
44
1. Introduction CS122_W16 1-44 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
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.