Lecture 22: Creating a Database and More joins

Slides:



Advertisements
Similar presentations
SQL Lecture 10 Inst: Haya Sammaneh. Example Instance of Students Relation  Cardinality = 3, degree = 5, all rows distinct.
Advertisements

Day 3 - Basics of MySQL What is MySQL What is MySQL How to make basic tables How to make basic tables Simple MySQL commands. Simple MySQL commands.
1 Translation of ER-diagram into Relational Schema Prof. Sin-Min Lee Department of Computer Science.
Structured Query Language SQL: An Introduction. SQL (Pronounced S.Q.L) The standard user and application program interface to a relational database is.
SQLite 1 CS440. What is SQLite?  Open Source Database embedded in Android  SQL syntax  Requires small memory at runtime (250 Kbytes)  Lightweight.
Lecture 16: SQL and HTML tables
Appendix A Database Design CS Database design principles  database design: the act of deciding the schema for a database  database schema: a description.
MySql In Action Step by step method to create your own database.
Rensselaer Polytechnic Institute CSCI-4380 – Database Systems David Goldschmidt, Ph.D.
Relational Database Management Systems. A set of programs to manage one or more databases Provides means for: Accessing the data Inserting, updating and.
Form and Graphical User Interfaces. Lesson plan More about queries More about entering data into a table Form.
Example simpsons database
IST 210: ORGANIZATION OF DATA Chapter 1. Getting Started IST210 1.
CSC 2720 Building Web Applications Database and SQL.
PHP and MySQL CS How Web Site Architectures Work  User’s browser sends HTTP request.  The request may be a form where the action is to call PHP.
CSE 154 LECTURE 14: MULTI-TABLE SQL QUERIES (JOINS)
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.
Entity-Relationship (ER) Modelling ER modelling - Identify entities - Identify relationships - Construct ER diagram - Collect attributes for entities &
Fox MIS Spring 2011 Database Week 6 ERD and SQL Exercise.
>> Introduction to MySQL. Introduction Structured Query Language (SQL) – Standard Database Language – Manage Data in a DBMS (Database Management System)
SQL queries Data Maintenance. RHS – SOC 2 Data maintenance In addition to asking question to the database (queries), we can also maintain the data itself.
 MySQL  DDL ◦ Create ◦ Alter  DML ◦ Insert ◦ Select ◦ Update ◦ Delete  DDL(again) ◦ Drop ◦ Truncate.
Introduction to Databases & SQL Ahmet Sacan. What you’ll need Firefox, SQLite plugin Mirdb and Targetscan databases.
Chapter 1. Getting Started IST 210: Organization of Data IST2101.
Logical DB Design: ER to Relational
Insert, Update and the rest…
MIS2502: Data Analytics SQL – Putting Information Into a Database
Data Definition and Data Types
MIS2502: Data Analytics SQL – Putting Information Into a Database
SQL – Column constraints
ISC440: Web Programming 2 Server-side Scripting PHP 3
Lecturer: Mukhtar Mohamed Ali “Hakaale”
MIS2502: Data Analytics SQL – Putting Information Into a Database
Translation of ER-diagram into Relational Schema
PHP and MySQL.
Structured Query Language
DATABASE SQL= Structure Query Language مبادئ قواعد بيانات
Creating Tables & Inserting Values Using SQL
SQL DATA CONSTRAINTS.
MIS2502: Data Analytics SQL – Putting Information Into a Database
CS122 Using Relational Databases and SQL
MIS2502: Data Analytics SQL – Putting Information Into a Database
CS122 Using Relational Databases and SQL
Lecture 22: Relational Databases and SQL
Lecture 23: Multi-table SQL Queries (Joins)
MIS2502: Data Analytics SQL – Putting Information Into a Database
Lecture 3 Finishing SQL
Introduction to Databases & SQL
CS3220 Web and Internet Programming SQL and MySQL
CS1222 Using Relational Databases and SQL
Data Definition Language
Lecture 24: Creating a Database and More joins
Data Definition Language
A Very Brief Introduction to Relational Databases
MIS2502: Data Analytics SQL 4– Putting Information Into a Database
MySQL Database System Installation Overview SQL summary
Topic 12 Lesson 2 – Retrieving Data with Queries
CS122 Using Relational Databases and SQL
CS3220 Web and Internet Programming SQL and MySQL
Instructor Information: Mark Jamil
MySQL Database System Installation Overview SQL summary
CS122 Using Relational Databases and SQL
CS122 Using Relational Databases and SQL
Lecture 20: Relational Databases and SQL
Lecture 21: Multi-table SQL Queries (Joins)
Database Instructor: Bei Kang.
កម្មវិធីបង្រៀន SQL Programming ជាភាសាខ្មែរ Online SQL Training Course
SQL NOT NULL Constraint
SQL (Structured Query Language)
CSC 453 Database Systems Lecture
Presentation transcript:

Lecture 22: Creating a Database and More joins CSc 337 Lecture 22: Creating a Database and More joins

