MIS2502: Data Analytics SQL – Putting Information Into a Database David Schuff

Slides:



Advertisements
Similar presentations
SQL components In Oracle. SQL in Oracle SQL is made up of 4 components: –DDL Data Definition Language CREATE, ALTER, DROP, TRUNCATE. Creates / Alters.
Advertisements

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”
DAT702.  Standard Query Language  Ability to access and manipulate databases ◦ Retrieve data ◦ Insert, delete, update records ◦ Create and set permissions.
MIS2502: Data Analytics Relational Data Modeling
DAY 21: MICROSOFT ACCESS – CHAPTER 5 MICROSOFT ACCESS – CHAPTER 6 MICROSOFT ACCESS – CHAPTER 7 Akhila Kondai October 30, 2013.
MySql In Action Step by step method to create your own database.
A Guide to SQL, Eighth Edition Chapter Three Creating Tables.
Session 5: Working with MySQL iNET Academy Open Source Web Development.
Chapter 5 Introduction to SQL. Structured Query Language = the “programming language” for relational databases SQL is a nonprocedural language = the user.
ASP.NET Programming with C# and SQL Server First Edition
 SQL stands for Structured Query Language.  SQL lets you access and manipulate databases.  SQL is an ANSI (American National Standards Institute) standard.
MIS2502: Data Analytics Coverting ERD into a DB Schema David Schuff
MIS 301 Information Systems in Organizations Dave Salisbury ( )
Introduction to MySQL Lab no. 10 Advance Database Management System.
CSC 2720 Building Web Applications Database and SQL.
SQL 1: GETTING INFORMATION OUT OF A DATABASE MIS2502 Data Analytics.
CPS120: Introduction to Computer Science Lecture 19 Introduction to SQL.
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.
DAT602 Database Application Development Lecture 3 Review of SQL Language.
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.
SQL John Nowobilski. What is SQL? Structured Query Language Manages Data in Database Management Systems based on the Relational Model Developed in 1970s.
Fox MIS Spring 2011 Database Week 6 ERD and SQL Exercise.
MIS2502: Data Analytics Relational Data Modeling
>> Introduction to MySQL. Introduction Structured Query Language (SQL) – Standard Database Language – Manage Data in a DBMS (Database Management System)
RELATIONAL DATA MODELING MIS2502 Data Analytics. What is a model? Representation of something in the real world.
Relational Database Management System(RDBMS) Structured Query Language(SQL)
Distribution of Marks For Second Semester Internal Sessional Evaluation External Evaluation Assignment /Project QuizzesClass Attendance Mid-Term Test Total.
MIS2502: Data Analytics SQL – Getting Information Out of a Database.
MIS2502: Data Analytics Relational Data Modeling David Schuff
Exam 2 Review: SQL In, Dimensional Modeling, Pivot Tables, ETL Describe data cube elements Understand facts, dimensions, granularity Create/read a Star.
MICROSOFT ACCESS – CHAPTER 5 MICROSOFT ACCESS – CHAPTER 6 MICROSOFT ACCESS – CHAPTER 7 Sravanthi Lakkimsety Mar 14,2016.
Understand Data Definition Language (DDL) Database Administration Fundamentals LESSON 1.4.
 MySQL  DDL ◦ Create ◦ Alter  DML ◦ Insert ◦ Select ◦ Update ◦ Delete  DDL(again) ◦ Drop ◦ Truncate.
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.
COM621: Advanced Interactive Web Development Lecture 11 MySQL – Data Manipulation Language.
Fundamentals of DBMS Notes-1.
How to: SQL By: Sam Loch.
Web Systems & Technologies
CS SQL.
Chapter 5 Introduction to SQL.
SQL: Schema Definition and Constraints Chapter 6 week 6
Insert, Update and the rest…
MIS2502: Data Analytics SQL – Putting Information Into a Database
MIS2502: Data Analytics Relational Data Modeling
MIS5101: Business Intelligence Relational Data Modeling
MIS2502: Data Analytics SQL – Putting Information Into a Database
Lecturer: Mukhtar Mohamed Ali “Hakaale”
MIS2502: Data Analytics SQL – Getting Information Out of a Database
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
Exam 2 Exam 2 Study Guide is posted on the Course Site
MIS2502: Data Analytics SQL – Putting Information Into a Database
MIS2502: Review for Exam 1 Aaron Zhi Cheng
MIS2502: Data Analytics SQL – Putting Information Into a Database
MIS2502: Data Analytics SQL – Putting Information Into a Database
MIS2502: Data Analytics SQL 4– Putting Information Into a Database
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 David Schuff

Our relational database A series of tables Linked together through primary/foreign key relationships

To create a database We need to define – The tables – The fields (columns) within those tables – The data types of those fields There are SQL commands that do each of those things So let`s assume that our database didn`t exist and we needed to create the tables

CREATE statement (create a table) CREATE TABLE schema_name.table_name ( columnName1 datatype [NULL][NOT NULL], columnName2 datatype [NULL][NOT NULL], PRIMARY KEY (KeyName) ); ItemDescription schema_nameThe schema that will contain the table table_nameThe name of the table columnNameThe name of the field datatypeThe datatype of the field [NULL][NOTNULL]Whether the field can be empty (i.e., null) (The [] means the parameter is optional) KeyNameThe name of the field that will serve as the primary key

Example: Creating the Customer Table 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) ); Customer CustomerID FirstName LastName City State Zip Based on this SQL statement: The only required field is CustomerID – the rest can be left blank. CustomerID is defined as the primary key. Based on this SQL statement: The only required field is CustomerID – the rest can be left blank. CustomerID is defined as the primary key. Remember, your schema will use your mx MySQL ID (i.e., m999orderdb.Customer)

