SQL 2: GETTING INFORMATION INTO A DATABASE. MIS2502 Data Analytics

Similar documents
SQL. Short introduction

A table is a collection of related data entries and it consists of columns and rows.

A basic create statement for a simple student table would look like the following.

Database Query 1: SQL Basics

Mul$media im Netz (Online Mul$media) Wintersemester 2014/15. Übung 03 (Nebenfach)

Structured Query Language. Telemark University College Department of Electrical Engineering, Information Technology and Cybernetics

Introduction to Database. Systems HANS- PETTER HALVORSEN,

A Brief Introduction to MySQL

Developing Web Applications for Microsoft SQL Server Databases - What you need to know

P_Id LastName FirstName Address City 1 Kumari Mounitha VPura Bangalore 2 Kumar Pranav Yelhanka Bangalore 3 Gubbi Sharan Hebbal Tumkur

How To Create A Table In Sql (Ahem)

Database Development and Management with SQL

Learning MySQL! Angola Africa SELECT name, gdp/population FROM world WHERE area > !

Advance DBMS. Structured Query Language (SQL)

2/3/04 Doc 7 SQL Part 1 slide # 1

Introduction to SQL and database objects

Review your answers, feedback, and question scores below. An asterisk (*) indicates a correct answer.

Once the schema has been designed, it can be implemented in the RDBMS.

Tutorial on Relational Database Design

Database Design Process. ER Model. Goals. Relational Model. SQL - The Language of Databases. IT420: Database Management and Organization

!"# $ %& '( ! %& $ ' &)* + ! * $, $ (, ( '! -,) (# *&23. mysql> select * from from clienti;

In This Lecture. SQL Data Definition SQL SQL. Notes. Non-Procedural Programming. Database Systems Lecture 5 Natasha Alechina

INTRODUCTION TO DATABASE SYSTEMS

DbSchema Tutorial with Introduction in SQL Databases

Darshan Institute of Engineering & Technology PL_SQL

Review your answers, feedback, and question scores below. An asterisk (*) indicates a correct answer.

Oracle Database 10g Express

MYSQL DATABASE ACCESS WITH PHP

Chapter 9 Java and SQL. Wang Yang wyang@njnet.edu.cn

CSC 443 Data Base Management Systems. Basic SQL

Microsoft Access 3: Understanding and Creating Queries

B.1 Database Design and Definition

Concepts Design Basics Command-line MySQL Security Loophole

SQL Injection. SQL Injection. CSCI 4971 Secure Software Principles. Rensselaer Polytechnic Institute. Spring

VBScript Database Tutorial Part 1

CSE 530A Database Management Systems. Introduction. Washington University Fall 2013

SQL - QUICK GUIDE. Allows users to access data in relational database management systems.

Other Language Types CMSC 330: Organization of Programming Languages

3.GETTING STARTED WITH ORACLE8i

Physical Design. Meeting the needs of the users is the gold standard against which we measure our success in creating a database.

Creating Database Tables in Microsoft SQL Server

CSI 2132 Lab 3. Outline 09/02/2012. More on SQL. Destroying and Altering Relations. Exercise: DROP TABLE ALTER TABLE SELECT

Database Migration from MySQL to RDM Server

SQL Server An Overview

Knocker main application User manual

How to use MySQL Workbench and other development tools

Access Queries (Office 2003)

types, but key declarations and constraints Similar CREATE X commands for other schema ëdrop X name" deletes the created element of beer VARCHARè20è,

A Migration Methodology of Transferring Database Structures and Data

MySQL for Beginners Ed 3

Understanding Sql Injection

Best Practices in SQL Programming. Madhivanan

Linas Virbalas Continuent, Inc.

Oracle Database 12c: Introduction to SQL Ed 1.1

Lesson 07: MS ACCESS - Handout. Introduction to database (30 mins)

6 CHAPTER. Relational Database Management Systems and SQL Chapter Objectives In this chapter you will learn the following:

Microsoft SQL connection to Sysmac NJ Quick Start Guide

Application note: Connecting the to a Database

Programming Database lectures for mathema

Lab 9 Access PreLab Copy the prelab folder, Lab09 PreLab9_Access_intro

Advanced SQL. Jim Mason. Web solutions for iseries engineer, build, deploy, support, train

MySQL 5.1 INTRODUCTION 5.2 TUTORIAL

SQL Simple Queries. Chapter 3.1 V3.0. Napier University Dr Gordon Russell

SQL Server Database Coding Standards and Guidelines

Database Communication. in LabVIEW

Services. Relational. Databases & JDBC. Today. Relational. Databases SQL JDBC. Next Time. Services. Relational. Databases & JDBC. Today.

The release notes provide details of enhancements and features in Cloudera ODBC Driver for Impala , as well as the version history.

In this session, we use the table ZZTELE with approx. 115,000 records for the examples. The primary key is defined on the columns NAME,VORNAME,STR

Database Administration with MySQL

Download: Server-side technologies. WAMP (Windows), MAMP (Mac),

LAB 1: Getting started with WebMatrix. Introduction. Creating a new database. M1G505190: Introduction to Database Development

Information Technology NVEQ Level 2 Class X IT207-NQ2012-Database Development (Basic) Student s Handbook

Product: DQ Order Manager Release Notes

Unified access to all your data points. with Apache MetaModel

Relational Databases. Christopher Simpkins

Programming Autodesk PLM 360 Using REST. Doug Redmond Software Engineer, Autodesk

Intro to Databases. ACM Webmonkeys 2011

Porting from Oracle to PostgreSQL

Create a Database Driven Application

CS 2316 Data Manipulation for Engineers

Teach Yourself InterBase

Supplement IV.D: Tutorial for MS Access. For Introduction to Java Programming By Y. Daniel Liang

database abstraction layer database abstraction layers in PHP Lukas Smith BackendMedia

Serious Threat. Targets for Attack. Characterization of Attack. SQL Injection 4/9/2010 COMP On August 17, 2009, the United States Justice

Using SQL Server Management Studio

EECS 647: Introduction to Database Systems

LSINF1124 Projet de programmation

SQL INJECTION IN MYSQL

SQL Server Table Design - Best Practices

InterBase 6. Embedded SQL Guide. Borland/INPRISE. 100 Enterprise Way, Scotts Valley, CA

Information Systems SQL. Nikolaj Popov

Exposed Database( SQL Server) Error messages Delicious food for Hackers

Using Temporary Tables to Improve Performance for SQL Data Services

Data Masking. Procedure

MS ACCESS DATABASE DATA TYPES

Geodatabase Programming with SQL

Transcription:

SQL 2: GETTING INFORMATION INTO A DATABASE MIS2502 Data Analytics

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) ); Item schema_name table_name columnname datatype [NULL][NOTNULL] KeyName Description The schema that will contain the table The name of the table The name of the field The datatype of the field Whether the field can be empty (i.e., null) (The [] means the parameter is optional) The name of the field that will serve as the primary key

Example: Creating the Customer Table CREATE TABLE `m1orderdb`.`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.

Looking at the new Customer table Column name CustomerID Data type INT The database management system stores this information about the table Customer FirstName LastName City State Zip VARCHAR(45) VARCHAR(45) VARCHAR(45) VARCHAR(2) VARCHAR(10) 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 type Description Examples INT Integer 3, -10 DECIMAL(n,n) Decimal 3.23, 3.14159 VARCHAR(n) String (numbers and letters) Hello, I like pizza, MySQL! DATETIME 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 `m1orderdb`.`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

