MIS2502: Data Analytics SQL – Putting Information Into a Database

Slides:



Advertisements
Similar presentations
Session 2Introduction to Database Technology Data Types and Table Creation.
Advertisements

Copyright © by Royal Institute of Information Technology Introduction To Structured Query Language (SQL) 1.
Database Basics I101 Summer 2006 Copyright 2004, Matt Hottell.
SQL components In Oracle. SQL in Oracle SQL is made up of 4 components: –DDL Data Definition Language CREATE, ALTER, DROP, TRUNCATE. Creates / Alters.
DT211 Stage 2 Databases Lab 1. Get to know SQL Server SQL server has 2 parts: –A client, running on your machine, in the lab. You access the database.
30-Jun-15 SQL A Brief Introduction. SQL SQL is Structured Query Language Some people pronounce SQL as “sequel” Other people insist that only “ess-cue-ell”
Oracle Data Definition Language (DDL)
A Guide to SQL, Eighth Edition Chapter Three Creating Tables.
 SQL stands for Structured Query Language.  SQL lets you access and manipulate databases.  SQL is an ANSI (American National Standards Institute) standard.
Introduction to MySQL Lab no. 10 Advance Database Management System.
CSC 2720 Building Web Applications Database and SQL.
Oracle Data Definition Language (DDL) Dr. Bernard Chen Ph.D. University of Central Arkansas Fall 2008.
SQL 1: GETTING INFORMATION OUT OF A DATABASE MIS2502 Data Analytics.
SQL SQL Server : Overview SQL : Overview Types of SQL Database : Creation Tables : Creation & Manipulation Data : Creation & Manipulation Data : Retrieving.
MIS2502: Data Analytics SQL – Getting Information Out of a Database David Schuff
SQL: DDL. SQL Statements DDL - data definition language –Defining and modifying data structures (metadata): database, tables, views, etc. DML - data manipulation.
Intro to SQL| MIS 2502  Spacing not relevant › BUT… no spaces in an attribute name or table name  Oracle commands keywords, table names, and attribute.
Tables and Constraints Oracle PL/SQL. Datatypes The SQL Data Definition Language Commands (or DDL) enable us to create, modify and remove database data.
# 1# 1 Creating Tables, Setting Constraints, and Datatypes What is a constraint and why do we use it? What is a datatype? What does CHAR mean? CS 105.
IMS 4212: Data Manipulation 1 Dr. Lawrence West, MIS Dept., University of Central Florida Additional Data Manipulation Statements INSERT.
1 DBS201: More on SQL Lecture 3. 2 Agenda How to use SQL to update table definitions How to update data in a table How to join tables together.
Fox MIS Spring 2011 Database Week 6 ERD and SQL Exercise.
MIS2502: Data Analytics SQL – Putting Information Into a Database David Schuff
MIS2502: Data Analytics SQL – Getting Information Out of a Database.
Exam 2 Review: SQL In, Dimensional Modeling, Pivot Tables, ETL Describe data cube elements Understand facts, dimensions, granularity Create/read a Star.
Physical Model Lecture 11. Physical Data Model The last step is the physical design phase, In this phase data is – Store – Organized and – Access.
Lec-7. The IN Operator The IN operator allows you to specify multiple values in a WHERE clause. SQL IN Syntax SELECT column_name(s) FROM table_name WHERE.
Database Constraints Ashima Wadhwa. Database Constraints Database constraints are restrictions on the contents of the database or on database operations.
Fundamentals of DBMS Notes-1.
How to: SQL By: Sam Loch.
Web Systems & Technologies
Chapter 5 Introduction to SQL.
SQL: Schema Definition and Constraints Chapter 6 week 6
Insert, Update and the rest…
Prepared by : Moshira M. Ali CS490 Coordinator Arab Open University
MIS2502: Data Analytics Relational Data Modeling
MIS5101: Business Intelligence Relational Data Modeling
MIS2502: Data Analytics SQL – Putting Information Into a Database
ORACLE SQL Developer & SQLPLUS Statements
STRUCTURED QUERY LANGUAGE
Lecturer: Mukhtar Mohamed Ali “Hakaale”
MIS2502: Data Analytics SQL – Getting Information Out of a Database
Insert, Update, Delete Manipulating Data.
MIS2502: Data Analytics Relational Data Modeling
MIS2502: Data Analytics SQL – Putting Information Into a Database
MIS2502: Review for Exam 1 JaeHwuen Jung
MIS2502: Data Analytics SQL – Getting Information Out of a Database
MIS2502: Data Analytics Converting ERDs to Schemas
MIS2502: Data Analytics SQL – Getting Information Out of a Database Part 2: Advanced Queries Aaron Zhi Cheng
Structured Query Language
Creating Tables & Inserting Values Using SQL
Exam 2 Exam 2 Study Guide is posted on the Course Site
SQL DATA CONSTRAINTS.
MIS2502: Data Analytics SQL – Putting Information Into a Database
CS122 Using Relational Databases and SQL
MIS2502: Data Analytics Relational Data Modeling
Oracle Data Definition Language (DDL)
MIS2502: Review for Exam 1 Aaron Zhi Cheng
MIS2502: Data Analytics SQL – Putting Information Into a Database
CS122 Using Relational Databases and SQL
MIS2502: Data Analytics SQL – Putting Information Into a Database
CS1222 Using Relational Databases and SQL
Data Definition Language
MIS2502: Data Analytics SQL 4– Putting Information Into a Database
CS122 Using Relational Databases and SQL
MIS2502: Data Analytics SQL – Getting Information Out of a Database Part 1: Basic Queries Aaron Zhi Cheng
MIS2502: Data Analytics SQL – Getting Information Out of a Database Part 2: Advanced Queries Zhe (Joe) Deng
MIS2502: Data Analytics Relational Data Modeling 3
CS122 Using Relational Databases and SQL
SQL NOT NULL Constraint
Presentation transcript:

MIS2502: Data Analytics SQL – Putting Information Into a Database Alvin Zuyin Zheng zheng@temple.edu http://community.mis.temple.edu/zuyinzheng/

Our relational database A series of tables Linked together through primary/foreign key relationships How to extract data from relational database?

To create a database We need to define The tables The fields (columns) within those tables The data types of those fields The primary/foreign key relationships There are SQL commands that do each of those things So let’s assume that we have a blank database and we needed to create the tables How to build a relational database?

CREATE statement (create a table) CREATE TABLE schema_name.table_name ( columnName1 datatype [NULL][NOT NULL], columnName2 datatype [NULL][NOT NULL], PRIMARY KEY (KeyName) ); Item Description schema_name The schema that will contain the table table_name The name of the table columnName The name of the field datatype The datatype of the field [NULL][NOT NULL] Whether the field can be empty (i.e., null) (The [] means the parameter is optional) A primary key column cannot contain NULL values KeyName The name of the field that will serve as the primary key

Example: Creating the Customer Table CustomerID FirstName LastName City State Zip CREATE TABLE orderdb.Customer ( CustomerID INT NOT NULL , FirstName VARCHAR(45) NULL , LastName VARCHAR(45) NULL , City VARCHAR(45) NULL , State VARCHAR(2) NULL , Zip VARCHAR(10) NULL , PRIMARY KEY (CustomerID) ); In MySQL Workbench, your schema will use your mx MySQL username (i.e., m80orderdb.Customer) Based on this SQL statement: The only required field is CustomerID – the rest can be left blank. CustomerID is defined as the primary key.

Looking at the “new” Customer table The database management system stores this information about the table It’s separate from the data in the table (i.e., Customer information) This is called metadata – “data about data” Column name Data type CustomerID INT FirstName VARCHAR(45) LastName City State VARCHAR(2) Zip VARCHAR(10) Customer

So why do you think we defined “Zip” as a VARCHAR() instead of an INT? Data types Each field can contain different types of data That must be specified when the table is created There are many data types; we’re only going to cover the most important ones Data type Description Examples INT Integer 3, -10 DECIMAL(p,s) Decimal. Example: decimal(5,2) is a number that has 3 digits before decimal and 2 digits after decimal (like 123.45) 3.23, 3.14159 VARCHAR(n) String (numbers and letters) with maximum length n 'Hello', 'I like pizza', 'MySQL!' DATETIME, DATE Date/Time, or just Date '2011-09-01 17:35:00', '2011-04-12' BOOLEAN Boolean value 0 or 1 So why do you think we defined “Zip” as a VARCHAR() instead of an INT?