Looking at the “new” Customer table Column nameData type CustomerIDINT FirstNameVARCHAR(45) LastNameVARCHAR(45) CityVARCHAR(45) StateVARCHAR(2) ZipVARCHAR(10) Customer 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”

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 typeDescriptionExamples INTInteger3, -10 DECIMAL(n,n)Decimal3.23, VARCHAR(n)String (numbers and letters)Hello, I like pizza, MySQL! DATETIMEDate/Time (or just date) :35:00, BOOLEANBoolean value0 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. 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

Some more create statements CREATE TABLE orderdb.`Order` ( OrderNumber INT NOT NULL, OrderDate DATETIME NULL, CustomerID INT NULL, PRIMARY KEY (OrderNumber) ); CREATE TABLE orderdb.Product ( ProductID INT NOT NULL, ProductName VARCHAR(45) NULL, Price DECIMAL(5,2) NULL, PRIMARY KEY (ProductID) ); Order OrderNumber OrderDate CustomerID Product ProductID ProductName Price DECIMAL(5, 2) indicates price can no larger than

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_name datatype [NULL][NOT NULL] ; or ALTER TABLE schema_name.table_name DROP COLUMN column_name; or ALTER TABLE schema_name.table_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 ALTER TABLE orderdb.Product ADD COLUMN `Manufacturer` VARCHAR(45) NULL ; ALTER TABLE orderdb.Product DROP COLUMN `Manufacturer; Adds ‘Manufacturer’ column to Product table Removes ‘Manufacturer’ column from Product table

An example of each 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 name of Price column in Product table to SalesPrice and its data type to DECIMAL(6,2) Changes data type of Price column in Product table to DECIMAL(6,2) but leaves the name unchanged.

Let’s try it! First, let’s create a table named Payroll in directdb, which contains 4 columns, payroll_id, amount, date, description; CREATE TABLE directdb.Payroll ( payroll_id INT NOT NULL, `date` DATETIME NULL, amount INT NULL, description VARCHAR(100) NULL, PRIMARY KEY (payroll_id) );

Let’s try it! Now, let’s add a new column to the table to store withholding tax rate. ALTER TABLE directdb.payroll ADD COLUMN tax_rate DECIMAL(4,2) NULL;

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

INSERT INTO schema_name.table_name (columnName1, columnName2, columnName3) VALUES (value1, value2, value3); ItemDescription schema_nameThe schema that contains the table table_nameThe name of the table columnNameThe name of the field valueThe data value for the field datatypeThe 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'); CustomerIDFirstNameLastNameCityStateZip 1001GregHousePrincetonNJ LisaCuddyPlainsboroNJ JamesWilsonPittsgroveNJ EricForemanWarminsterPA ChrisTaubPrincetonNJ09120 BIG TIP: Note that field names are surrounded by “back quotes” (`) and string field values are surrounded by “regular quotes” (')

Changing a row UPDATE schema_name.table_name SET columnName1=value1, columnName2=value2 WHERE condition; UPDATE `test`.`product` SET `ProductName`='Honey Nut Cheerios', `Price`='4.50' WHERE `ProductID`='2251'; ItemDescription schema_nameThe schema that contains the table table_nameThe name of the table columnNameThe name of the field valueThe data value for the field conditionA 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; ProductIDProductNamePrice 2251Cheerios Bananas Eggo Waffles2.99 Product ProductIDProductNamePrice 2251Honey Nut Cheerios Bananas Eggo Waffles2.99 The “safest” way to UPDATE is one record at a time, based on the primary key field.

Changing multiple rows UPDATE orderdb.Customer SET City='Cherry Hill' WHERE State='NJ'; CustomerIDFirstNameLastNameCityStateZip 1001GregHousePrincetonNJ LisaCuddyPlainsboroNJ JamesWilsonPittsgroveNJ EricForemanWarminsterPA19111 CustomerIDFirstNameLastNameCityStateZip 1001GregHouseCherry HillNJ LisaCuddyCherry HillNJ JamesWilsonCherry HillNJ EricForemanWarminsterPA19111 Be careful! You can do a lot of damage with a query like this! Be careful! You can do a lot of damage with a query like this!

Deleting a row DELETE FROM schema_name.table_name WHERE condition; ItemDescription schema_nameThe schema that contains the table table_nameThe name of the table conditionA conditional statement to specify the records which should be changed

DELETE example DELETE FROM orderdb.Customer WHERE CustomerID=1004; CustomerIDFirstNameLastNameCityStateZip 1001GregHousePrincetonNJ LisaCuddyPlainsboroNJ JamesWilsonPittsgroveNJ EricForemanWarminsterPA19111 CustomerIDFirstNameLastNameCityStateZip 1001GregHousePrincetonNJ LisaCuddyPlainsboroNJ JamesWilsonPittsgroveNJ09121

Deleting multiple rows DELETE FROM orderdb.Customer WHERE CustomerID>1002; CustomerIDFirstNameLastNameCityStateZip 1001GregHousePrincetonNJ LisaCuddyPlainsboroNJ JamesWilsonPittsgroveNJ EricForemanWarminsterPA19111 CustomerIDFirstNameLastNameCityStateZip 1001GregHousePrincetonNJ LisaCuddyPlainsboroNJ09123 Avoid doing this!

One more DELETE example DELETE FROM orderdb.Customer WHERE State='NJ' AND Zip='09121' CustomerIDFirstNameLastNameCityStateZip 1001GregHousePrincetonNJ LisaCuddyPlainsboroNJ JamesWilsonPittsgroveNJ EricForemanWarminsterPA19111 CustomerIDFirstNameLastNameCityStateZip 1001GregHousePrincetonNJ LisaCuddyPlainsboroNJ EricForemanWarminsterPA19111