Creating Tables & Inserting Values Using SQL

Slides:



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

Creating Tables, Setting Constraints, and Datatypes What is a constraint and why do we use it? What is a datatype? What does CHAR mean? Page 97 in Course.
SQL - Subqueries and Schema Chapter 3.4 V3.0 Napier University Dr Gordon Russell.
Database Basics I101 Summer 2006 Copyright 2004, Matt Hottell.
Structured Query Language - SQL Carol Wolf Computer Science.
Beginning SQL Tutorial Author Jay Mussan-Levy. What is SQL?  Structured Query Language  Communicate with databases  Used to created and edit databases.
This course has taken from This unique introductory SQL tutorial not only provides easy-to-understand SQL instructions, but it allows.
A Guide to SQL, Seventh Edition. Objectives Understand the concepts and terminology associated with relational databases Create and run SQL commands in.
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”
Entity Relationship Diagram Farrokh Alemi Ph.D. Francesco Loaiza, Ph.D. J.D. Vikas Arya.
Oracle Data Definition Language (DDL)
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
CHAPTER:14 Simple Queries in SQL Prepared By Prepared By : VINAY ALEXANDER ( विनय अलेक्सजेंड़र ) PGT(CS),KV JHAGRAKHAND.
CSC 2720 Building Web Applications Database and SQL.
Oracle Data Definition Language (DDL) Dr. Bernard Chen Ph.D. University of Central Arkansas Fall 2008.
Information Technologies and Microsoft SQL Server Day 2 by Alper Özpınar
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.
6 1 Lecture 8: Introduction to Structured Query Language (SQL) J. S. Chou, P.E., Ph.D.
WHAT IS A DATABASE? A DATABASE IS A COLLECTION OF DATA RELATED TO A PARTICULAR TOPIC OR PURPOSE OR TO PUT IT SIMPLY A GENERAL PURPOSE CONTAINER FOR STORING.
1 CSE 2337 Introduction to Data Management Access Book – Ch 1.
Relational Databases. Relational database  data stored in tables  must put data into the correct tables  define relationship between tables  primary.
Fox MIS Spring 2011 Database Week 6 ERD and SQL Exercise.
Database Basics BCIS 3680 Enterprise Programming.
Introduction to Database Programming with Python Gary Stewart
DATA TYPES.
Prepared By: Bobby Wan Microsoft Access Prepared By: Bobby Wan
Chapter 5 Introduction to SQL.
Databases Chapter 9 Asfia Rahman.
Managing Tables, Data Integrity, Constraints by Adrienne Watt
CHAPTER 7 DATABASE ACCESS THROUGH WEB
Practical Office 2007 Chapter 10
MIS2502: Data Analytics SQL – Putting Information Into a Database
Data Definition and Data Types
Database application MySQL Database and PhpMyAdmin
Objectives Identify the built-in data types in C++
Unit 9.2 Database access from code Database Cycle Review
MIS2502: Data Analytics SQL – Putting Information Into a Database
Dead Man Visiting Farrokh Alemi, PhD Narrated by …
ORACLE SQL Developer & SQLPLUS Statements
ISC440: Web Programming 2 Server-side Scripting PHP 3
STRUCTURED QUERY LANGUAGE
SQL Text Manipulation Farrokh Alemi, Ph.D.
MODULE 7 Microsoft Access 2010
Graphical Interface for Queries
What Are Databases? Organized by Dr. Farrokh Alemi PhD
Types of Joins Farrokh Alemi, Ph.D.
SQL for Cleaning Data Farrokh Alemi, Ph.D.
SELECT & FROM Commands Farrokh Alemi, PhD
Rank Order Function Farrokh Alemi, Ph.D.
Date Functions Farrokh Alemi, Ph.D.
Procedures Organized by Farrokh Alemi, Ph.D. Narrated by Yara Alemi
CIS16 Application Programming with Visual Basic
MIS2502: Data Analytics SQL – Putting Information Into a Database
Oracle Data Definition Language (DDL)
Relationships as Primary & Foreign Keys
MIS2502: Data Analytics SQL – Putting Information Into a Database
CS122 Using Relational Databases and SQL
Convert from Variable Character to Float
Indexing & Computational Efficiency
Spreadsheets, Modelling & Databases
CS1222 Using Relational Databases and SQL
Data Definition Language
MIS2502: Data Analytics SQL 4– Putting Information Into a Database
ICT Database Lesson 2 Designing a Database.
CS122 Using Relational Databases and SQL
Database Instructor: Bei Kang.
SQL (Structured Query Language)
Presentation transcript:

Creating Tables & Inserting Values Using SQL Farrokh Alemi, PhD This set of slides were organized by Professor Alemi and describe how to create tables using SQL commands. We create three tables and link them together to create a relational database. Assume that you need to prepare a database that contains three entities: Patients, Providers and Encounters. For each of these three entities, we need to create separate tables.  Each table will describe the attributes of one of the three entities.  Each attribute will be a separate field.

SQL Can Create Tables Most of the time, the data needed is imported, such import often include the table definition. Sometimes the tables are created and must be defined.

CREATE TABLE table_name (     column1 datatype,     column2 datatype,     column3 datatype,    .... ); This slides shows the create table command format. The words create and table are reserved words in SQL.

CREATE TABLE table_name (     column1 datatype,     column2 datatype,     column3 datatype,    .... ); The command includes table name.

CREATE TABLE table_name (     column1 datatype,     column2 datatype,     column3 datatype,    .... ); Inside parenthesis it includes column names or field names.

CREATE TABLE table_name (     column1 datatype,     column2 datatype,     column3 datatype,    .... ); The data type parameter specifies the type of data the column can hold.

Data types are specified on the web but the most common data types are: variable character, integer, float, date, and text. Always consult the web for exact data types allowed in your implementation of SQL code, as there are variations in different implementations.

CAST and CONVERT commands can change data from one type to another Cast and convert commands can change data from one type to another but there are many exceptions where the data cannot be converted. For example, to convert text to integer one has to make sure that all entries in the text fields are digits and not letters before one can proceed with the conversion.

Telephone of the Patient Patient Table First Name Last  Name Zip Code City State Date of Birth Email Telephone of the Patient Larry Kim 22101 Mclean DC 08-Jan-54 email@test.edu 703-9934226 George Smith 22102 McLean Virginia 09-Sep-60 email@tes.com (703) 8884545 Jill VA 01-Aug-89 test@test.com 703 993 4226 The patient attributes are shown in this slide and are assumed to be first name, last name, date of birth, email, address including street name, street number, city, State and zip code. 

Telephone of the Patient Patient Table String String String String First Name Last  Name Zip Code City State Date of Birth Email Telephone of the Patient Larry Kim 22101 Mclean DC 08-Jan-54 email@test.edu 703-9934226 George Smith 22102 McLean Virginia 09-Sep-60 email@tes.com (703) 8884545 Jill VA 01-Aug-89 test@test.com 703 993 4226 First name is a string of maximum size 20. Last name is a string of maximum size 50. These are not reasonable maximum lengths; many names and last names will exceed these sizes, but we are trying a simple example. City and phone numbers are entered in as text.

Telephone of the Patient Patient Table Integer Date First Name Last  Name Zip Code City State Date of Birth Email Telephone of the Patient Larry Kim 22101 Mclean DC 08-Jan-54 email@test.edu 703-9934226 George Smith 22102 McLean Virginia 09-Sep-60 email@tes.com (703) 8884545 Jill VA 01-Aug-89 test@test.com 703 993 4226 Zip code is integer and not decimals, floats or real numbers. Date of birth is a date. 

Telephone of the Patient Patient Table First Name Last  Name Zip Code City State Date of Birth Email Telephone of the Patient Larry Kim 22101 Mclean DC 08-Jan-54 email@test.edu 703-9934226 George Smith 22102 McLean Virginia 09-Sep-60 email@tes.com (703) 8884545 Jill VA 01-Aug-89 test@test.com 703 993 4226 Note that George and Jill Smith might be living in the same place.

Telephone of the Patient Patient Table First Name Last  Name Zip Code City State Date of Birth Email Telephone of the Patient Larry Kim 22101 Mclean DC 08-Jan-54 email@test.edu 703-9934226 George Smith 22102 McLean Virginia 09-Sep-60 email@tes.com (703) 8884545 Jill VA 01-Aug-89 test@test.com 703 993 4226 Note that states are entered in different ways, sometimes referring to Virginia by its abbreviation and others times spelling it out. Note how the letter L in McLean is sometimes capitalized and other times not. Note for some phone numbers, the area code is in parentheses and for others not. All of this variability in data entry can create errors in data processing and these variations must be corrected before proceeding.