So back to our CREATE statement CREATE TABLE orderdb.Customer ( CustomerID INT NOT NULL , FirstName VARCHAR(45) NULL , LastName VARCHAR(45) NULL , City VARCHAR(45) NULL , State VARCHAR(2) NULL , Zip VARCHAR(10) NULL , PRIMARY KEY (CustomerID) ); FirstName can be a string of up to 45 letters and numbers. Why 45? It’s the MySQL default. State can be a string of up to 2 letters and numbers

NULL vs. NOT NULL NULL values represent missing/empty data. The NOT NULL constraint enforces a field to always contain a value. This means that you cannot insert a new record, or update a record without adding a value to this field. CREATE TABLE orderdb.Customer ( CustomerID INT NOT NULL , FirstName VARCHAR(45) NULL , LastName VARCHAR(45) NULL , City VARCHAR(45) NULL , State VARCHAR(2) NULL , Zip VARCHAR(10) NULL , PRIMARY KEY (CustomerID) ); Rule of Thumb A primary key (e.g., CustomerID) column should be NOT NULL. The rest can be left NULL.

Foreign Key A foreign key in one table points to a primary key in another table. CustomerID is a foreign key in the Order table, and a primary key in the Customer table CREATE TABLE orderdb.`Order` ( OrderNumber INT NOT NULL , OrderDate DATETIME NULL , CustomerID INT NULL , PRIMARY KEY (OrderNumber) , FOREIGN KEY (CustomerID) REFERENCES orderdb.Customer(CustomerID)); Now let’s implement the relationships?

Some more CREATE statements CREATE TABLE orderdb.Product ( ProductID INT NOT NULL , ProductName VARCHAR(45) NULL , Price DECIMAL(5,2) NULL , PRIMARY KEY (ProductID) ); CREATE TABLE orderdb.`Order-Product` ( OrderProductID INT NOT NULL , OrderNumber INT NULL , ProductID INT NULL , PRIMARY KEY (OrderProductID) , FOREIGN KEY (OrderNumber) REFERENCES orderdb.`Order`(OrderNumber), FOREIGN KEY (ProductID) REFERENCES orderdb.Product(ProductID)); DECIMAL(5, 2) indicates price can no larger than 999.99.

Be careful! Removing tables DROP TABLE schema_name.table_name; Example: DROP TABLE orderdb.Customer; This deletes the entire table and all data! It’s a pain to get it back (if you can at all)! Be careful!

Changing a table’s metadata ALTER TABLE schema_name.table_name ADD COLUMN column_name datatype [NULL][NOT NULL]; or ALTER TABLE schema_name.table_name DROP COLUMN column_name; CHANGE COLUMN old_column_name new_column_name datatype [NULL][NOT NULL]; Adds a column to the table Removes a column from the table Changes a column in the table

An example of each Adds ‘Manufacturer’ column to Product table ALTER TABLE orderdb.Product ADD COLUMN Manufacturer VARCHAR(45) NULL; ALTER TABLE orderdb.Product DROP COLUMN Manufacturer; Removes ‘Manufacturer’ column from Product table

An example of each Changes name of Price column in Product table to SalesPrice and its data type to DECIMAL(6,2) ALTER TABLE orderdb.Product CHANGE COLUMN Price SalesPrice DECIMAL(6,2) NULL; ALTER TABLE orderdb.Product CHANGE COLUMN Price Price DECIMAL(6,2) NULL; Changes data type of Price column in Product table to DECIMAL(6,2) but leaves the name unchanged.

Adding a row to a table (versus columns) A change in the table structure Done using ALTER TABLE Adding a column A change in the table data Done using INSERT INTO Adding a row

Adding a row INSERT INTO schema_name.table_name (columnName1, columnName2, columnName3) VALUES (value1, value2, value3); Item Description schema_name The schema that contains the table table_name The name of the table columnName The name of the field value The data value for the field datatype The datatype of the field BIG TIP: The order of the values MUST match the order of the field names!