Creating a database In the command line in mysql: CREATE DATABASE name; To get to your database: USE name

Creating a Database CREATE TABLE name ( columnName type constraints, ... columnName type constraints ); CREATE TABLE students ( sid INTEGER UNSIGNED NOT NULL PRIMARY KEY, name VARCHAR(20), email VARCHAR(32) adds/deletes an entire new table from this database you can add constraints such as NOT NULL for a field that cannot be blank or PRIMARY KEY for a column that must be unique for every row related commands: CREATE DATABASE, DROP TABLE, ALTER TABLE

Inserting into a database INSERT INTO table VALUES ('value1', 'value2', …);

example.sql create table zipcodes ( zip integer(5) primary key, city varchar(30), State varchar(20)); create table employees ( eno varchar(10) primary key, ename varchar(30), zip integer(5) references zipcodes, hire_date date); insert into zipcodes values (98225, 'Bellingham', 'WA'); insert into zipcodes values (95388, 'Winton', 'CA'); insert into zipcodes values (44242, 'Stow', 'OH'); insert into zipcodes values (61536, 'Hanna city', 'IL'); insert into zipcodes values (01254, 'Richmond', 'MA'); insert into zipcodes values (95124, 'San Jose', 'CA'); insert into zipcodes values (95382, 'Turlock', 'MA'); insert into zipcodes values (95380, 'Turlock', 'CA'); insert into employees values ('P0239400', 'Jones Hoffer',98225, '2000-12-12'); insert into employees values ('P0239401', 'Jeffrey Prescott',95388, '2006-01-01'); insert into employees values ('P0239402', 'Fred NcFaddeb',95124, '2008-09-01');

SQL data types BOOLEAN INTEGER FLOAT VARCHAR : a string DATE, TIME, DATETIME BLOB : binary data quick reference

Database Design 1 what's good and bad about this design? name email course grade Bart bart@fox.com Computer Science 142 B- Computer Science 143 C Milhouse milhouse@fox.com B+ Lisa lisa@fox.com A+ Computer Science 190M Ralph ralph@fox.com Informatics 100 D+ what's good and bad about this design?

Database Design 2 student_id course_id grade 123 10001 B- 10002 C 456 B+ 888 A+ 10003 404 10004 D+ id name email 123 Bart bart@fox.com 456 Milhouse milhouse@fox.com 888 Lisa lisa@fox.com 404 Ralph ralph@fox.com id name 10001 Computer Science 142 10002 Computer Science 143 10003 Computer Science 190M 10004 Informatics 100 splitting data into multiple tables avoids redundancy normalizing: splitting tables to improve structure and remove redundancy / anomalies normalized tables are often linked by unique integer IDs

Related Tables and Keys student_id course_id grade 123 10001 B- 10002 C 456 B+ 888 A+ 10003 404 10004 D+ id name email 123 Bart bart@fox.com 456 Milhouse milhouse@fox.com 888 Lisa lisa@fox.com 404 Ralph ralph@fox.com id name 10001 Computer Science 142 10002 Computer Science 143 10003 Computer Science 190M 10004 Informatics 100 records of one table may be associated with record(s) in another table record in Student table with student_id of 888 is Lisa Simpson's student info records in Grade table with student_id of 888 are Lisa Simpson's course grades primary key: a table column guaranteed to be unique for each record

Designing a query Figure out the proper SQL queries in the following way: Which table(s) contain the critical data? (FROM) Which columns do I need in the result set? (SELECT) How are tables connected (JOIN) and values filtered (WHERE)? Test on a small data set (imdb_small). Confirm on the real data set (imdb). Try out the queries first in the query tool. Write the NodeJS code to run those same queries. Make sure to check for SQL errors at every step!!

Example imdb database id first_name last_name gender 433259 William Shatner M 797926 Britney Spears F 831289 Sigourney Weaver ... id name year rank 112290 Fight Club 1999 8.5 209658 Meet the Parents 2000 7 210511 Memento 8.7 ... actor_id movie_id role 433259 313398 Capt. James T. Kirk 407323 Sgt. T.J. Hooker 797926 342189 Herself ... actors movies roles movie_id genre 209658 Comedy 313398 Action Sci-Fi ... id first_name last_name 24758 David Fincher 66965 Jay Roach 72723 William Shatner ... director_id movie_id 24758 112290 66965 209658 72723 313398 ... also available, imdb_small with fewer records (for testing queries) movies_genres directors movies_directors

IMDb table relationships / ids

IMDb practice queries What are the names of all movies released in 1995? How many people played a part in the movie "Lost in Translation"? What are the names of all the people who played a part in the movie "Lost in Translation"? Who directed the movie "Fight Club"? How many movies has Clint Eastwood directed? What are the names of all movies Clint Eastwood has directed? What are the names of all directors who have directed at least one horror film? What are the names of every actor who has appeared in a movie directed by Christopher Nolan?