Telephone of the Patient Patient Table First Name Last  Name Zip Code City State Date of Birth Email Telephone of the Patient Larry Kim 22101 Mclean DC 08-Jan-54 email@test.edu 703-9934226 George Smith 22102 McLean Virginia 09-Sep-60 email@tes.com (703) 8884545 Jill VA 01-Aug-89 test@test.com 703 993 4226 Note how the letter L in McLean is sometimes capitalized and other times not.

Telephone of the Patient Patient Table First Name Last  Name Zip Code City State Date of Birth Email Telephone of the Patient Larry Kim 22101 Mclean DC 08-Jan-54 email@test.edu 703-9934226 George Smith 22102 McLean Virginia 09-Sep-60 email@tes.com (703) 8884545 Jill VA 01-Aug-89 test@test.com 703 993 4226 Note for some phone numbers, the area code is in parentheses and for others not. All of this variability in data entry can create errors in data processing and these variations must be corrected before proceeding.

Telephone of the Patient Remove Variability in Data Entry Patient Table First Name Last  Name Zip Code City State Date of Birth Email Telephone of the Patient Larry Kim 22101 Mclean DC 08-Jan-54 email@test.edu 703-9934226 George Smith 22102 McLean Virginia 09-Sep-60 email@tes.com (703) 8884545 Jill VA 01-Aug-89 test@test.com 703 993 4226 Remove Variability in Data Entry All of this variability in data entry can create errors in data processing and these variations must be corrected before proceeding.

CREATE TABLE #Patient ( [First Name] char(20), [Last Name] char(50), [Street Number] Int, [Street] Text, [Zip Code] Int, [Birth Date] Date, [Email] text, [State] Text, [Phone Number] Text, [Patient ID] int IDENTITY(1,1) PRIMARY KEY ) Here is a code that can create the patient table.

In [ ] CREATE TABLE #Patient ( [First Name] char(20), [Last Name] char(50), [Street Number] Int, [Street] Text, [Zip Code] Int, [Birth Date] Date, [Email] text, [State] Text, [Phone Number] Text, [Patient ID] int IDENTITY(1,1) PRIMARY KEY ) In [ ] Note that field names are put in brackets because they contain spaces.

# CREATE TABLE #Patient ( [First Name] char(20), [Last Name] char(50), [Street Number] Int, [Street] Text, [Zip Code] Int, [Birth Date] Date, [Email] text, [State] Text, [Phone Number] Text, [Patient ID] int IDENTITY(1,1) PRIMARY KEY ) # Also note that the hash tag before the table name indicates that the table is a temporary table which will disappear once the SQL window is closed.

Primary Key CREATE TABLE #Patient ( [First Name] char(20), [Last Name] char(50), [Street Number] Int, [Street] Text, [Zip Code] Int, [Birth Date] Date, [Email] text, [State] Text, [Phone Number] Text, [Patient ID] int IDENTITY(1,1) PRIMARY KEY ) Primary Key Note that the primary key is automatically generated as an integer that is aggregated by 1 every time a new record is entered.

Provider Table First Name Last Name Board Certified Email Telephone Employee ID Jim Jones Yes jl@w.com 3456714545 452310 Jill Smith No js@w.com 3454561234 454545 George John g@w.com 3104561234 456734 The provider attributes are assumed to be first name (text of size 20), last name (text of size 50), whether they are board certified (a yes/no value), date of hire, telephone in text format and email in text of size 75.

Provider Table Primary Key First Name Last Name Board Certified Email Telephone Employee ID Jim Jones Yes jl@w.com 3456714545 452310 Jill Smith No js@w.com 3454561234 454545 George John g@w.com 3104561234 456734 The provider attributes are assumed to be first name (text of size 20), last name (text of size 50), whether they are board certified (a yes/no value), date of hire, telephone in text format and email in text of size 75. Employer's ID number should be the primary key for the table. This slide shows the first three providers; note that one of the patients, Jill Smith, previously described in patient Table is also a provider.

Provider Table First Name Last Name Board Certified Email Telephone Employee ID Jim Jones Yes jl@w.com 3456714545 452310 Jill Smith No js@w.com 3454561234 454545 George John g@w.com 3104561234 456734 This slide shows the first three providers;

Provider Table First Name Last Name Board Certified Email Telephone Employee ID Jim Jones Yes jl@w.com 3456714545 452310 Jill Smith No js@w.com 3454561234 454545 George John g@w.com 3104561234 456734 note that one of the patients, Jill Smith, previously described in patient Table is also a provider.