INSERT example INSERT INTO orderdb.Customer (CustomerID, FirstName, LastName, City, State, Zip) VALUES (1005, 'Chris', 'Taub', 'Princeton', 'NJ', '09120'); CustomerID FirstName LastName City State Zip 1001 Greg House Princeton NJ 09120 1002 Lisa Cuddy Plainsboro 09123 1003 James Wilson Pittsgrove 09121 1004 Eric Foreman Warminster PA 19111 1005 Chris Taub BIG TIP: Note that field names are surrounded by back quotes (`) and string field values are surrounded by regular single quotes (')

Changing a row UPDATE schema_name.table_name SET columnName1=value1, columnName2=value2 WHERE condition; Item Description schema_name The schema that contains the table table_name The name of the table columnName The name of the field value The data value for the field condition A conditional statement to specify the records which should be changed

UDPATE example UPDATE orderdb.Product SET ProductName='Honey Nut Cheerios', Price=4.50 WHERE ProductID=2251; The “safest” way to UPDATE is one record at a time, based on the primary key field. ProductID ProductName Price 2251 Honey Nut Cheerios 4.50 2282 Bananas 1.29 2505 Eggo Waffles 2.99 ProductID ProductName Price 2251 Cheerios 3.99 2282 Bananas 1.29 2505 Eggo Waffles 2.99 Product

Changing multiple rows UPDATE orderdb.Customer SET City='Cherry Hill' WHERE State='NJ'; CustomerID FirstName LastName City State Zip 1001 Greg House Princeton NJ 09120 1002 Lisa Cuddy Plainsboro 09123 1003 James Wilson Pittsgrove 09121 1004 Eric Foreman Warminster PA 19111 Be careful! You can do a lot of damage with a query like this! CustomerID FirstName LastName City State Zip 1001 Greg House Cherry Hill NJ 09120 1002 Lisa Cuddy 09123 1003 James Wilson 09121 1004 Eric Foreman Warminster PA 19111

Deleting a row DELETE FROM schema_name.table_name WHERE condition; Item Description schema_name The schema that contains the table table_name The name of the table condition A conditional statement to specify the records which should be changed

Again, the “safest” way to DELETE is based on the primary key field. DELETE example DELETE FROM orderdb.Customer WHERE CustomerID=1004; Again, the “safest” way to DELETE is based on the primary key field. CustomerID FirstName LastName City State Zip 1001 Greg House Princeton NJ 09120 1002 Lisa Cuddy Plainsboro 09123 1003 James Wilson Pittsgrove 09121 1004 Eric Foreman Warminster PA 19111 CustomerID FirstName LastName City State Zip 1001 Greg House Princeton NJ 09120 1002 Lisa Cuddy Plainsboro 09123 1003 James Wilson Pittsgrove 09121

Deleting multiple rows Avoid doing this! DELETE FROM orderdb.Customer WHERE CustomerID>1002; CustomerID FirstName LastName City State Zip 1001 Greg House Princeton NJ 09120 1002 Lisa Cuddy Plainsboro 09123 1003 James Wilson Pittsgrove 09121 1004 Eric Foreman Warminster PA 19111 CustomerID FirstName LastName City State Zip 1001 Greg House Princeton NJ 09120 1002 Lisa Cuddy Plainsboro 09123

One more DELETE example DELETE FROM orderdb.Customer WHERE State='NJ' AND Zip='09121‘; CustomerID FirstName LastName City State Zip 1001 Greg House Princeton NJ 09120 1002 Lisa Cuddy Plainsboro 09123 1003 James Wilson Pittsgrove 09121 1004 Eric Foreman Warminster PA 19111 CustomerID FirstName LastName City State Zip 1001 Greg House Princeton NJ 09120 1002 Lisa Cuddy Plainsboro 09123 1004 Eric Foreman Warminster PA 19111

Summary 1 Creating Tables Removing Tables CREATE TABLE schema_name.table_name ( columnName1 datatype [NULL][NOT NULL], columnName2 datatype [NULL][NOT NULL], PRIMARY KEY (KeyName), FOREIGN KEY (KeyName) REFERENCES schema_name.table_name (KeyName) ); Removing Tables DROP TABLE schema_name.table_name;

Summary 2 Changing a table’s metadata ALTER TABLE schema_name.table_name ADD COLUMN column_name datatype [NULL][NOT NULL]; ALTER TABLE schema_name.table_name DROP COLUMN column_name; ALTER TABLE schema_name.table_name CHANGE COLUMN old_column_name new_column_name datatype [NULL][NOT NULL];

Summary 3 Adding rows Updating rows Deleting rows INSERT INTO schema_name.table_name (columnName1, columnName2, columnName3) VALUES (value1, value2, value3); Updating rows UPDATE schema_name.table_name SET columnName1=value1, columnName2=value2 WHERE condition; Deleting rows DELETE FROM schema_name.table_name WHERE condition;