INF1343, Week 6 Implementing a Database with SQL This presentation is licensed under Creative Commons Attribution License, v. 3.0. To view a copy of this.

Slides:



Advertisements
Similar presentations
Overview Begin 6:00 Quiz15 mins6:15 Review Table Terms25 mins6:40 Short Break10 mins6:50 SQL: Creating Tables60 mins7:50 Break10 mins8:00 Lab – Creating.
Advertisements

DAT702.  Standard Query Language  Ability to access and manipulate databases ◦ Retrieve data ◦ Insert, delete, update records ◦ Create and set permissions.
Phonegap Bridge – File System CIS 136 Building Mobile Apps 1.
Database Systems Lecture 5 Natasha Alechina
1 CSE 480: Database Systems Lecture 9: SQL-DDL Reference: Read Chapter of the textbook.
MySql In Action Step by step method to create your own database.
Structured Query Language (SQL) A2 Teacher Up skilling LECTURE 2.
Session 5: Working with MySQL iNET Academy Open Source Web Development.
Creating Databases with MySQL Workbench Build the Forums database in Ullman’s Chapter 6.
Dbwebsites 2.1 Making Database backed Websites Session 2 The SQL… Where do we put the data?
Relational Database Management Systems. A set of programs to manage one or more databases Provides means for: Accessing the data Inserting, updating and.
Lecture 7 Integrity & Veracity UFCE8K-15-M: Data Management.
CSC 2720 Building Web Applications Database and SQL.
Database and mySQL Week 07 Dynamic Web TCNJ Jean Chu.
Chapter 5 MYSQL Database. Introduction to MYSQL MySQL is the world's most popular open-source database. Open source means that the source code, the programming.
Open Source Server Side Scripting ECA 236 Open Source Server Side Scripting MySQL – Inserting Data.
MySQL More… 1. More on SQL In MySQL, the Information Schema is the “Catalog” in the SQL standard SQL has three components: Data definition Data manipulation.
Advanced Web 2012 Lecture 3 Sean Costain What is a Database? Sean Costain 2012 A database is a structured way of dealing with structured information.
Sql DDL queries CS 260 Database Systems.
Week 8-9 SQL-1. SQL Components: DDL, DCL, & DML SQL is a very large and powerful language, but every type of SQL statement falls within one of three main.
©Silberschatz, Korth and Sudarshan1 Structured Query Language (SQL) Data Definition Language Domains Integrity Constraints.
DBMS 3. course. Reminder Data independence: logical and physical Concurrent processing – Transaction – Deadlock – Rollback – Logging ER Diagrams.
1 CS 430 Database Theory Winter 2005 Lecture 11: SQL DDL.
There are two types of MySQL instructions (Data Definition Language) DDL: Create database, create table, alter table,,,. (Data Manipulation Language) DML.
Creating E/R Diagrams with SQL Server Management Studio, Writing SQL Queries D0ncho Minkov Telerik School Academy schoolacademy.telerik.com Technical Trainer.
CS320 Web and Internet Programming SQL and MySQL Chengyu Sun California State University, Los Angeles.
Lecture 1.21 SQL Introduction Steven Jones, Genome Sciences Centre.
CCT396, Fall 2011 Database Design and Implementation Yuri Takhteyev University of Toronto This presentation is licensed under Creative Commons Attribution.
INF1343, Winter 2012 Data Modeling and Database Design Yuri Takhteyev Faculty of Information University of Toronto This presentation is licensed under.
CCT396, Fall 2011 Database Design and Implementation Yuri Takhteyev University of Toronto This presentation is licensed under Creative Commons Attribution.
Welcome to INF1343! Database Modeling and Database Design This presentation is licensed under Creative Commons Attribution License, v To view a copy.
CCT395, Week 4 Database Design and ER Modeling This presentation is licensed under Creative Commons Attribution License, v To view a copy of this.
CCT1343, Week 3 SQL Queries Using Multiple Tables This presentation is licensed under Creative Commons Attribution License, v To view a copy of this.
CCT395, Week 11 Review, Permissions Physical Storage and Performance This presentation is licensed under Creative Commons Attribution License, v. 3.0.
CCT395, Week 6 Implementing a Database with SQL and a Case Study Exercise This presentation is licensed under Creative Commons Attribution License, v.
Database Design and Implementation
CCT395, Week 2 Introduction to SQL Yuri Takhteyev September 15, 2010
CCT396, Fall 2011 Database Design and Implementation Yuri Takhteyev University of Toronto This presentation is licensed under Creative Commons Attribution.
INF1343, Week 4 Database Design and ER Modeling This presentation is licensed under Creative Commons Attribution License, v To view a copy of this.
CCT396, Fall 2011 Database Design and Implementation Yuri Takhteyev University of Toronto This presentation is licensed under Creative Commons Attribution.
CCT395, Week 5 Translating ER into Relations; Normalization This presentation is licensed under Creative Commons Attribution License, v To view a.
Database Design and Implementation
Translating ER into Relations; Normalization
CS320 Web and Internet Programming SQL and MySQL
Managing Tables, Data Integrity, Constraints by Adrienne Watt
MySQL-Database Jouni Juntunen Oulu University of Applied Sciences
Data Modeling and Database Design INF1343, Winter 2012 Yuri Takhteyev
Special Topics in CCIT: Databases
CCT1343, Week 2 Single-Table SQL Yuri Takhteyev University of Toronto
CS 3630 Database Design and Implementation
Data Definition and Data Types
Data Modeling and Database Design
Data Modeling and Database Design INF1343, Winter 2012 Yuri Takhteyev
Database Design and Implementation
Data Modeling and Database Design INF1343, Winter 2012 Yuri Takhteyev
ORACLE SQL Developer & SQLPLUS Statements
Relational Databases The Relational Model.
Relational Databases The Relational Model.
CS4222 Principles of Database System
SQL OVERVIEW DEFINING A SCHEMA
Database systems Lecture 2 – Data Types
SQL-1 Week 8-9.
Session - 6 Sequence - 1 SQL: The Structured Query Language:
Computer Science Projects Database Theory / Prototypes
CS3220 Web and Internet Programming SQL and MySQL
MySQL Database System Installation Overview SQL summary
CS3220 Web and Internet Programming SQL and MySQL
MySQL Database System Installation Overview SQL summary
Database Instructor: Bei Kang.
CS4540 Special Topics in Web Development SQL and MS SQL
Presentation transcript:

INF1343, Week 6 Implementing a Database with SQL This presentation is licensed under Creative Commons Attribution License, v To view a copy of this license, visit This presentation incorporates images from the Crystal Clear icon collection by Everaldo Coelho, available under LGPL from Yuri Takhteyev University of Toronto February 7, 2011

Normal Forms 4 th Normal Form BC Normal Form 3 rd Normal Form 2 nd Normal Form 1 st Normal Form 5 th Normal Form trivial! you can do it! fixing weird issues

Mistaking binary relationships for ternary 4NF

course code title session code start date room code capacity

course code title session code start date room code capacity cours e insta nce

5NF “We could break it up yet more”

Recursive Relationships employee salary supervise s

Recursive Relationships employee salary employee_id * supervise s

Step 3 “For each entity that is at the ‘many’ end of one or more relationships, include the primary key of each parent entity in the table as foreign keys.”

Recursive Relationships employee salary employee_id * supervise s

Recursive Relationships employee salary employee_id * employee_id supervise s

Recursive Relationships employee salary employee_id * supervisor_id supervise s

employee

1-1 Relationships chef name signature dish description

Option 1: Merge chef name signature dish description

Option 1: Merge chef

Option 2: FK as PK chef name signature dish description

Option 2: FK as PK chef chef_id* name signature dish description

Option 2: FK as PK chef chef_id* name signature dish chef_id* description

Option 2: FK as PK signature dish

Questions?

Implementation - Create a database/schema - Define domains - Create the tables

Domains / Data Types

Domains / Data Types

Domains / Data Types “Harold\n”? ? ? * ?

String (Text) CHAR(n) VARCHAR(n) TEXT BINARY(n) VARBINARY(n) BLOB ENUM