CREATE TABLE #Provider ( [First Name] char(20), [Last Name] char(50), [Board Certified] bit, [Date of Hire] Date, [Phone] Text, [Email] char(75), [Patient ID] int IDENTITY(1,1) PRIMARY KEY ); The following shows the code to create the provider table in SQL. Create Table command organizes the fields in the provider table.

CREATE TABLE #Provider ( [First Name] char(20), [Last Name] char(50), [Board Certified] bit, [Date of Hire] Date, [Phone] Text, [Email] char(75), [Patient ID] int IDENTITY(1,1) PRIMARY KEY ); Primary Key Note that patient ID is generated automatically as an integer starting with 1 and increasing by 1 each time there is an entry.

CREATE TABLE #Provider ( [First Name] char(20), [Last Name] char(50), [Board Certified] bit, [Date of Hire] Date, [Phone] Text, [Email] char(75), [Patient ID] int IDENTITY(1,1) PRIMARY KEY ); 1, 0, or Null In SQL server there is no Yes/No field but the closet data type is a bit type, which assigns it a value of 1, 0, or Null. Here is the code that will create this table:

Encounter Table ID Patient ID Provider ID Date of Encounter Diagnosis Treatment 1 452310 10-Jan-04 Hypertension Assessment 2 17-Jan-04 Heart Failure Monitoring 3 Null 4 5 454545 Asthma Education The encounter has patient ID as integer, provider ID as integer, diagnosis as text of size 50, and date of encounter. The table also includes an ID as a primary key, which start6s at 1 and incremented by 1 each time there is an entry.  Each encounter should have its own ID number. 

Encounter Table ID Patient ID Provider ID Date of Encounter Diagnosis Treatment 1 452310 10-Jan-04 Hypertension Assessment 2 17-Jan-04 Heart Failure Monitoring 3 Null 4 5 454545 Asthma Education The table also includes an ID as a primary key, which start6s at 1 and incremented by 1 each time there is an entry.  Each encounter has its own ID number. 

Encounter Table ID Patient ID Provider ID Date of Encounter Diagnosis Treatment 1 452310 10-Jan-04 Hypertension Assessment 2 17-Jan-04 Heart Failure Monitoring 3 Null 4 5 454545 Asthma Education Patient ID is a foreign key in the encounter table. It can be duplicated, as when the same patient has multiple visits.

Encounter Table ID Patient ID Provider ID Date of Encounter Diagnosis Treatment 1 452310 10-Jan-04 Hypertension Assessment 2 17-Jan-04 Heart Failure Monitoring 3 Null 4 5 454545 Asthma Education Provider ID is a foreign key in the encounter table but a primary key in the provider table. This key connects the encounter to the provider table. It can have duplicates as when the same provider sees different patients.

The encounter table connects visits to patients and provider tables.  

CREATE TABLE #Encounter ( [Patient ID] Int, [Provider ID] Int, [Diagnoses] char(50), [Treatment] char(50), [Date of Encounter] Date, [Encounter ID] int IDENTITY(1,1) PRIMARY KEY );   This code creates the encounter table with its related primary and foreign keys.

Connects to Patient Table CREATE TABLE #Encounter ( [Patient ID] Int, [Provider ID] Int, [Diagnoses] char(50), [Treatment] char(50), [Date of Encounter] Date, [Encounter ID] int IDENTITY(1,1) PRIMARY KEY ); The patient and encounter table share this field and that is why the two are connected.

Connects to Provider CREATE TABLE #Encounter ( [Patient ID] Int, [Provider ID] Int, [Diagnoses] char(50), [Treatment] char(50), [Date of Encounter] Date, [Encounter ID] int IDENTITY(1,1) PRIMARY KEY ); Connects to Provider The provider and the encounter table share this field.

This graph shows how the encounter table connects patients and the provider tables.

One to Many Every patient shows only once in the patient table but can show many times in the encounter table.

One to Many Similarly every provider shows only once in the provider table but can show many times in the encounter table.

