Download presentation
Presentation is loading. Please wait.
1
CS1222 Using Relational Databases and SQL
11/23/2018 CS Using Relational Databases and SQL Introduction Randy Moss Department of Computer Science California State University, Los Angeles
2
11/23/2018 Introduction
3
What is a database? A large, integrated collection of data Bank
11/23/2018 What is a database? A large, integrated collection of data Bank Company E-commerce Government University ………. 1. Introduction CS1222-Fall2017
4
Why use a database? Efficient data management access
11/23/2018 Why use a database? Efficient data management access Concurrent access and crash recovery Data administration Data integrity and security Data independence Reduced application development time 1. Introduction CS1222-Fall2017
5
Database models Physical model Logical model
11/23/2018 Database models Physical model How data is stored on a disk Logical model How data is organized Examples NoSQL Key-value pair Relational A set of concepts to describe the structure of a database, and certain constraints that the database should obey. 1. Introduction CS1222-Fall2017
6
Relational model Most popular relational DBMS
11/23/2018 Relational model Most popular relational DBMS Oracle, Postgres, MySQL, SQL Server, Access, and more How do Relational DBs store data? Tables! 1. Introduction CS1222-Fall2017
7
Relational Database Concepts: Table
11/23/2018 Relational Database Concepts: Table Field (column, Attribute) Table (Relation) Table: Titles Record (tuple, row) 1. Introduction CS1222-Fall2017
8
Relational Database Concepts
11/23/2018 Relational Database Concepts Candidate Key Uniquely identify a record No two rows have the same candidate key Maybe multiple candidate keys in a table Primary key Select a candidate key as the primary key Only one primary key in a table 1. Introduction CS1222-Fall2017
9
Relational Database Concepts
11/23/2018 Relational Database Concepts Candidate keys titleID, Title, UPC Primary key Any ONE of the candidate keys 1. Introduction CS1222-Fall2017
10
Data type Determine what kind of data a field can store
11/23/2018 Data type Determine what kind of data a field can store Common data types Text Numeric Byte integer, long integer, single, double, decimal date/time, boolean Special 1. Introduction CS1222-Fall2017
11
Schema “Definition” of a database Names of the tables
11/23/2018 Schema “Definition” of a database Names of the tables Attributes and attribute types in each table Constraints on each tables Dependencies between tables 1. Introduction CS1222-Fall2017
12
Introduction to SQL Structured Query Language Open ANSI standard
11/23/2018 Introduction to SQL Structured Query Language Open ANSI standard Supported by most databases Some variation in implementation A skill that is used by many people in many environments Programmers Database Administrators Managers 1. Introduction CS1222-Fall2017
13
What Does SQL Do? Views information from relational database
11/23/2018 What Does SQL Do? Views information from relational database Single or Multiple Tables Tools to Calculate and Summarize Manipulates information in relational database Insert Records Update Records Delete Records Operates on entire recordset with single command Defines relational database Create Database, Tables, Primary and Foreign Keys 1. Introduction CS1222-Fall2017
14
SQL queries Basic format: If you select ALL attributes of a table
11/23/2018 SQL queries Basic format: SELECT filed_name1, field_name2,… FROM table_name1, tabel_name2… [WHERE conditions] If you select ALL attributes of a table SELECT * FROM table_name [WHERE conditions] 1. Introduction CS1222-Fall2017
15
SQL examples List all the attributes of all titles
11/23/2018 SQL examples List all the attributes of all titles SELECT * FROM Titles List the title and upc of all titles SELECT title, upc List the title and upc of the titles in the ‘metal’ genre WHERE genre = ‘metal’ 1. Introduction CS1222-Fall2017
16
11/23/2018 Using MySQL
17
2. Connect to the MySQL server
11/23/2018 2. Connect to the MySQL server $mysql –p You will be asked to input the password. The password is the original password assigned to you, not the password you just changed to. Change your password mysql> set password=PASSWORD(“new password”); $mysql –u username –p 1. Introduction CS1222-Fall2017
18
Create tables Each student has a database
11/23/2018 Create tables Each student has a database The name of the database is the same as your username You don’t have the privilege to create databases After connected to MySQL sever, you need to choose the database Mysql> Use database_name; Note: In MySQL, all commands end with ‘;’ 1. Introduction CS1222-Fall2017
19
Run script files You’ll use four script files
11/23/2018 Run script files You’ll use four script files Lyric.sql: mostly for your labs Books.sql: mostly for your homework Orders.sql and Sailors.sql: mostly for exams These four files are used to create tables in your database 1. Introduction CS1222-Fall2017
20
How transfer sql files Log onto SSH Secure Shell and go to mysql
11/23/2018 How transfer sql files Log onto SSH Secure Shell and go to mysql Type: Mysql> use “your user name” for example Mysql> use “cs122stu35” (without the quotes) Click on “New File Transfer Window” icon on top; a new window with two main panels will open On the left panel locate the database file on your system (for example Lyric.sql) Drag the file to cs122stu35 folder on the right under “Remote Name” You should see this file under “Remote Name” Close File Transfer Window and go back to the main window “SSH Secure Shell” In mysql type: Mysql> source Lyric.sql; In mysql type: Mysql> show tables; You should see the names of eight tables; 1. Introduction CS1222-Fall2017
21
Some MySQL commands Help Reconnect to the server Display sever info
11/23/2018 Some MySQL commands Help ? or \h or help; Reconnect to the server Connect; Display sever info Status Quit MySQL \q or quit; or exit; 1. Introduction CS1222-Fall2017
22
Some MySQL commands Show databases use database Show tables
11/23/2018 Some MySQL commands Show databases Show databases; use database Use dbname; Show tables Show tables; Show table schema Desc tablename 1. Introduction CS1222-Fall2017
23
Exercise Design the following queries:
11/23/2018 Exercise Design the following queries: Find all the information of sales person Find the name and web address of all artists Find the names of all artists who are in Canada 1. Introduction CS1222-Fall2017
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.