Numeric INTEGER UNSIGNED SMALLINT BIGINT NUMERIC(n,m) DECIMAL(n,m) REAL DOUBLE PRECISION FLOAT(m,d) BOOLEAN

Date and Time DATE TIME DATETIME TIMESTAMP

Extensions Spatial: POINT POLYGON LINESTRING GEOMETRY

Type Casting Explicit casting: CAST(weight AS INTEGER) Implicit casting: WHERE weight > "25" Beware: 3.2 < 25 “3.2” > “25”

Create a Database create database «name»; create database university; For instance:

Query OK, 1 row affected (0.00 sec)

Delete a Database drop database «name»; drop database university; For instance:

Query OK, 0 rows affected (0.01 sec)

Create a Table create table «name»; use university; create table student; For instance:

ERROR 1113 (42000): A table must have at least 1 column

Create a Table create table «name» («column» «vartype»); create table student (student_id int); For instance:

Query OK, 0 rows affected (0.02 sec)

Create a Table create table «name» ( «column1» «vartype1», «column2» «vartype2» ); create table student ( student_id int, last_name varchar(100) ); For instance:

ERROR 1050 (42S01): Table 'student' already exists

Delete a Table drop table «name»; drop table student; For instance:

Query OK, 0 rows affected (0.00 sec)

Create a Table create table «name»> ( «column1» «vartype1», «column2» «vartype2» ); create table student ( student_id int, last_name varchar(100) ); For instance:

Query OK, 0 rows affected (0.02 sec)

Required Fields create table student ( student_id int not null, last_name varchar(100) );

Primary Keys create table student ( student_id int not null, last_name varchar(100), primary key (student_id) );

mysql> describe student; | Field | Type | Null | Key | Default | Extra | | student_id | int(11) | NO | PRI | NULL | | | last_name | varchar(100) | YES | | NULL | | rows in set (0.00 sec)

Foreign Keys create table enrollment ( student_id int not null, foreign key (student_id) references student(student_id), instance_id int not null ); A dirty secret: MySQL 5.0 ignores FKs!

FKs with InnoDB create table enrollment ( student_id int not null, foreign key (student_id) references student(student_id), instance_id int not null ) engine=InnoDB; (We'll need to re-create the student table first.)

Load Some Data! insert into student values (1, "Kenobi"); insert into enrollment values (1, 2);

Query OK, 1 row affected (0.00 sec) mysql> select * from student; | student_id | last_name | | 1 | Kenobi | row in set (0.00 sec)

From a File load data infile "/tmp/students.csv" into table student;

Using SCP scp = secure copy (or ssh + cp) copy files over an ssh connection Hint: You will usually be running this in your local bash session (i.e. on your laptop/desktop). Hint: Windows users can use WinSCP instead.

Remote to Local scp /local/dir e.g.: scp usernam e host (server) remote file local directory

Local to Remote scp /local/file e.g.: scp students.csv

Permissions chmod a+r /tmp/students.csv

Editing Code Locally Windows: Notepad++ Mac: TextWrangler Linux: gedit (or emacs, vi) Key feature: syntax highlighting

Comma-Separated load data infile "/tmp/students.csv" into table student fields terminated by ",";

Query OK, 1 row affected (0.00 sec) mysql> select * from student; | student_id | last_name | | 1 | Kenobi | row in set (0.00 sec) mysql> load data infile "/tmp/students.csv" into table student; ERROR 1062 (23000): Duplicate entry '1' for key 1

Deleting Data delete from student;

Deleting Select Data delete from student where... ;

ON DELETE create table enrollment ( student_id int not null, foreign key (student_id) references student(student_id) on delete cascade, instance_id int not null ) engine=InnoDB;

Data 1273SmithRobert 1274Ivanov Boris 1275AlmeidaJoão

Easier SmithRobert IvanovBoris AlmeidaJoão

Auto-Increment create table student ( student_id int not null auto_increment, last_name varchar(100), primary key (student_id) ) engine=InnoDB;

Auto-Increment insert into student values (NULL, "Kenobi"); insert into student (last_name) values ("Kenobi");

Auto-Increment load data infile "/tmp/students.csv" into table student;

Questions?

Assignment 1 See the answer sheet on the website.