INSERT INTO table_name (column1, column2, column3, INSERT INTO table_name (column1, column2, column3, ...) VALUES (value1, value2, value3, ...); Now that we have created the three tables and their relationships, we can start putting data into them. The syntax for inserting values into fields is provided on the web. The syntax is also shown in this slide.

INSERT INTO table_name (column1, column2, column3, INSERT INTO table_name (column1, column2, column3, ...) VALUES (value1, value2, value3, ...); Reserved Words The two words “insert into” are reserved words.

INSERT INTO table_name (column1, column2, column3, INSERT INTO table_name (column1, column2, column3, ...) VALUES (value1, value2, value3, ...); Existing Table The values must be inserted into an existing table with specified field or column names.

INSERT INTO table_name (column1, column2, column3, INSERT INTO table_name (column1, column2, column3, ...) VALUES (value1, value2, value3, ...); Reserved Word Values is also a reserved word. It instructs the servers that data will follow within the parenthesis.

INSERT INTO table_name (column1, column2, column3, INSERT INTO table_name (column1, column2, column3, ...) VALUES (value1, value2, value3, ...); Separated by Comma Data are inserted with each column values separated by comma.

Each Row of Data in Separate Parenthesis INSERT INTO #Patient ([First Name], [Last Name], [Street Number],[Street], [Zip Code], [Birth Date], [Email], [State], [Phone Number]) VALUES ('Farrokh', 'Alemi', Null, Null, 22101, '08/01/1954', 'Test2@gmu.edu', Null, '7039934226'), ('George', 'Smith', Null, Null, 22102, '09/09/1960','t@tes.com', Null,'7038884545'), ('Jill', 'Smith', Null, Null, 22103, '01/08/1989', 'test@test.com', Null,'7039934226'); Each Row of Data in Separate Parenthesis This provides how three row of data are inserted into the patient table. Each row of data is entered in separate parenthesis.

Null Values for When Data Is Missing INSERT INTO #Patient ([First Name], [Last Name], [Street Number],[Street], [Zip Code], [Birth Date], [Email], [State], [Phone Number]) VALUES ('Farrokh', 'Alemi', Null, Null, 22101, '08/01/1954', 'Test2@gmu.edu', Null, '7039934226'), ('George', 'Smith', Null, Null, 22102, '09/09/1960','t@tes.com', Null,'7038884545'), ('Jill', 'Smith', Null, Null, 22103, '01/08/1989', 'test@test.com', Null,'7039934226'); Null Values for When Data Is Missing Within each parenthesis the data are provided in order of listed columns. Did you notice that the street name, street number, and state were entered as null values? Note that null value specification is done without a quote. Inserting a blank is not the same as null value specification.

No Patient ID INSERT INTO #Patient ([First Name], [Last Name], [Street Number],[Street], [Zip Code], [Birth Date], [Email], [State], [Phone Number]) VALUES ('Farrokh', 'Alemi', Null, Null, 22101, '08/01/1954', 'Test2@gmu.edu', Null, '7039934226'), ('George', 'Smith', Null, Null, 22102, '09/09/1960','t@tes.com', Null,'7038884545'), ('Jill', 'Smith', Null, Null, 22103, '01/08/1989', 'test@test.com', Null,'7039934226'); Also note that patient ID was not entered. The software will assign a unique number for patient ID. It will automatically increment by 1 each time a new record is entered. We do not need to enter it.

Text Values are Entered in Quotes INSERT INTO #Patient ([First Name], [Last Name], [Street Number],[Street], [Zip Code], [Birth Date], [Email], [State], [Phone Number]) VALUES ('Farrokh', 'Alemi', Null, Null, 22101, '08/01/1954', 'Test2@gmu.edu', Null, '7039934226'), ('George', 'Smith', Null, Null, 22102, '09/09/1960','t@tes.com', Null,'7038884545'), ('Jill', 'Smith', Null, Null, 22103, '01/08/1989', 'test@test.com', Null,'7039934226'); Text Values are Entered in Quotes Finally, note that text fields are in quotes, dates are in quotes, but numbers and null values are not. Putting the null value in quotes will enter it as if was a text, which defeats the purpose.

Patient Provider Encounter Create Table Insert Values Provider and Encounter tables are created in a similar fashion by using the “Create Table” and “Insert Values” commands repeatedly.

Relational Database Once all three tables have been created then a relational database has been specified and Microsoft SAL Management Studio can be used to analyze the data across all three tables.

DROP TABLE table-name Now that we have shown you how to create a table, it is important to also learn how to delete it. The reserved words drop table accomplishes this goal. If the table does not exist, the command will create an error that you can ignore.

Create Table & Insert Values Create table and insert values can make new tables within SQL