Some more create statements CREATE TABLE `m1orderdb`.`order` ( `OrderNumber` INT NOT NULL, `OrderDate` DATETIME NULL, `CustomerID` INT NULL, PRIMARY KEY (`OrderNumber`) ); Order OrderNumber OrderDate CustomerID CREATE TABLE `m1orderdb`.`product` ( `ProductID` INT NOT NULL, `ProductName` VARCHAR(45) NULL, `Price` DECIMAL(5,2) NULL, PRIMARY KEY (`ProductID`) ); Product ProductID ProductName Price DECIMAL(5, 2) indicates price can be as large as 99999.99.

Removing tables DROP TABLE schema_name.table_name Example: DROP TABLE `m1orderdb`.`customer` Be careful! This deletes the entire table And all of its data!

Changing a table s metadata ALTER TABLE schema_name.table_name ADD column_name datatype [NULL][NOTNULL] 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] [NOTNULL] Adds a column to the table Removes a column from the table Changes a column in the table

An example of each ALTER TABLE `m1orderdb`.`product` ADD COLUMN `Manufacturer` VARCHAR(45) NULL Adds Manufacturer column to Product table ALTER TABLE `m1orderdb`.`product` DROP COLUMN `Manufacturer Removes Manufacturer column from Product table

An example of each ALTER TABLE `m1orderdb`.`product` CHANGE COLUMN `Price` `SalesPrice` DECIMAL(6,2) NULL Changes name of Price column in Product table to SalesPrice and its data type to DECIMAL (6.2) ALTER TABLE `m1orderdb`.`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) Adding a column A change in the table structure Done using ALTER TABLE Adding a row 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) Item schema_name table_name columnname value datatype Description The schema that contains the table The name of the table The name of the field The data value for the field The datatype of the field The order of the values MUST match the order of the field names!

INSERT example INSERT INTO `m1orderdb`.`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 NJ 09123 1003 James Wilson Pittsgrove NJ 09121 1004 Eric Foreman Warminster PA 19111 1005 Chris Taub Princeton NJ 09120 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 Item schema_name table_name columnname Description The schema that contains the table The name of the table The name of the field UPDATE `test`.`product` SET value The data value for the field condition `ProductName`='Honey Nut Cheerios', `Price`='4.50' should WHERE be changed `ProductID`='2251'; A conditional statement to specify the records which

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

Changing multiple rows UPDATE `m1orderdb`.`customer` SET City='Cherry Hill' WHERE State='NJ' CustomerID FirstName LastName City State Zip 1001 Greg House Princeton NJ 09120 1002 Lisa Cuddy Plainsboro NJ 09123 1003 James Wilson Pittsgrove NJ 09121 1004 Eric Foreman Warminster PA 19111 CustomerID FirstName LastName City State Zip 1001 Greg House Cherry Hill NJ 09120 1002 Lisa Cuddy Cherry Hill NJ 09123 1003 James Wilson Cherry Hill NJ 09121 1004 Eric Foreman Warminster PA 19111 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 Item schema_name table_name condition Description The schema that contains the table The name of the table A conditional statement to specify the records which should be changed

DELETE example DELETE FROM `m1orderdb`.`customer` WHERE `CustomerID`=1004 CustomerID FirstName LastName City State Zip 1001 Greg House Princeton NJ 09120 1002 Lisa Cuddy Plainsboro NJ 09123 1003 James Wilson Pittsgrove NJ 09121 1004 Eric Foreman Warminster PA 19111 CustomerID FirstName LastName City State Zip 1001 Greg House Princeton NJ 09120 1002 Lisa Cuddy Plainsboro NJ 09123 1003 James Wilson Pittsgrove NJ 09121

Deleting multiple rows DELETE FROM `m1orderdb`.`customer` WHERE `CustomerID`>1002 CustomerID FirstName LastName City State Zip 1001 Greg House Princeton NJ 09120 1002 Lisa Cuddy Plainsboro NJ 09123 1003 James Wilson Pittsgrove NJ 09121 1004 Eric Foreman Warminster PA 19111 CustomerID FirstName LastName City State Zip 1001 Greg House Princeton NJ 09120 1002 Lisa Cuddy Plainsboro NJ 09123

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