Presentation is loading. Please wait.

Presentation is loading. Please wait.

Creating Tables & Inserting Values Using SQL

Similar presentations


Presentation on theme: "Creating Tables & Inserting Values Using SQL"— Presentation transcript:

1 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.

2 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.

3 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.

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

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

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

7 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.

8 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.

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

10 Telephone of the Patient
Patient Table String String String String First Name Last  Name Zip Code City State Date of Birth Telephone of the Patient Larry Kim 22101 Mclean DC 08-Jan-54 George Smith 22102 McLean Virginia 09-Sep-60 (703) Jill VA 01-Aug-89 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.

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

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

13 Telephone of the Patient
Patient Table First Name Last  Name Zip Code City State Date of Birth Telephone of the Patient Larry Kim 22101 Mclean DC 08-Jan-54 George Smith 22102 McLean Virginia 09-Sep-60 (703) Jill VA 01-Aug-89 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.

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

15 Telephone of the Patient
Patient Table First Name Last  Name Zip Code City State Date of Birth Telephone of the Patient Larry Kim 22101 Mclean DC 08-Jan-54 George Smith 22102 McLean Virginia 09-Sep-60 (703) Jill VA 01-Aug-89 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.

16 Telephone of the Patient Remove Variability in Data Entry
Patient Table First Name Last  Name Zip Code City State Date of Birth Telephone of the Patient Larry Kim 22101 Mclean DC 08-Jan-54 George Smith 22102 McLean Virginia 09-Sep-60 (703) Jill VA 01-Aug-89 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.

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

18 In [ ] CREATE TABLE #Patient ( [First Name] char(20),
[Last Name] char(50), [Street Number] Int, [Street] Text, [Zip Code] Int, [Birth Date] Date, [ ] 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.

19 # CREATE TABLE #Patient ( [First Name] char(20), [Last Name] char(50),
[Street Number] Int, [Street] Text, [Zip Code] Int, [Birth Date] Date, [ ] 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.

20 Primary Key CREATE TABLE #Patient ( [First Name] char(20),
[Last Name] char(50), [Street Number] Int, [Street] Text, [Zip Code] Int, [Birth Date] Date, [ ] 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.

21 Provider Table First Name Last Name Board Certified Email Telephone
Employee ID Jim Jones Yes 452310 Jill Smith No 454545 George John 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 in text of size 75.

22 Provider Table Primary Key First Name Last Name Board Certified Email
Telephone Employee ID Jim Jones Yes 452310 Jill Smith No 454545 George John 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 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.

23 Provider Table First Name Last Name Board Certified Email Telephone
Employee ID Jim Jones Yes 452310 Jill Smith No 454545 George John 456734 This slide shows the first three providers;

24 Provider Table First Name Last Name Board Certified Email Telephone
Employee ID Jim Jones Yes 452310 Jill Smith No 454545 George John 456734 note that one of the patients, Jill Smith, previously described in patient Table is also a provider.

25 CREATE TABLE #Provider ( [First Name] char(20), [Last Name] char(50),
[Board Certified] bit, [Date of Hire] Date, [Phone] Text, [ ] 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.

26 CREATE TABLE #Provider ( [First Name] char(20), [Last Name] char(50),
[Board Certified] bit, [Date of Hire] Date, [Phone] Text, [ ] 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.

27 CREATE TABLE #Provider ( [First Name] char(20), [Last Name] char(50),
[Board Certified] bit, [Date of Hire] Date, [Phone] Text, [ ] 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:

28 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. 

29 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. 

30 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.

31 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.

32 The encounter table connects visits to patients and provider tables.  

33 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.

34 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.

35 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.

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

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

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

39 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.

40 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.

41 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.

42 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.

43 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.

44 Each Row of Data in Separate Parenthesis
INSERT INTO #Patient ([First Name], [Last Name], [Street Number],[Street], [Zip Code], [Birth Date], [ ], [State], [Phone Number]) VALUES ('Farrokh', 'Alemi', Null, Null, 22101, '08/01/1954', Null, ' '), ('George', 'Smith', Null, Null, 22102, Null,' '), ('Jill', 'Smith', Null, Null, 22103, '01/08/1989', Null,' '); 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.

45 Null Values for When Data Is Missing
INSERT INTO #Patient ([First Name], [Last Name], [Street Number],[Street], [Zip Code], [Birth Date], [ ], [State], [Phone Number]) VALUES ('Farrokh', 'Alemi', Null, Null, 22101, '08/01/1954', Null, ' '), ('George', 'Smith', Null, Null, 22102, Null,' '), ('Jill', 'Smith', Null, Null, 22103, '01/08/1989', Null,' '); 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.

46 No Patient ID INSERT INTO #Patient ([First Name], [Last Name], [Street Number],[Street], [Zip Code], [Birth Date], [ ], [State], [Phone Number]) VALUES ('Farrokh', 'Alemi', Null, Null, 22101, '08/01/1954', Null, ' '), ('George', 'Smith', Null, Null, 22102, Null,' '), ('Jill', 'Smith', Null, Null, 22103, '01/08/1989', Null,' '); 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.

47 Text Values are Entered in Quotes
INSERT INTO #Patient ([First Name], [Last Name], [Street Number],[Street], [Zip Code], [Birth Date], [ ], [State], [Phone Number]) VALUES ('Farrokh', 'Alemi', Null, Null, 22101, '08/01/1954', Null, ' '), ('George', 'Smith', Null, Null, 22102, Null,' '), ('Jill', 'Smith', Null, Null, 22103, '01/08/1989', Null,' '); 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.

48 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.

49 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.

50 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.

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


Download ppt "Creating Tables & Inserting Values Using SQL"

Similar presentations


Ads by Google