Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to Information and Computer Science

Similar presentations


Presentation on theme: "Introduction to Information and Computer Science"— Presentation transcript:

1 Introduction to Information and Computer Science
Welcome to Introduction to Information and Computer Science: Databases and SQL. This is Lecture (c). The component, Introduction to Information and Computer Science, is a basic overview of computer architecture; data organization, representation and structure; structure of programming languages; networking and data communication. It also includes basic terminology of computing. Databases and SQL Lecture c This material (Comp4_Unit6c) was developed by Oregon Health and Science University, funded by the Department of Health and Human Services, Office of the National Coordinator for Health Information Technology under Award Number IU24OC

2 Databases and SQL Learning Objectives
Define and describe the purpose of databases (Lecture a) Define a relational database (Lecture a) Describe data modeling and normalization (Lecture b) Describe the structured query language (SQL) (Lecture c) Define the basic data operations for relational databases and how to implement them in SQL (Lecture c) Design a simple relational database and create corresponding SQL commands (Lecture c) Examine the structure of a healthcare database component (Lecture d) The objectives for Databases and SQL are to: Define and describe the purpose of databases Define a relational database Describe data modeling and normalization Describe the structured query language (SQL) Define the basic data operations for relational databases and how to implement them in SQL Design a simple relational database and create corresponding SQL commands Examine the structure of a healthcare database component Health IT Workforce Curriculum Version 3.0/Spring 2012 Introduction to Information and Computer Science Databases and SQL Lecture c

3 SQL Used to manage and access database information
ANSI, ISO and IEC standard DBMS have custom extensions! Extensive language We’ll look at a few basic commands This lecture covers the objectives relating to SQL. SQL [see kwel], for Structured Query Language, is the language commonly used to manage and access a relational database. The language is an international American National Standards Institute (ANSI) [an see], International Organization for Standardization (ISO) [eye so] and International Electrotechnical Commission (IEC) [I E C] standard. Nevertheless, each database management system (DBMS) may interpret the standard differently or offer proprietary extensions to the language; there Is no guarantee that one set of SQL commands will work exactly the same way on another vendor’s system. Portability is difficult from one DBMS to another; however, most common commands will operate in the same fashion. While the following examples show the use of a command line interface to access the database directly through SQL commands, this is certainly not the only way to access a database. Databases can also be accessed through graphical user interfaces as well programs that issue SQL commands through defined system interfaces. The following slides will explore some basic SQL commands. Health IT Workforce Curriculum Version 3.0/Spring 2012 Introduction to Information and Computer Science Databases and SQL Lecture c

4 Example Make a space for the tables by creating the database
create database <name> sql> create database contacts To remove: drop database <name> To create a contacts database of individuals, their companies and the associated addresses of those companies using an SQL command, the command “create database” with the database name specified is used. It is always helpful to use a meaningful name, so for this example, the database will be called “contacts.” To create the database structure, the command “create database contacts” is issued. In order to delete a database, the command, “drop database”, with the database name specified, is used. Health IT Workforce Curriculum Version 3.0/Spring 2012 Introduction to Information and Computer Science Databases and SQL Lecture c

5 Create the Tables create table <name> (<column information>); Column information consists of column names and data types Tables also have extra information To remove: drop table <name> Once the database is created, then data tables can be created. These are also created with a SQL command where the table name and information about the columns contained in the database table are specified. The column information contains the name of the column as well as the type of information contained inside the specific column. Additional information about the table may also be specified. SQL has an extensive set of options available; n the following slides several examples of how to use the create command to create the tables will be explored. Just as with the database itself, a table may need to be deleted when it no longer serves its purpose. To do this, a “drop table” command with appropriate options is used. Health IT Workforce Curriculum Version 3.0/Spring 2012 Introduction to Information and Computer Science Databases and SQL Lecture c

6 SQL Basic Data Types Data Type Description integer or int
Whole numbers float Floating point number date Date time Time char (length) Fixed number of characters varchar (length) Variable number of characters The columns in a table have both an identifying name and an associated data type. To the system, this information is in ones and zeroes, so the designer needs to tell the database what type of information is being stored so that it knows how to interpret it. And while SQL is a standard language, the data types, formats and range limit of values may be vendor-specific; however, these examples are some of the more common data types. An integer type is used to store whole numbers with no decimal points. To store a number with a decimal point, it needs to be identified it as a float type. If it is important to record information about when an event occurred, then both date and time data types will be indicated. Finally, there are two formats for storing textual information - char [care] and varchar [var-car]. A char is used to store strings of a known length, such as a zip code. A varchar data type is used to store text strings where the actual length is variable, such as a person’s name. 6.14 Table: SQL Basic Data Types (PD-US, 2011). Health IT Workforce Curriculum Version 3.0/Spring 2012 Introduction to Information and Computer Science Databases and SQL Lecture c

7 Create Company Table sql> create table company (id integer auto_increment, -> name varchar(50), -> address varchar(50), -> city varchar(50), -> state char(2), -> primary key(id)); This slide shows the steps to create a table for company information. It is important to rremember that the company name, address, and city are of variable length, while the state will be stored as a fixed two-character abbreviation. The row will need to be uniquely identified; “id” [eye-dee] will be used as an identifier. Finally, each column must have a meaningful name, preferably one that has an easily understandable meaning to anyone accessing the database, such as “name”. Notice that there are a few more options used in the command on this slide - the “id” column is identified as having an integer type, as well as an auto_increment option which instructs the system to keeps track of the value, incrementing it every time a new row is added. The name, address and city are represented as variable text strings of up to 50 characters; however, a larger value may need to be specified ifnames or addresses longer than 50 characters are to be stored. It was determined that the two-letter abbreviations would be used for the state names, as indicated by the “char” [care] field value of 2. And finally, the primary (or unique) key for the company table is the “id” column. Health IT Workforce Curriculum Version 3.0/Spring 2012 Introduction to Information and Computer Science Databases and SQL Lecture c

8 Create person table sql> create table person(id integer auto_increment, -> first_name varchar(50), -> last_name varchar(50), -> company_id integer, -> primary key(id), -> foreign key(company_id) references company(id)); This table shows the structure for the person table. The person’s first name and last name are stored separately to enable sorting by last name only, and each person’s name is associated with a company. As in the company table, "id" is used for the primary key in this table. Once names have been determined for the columns, the “create table” command is used to create the table as shown. This table is very similar to the “create company table” on the previous screen with the addition of the foreign key (company_ id [eye-dee]), which ties the person to the appropriate company. The addition of “references company (id) [eye-dee]” to this command tells the database system that the “company id” column in the person table is tied to the “primary key (id)” column in the company database, ensuring that the company id values are consistent between the company and person tables. Also note that it is not necessary to use the same name for the foreign key in both tables; in this example, it is named "company_id" in the person table, and "id" in the company table. Health IT Workforce Curriculum Version 3.0/Spring 2012 Introduction to Information and Computer Science Databases and SQL Lecture c

9 View Tables 6.15 Figure: SQL command and output (PD-US, 2011).
Once the tables are created, then the command line interface to issue the “show tables” command to confirm that the company and person tables have been correctly structured in the contacts database. 6.15 Figure: SQL command and output (PD-US, 2011). Health IT Workforce Curriculum Version 3.0/Spring 2012 Introduction to Information and Computer Science Databases and SQL Lecture c

10 View Table Columns 6.16 Figure: View table columns (PD-US, 2011).
To verify the correct columns and data types in the tables, a “show columns from” command is issued for each table. Along with additional detailed information, this will confirm that the individual tables have the expected names and data types, and that the primary and foreign keys (abbreviated in this table as PRI and MUL) have been correctly identified. 6.16 Figure: View table columns (PD-US, 2011). Health IT Workforce Curriculum Version 3.0/Spring 2012 Introduction to Information and Computer Science Databases and SQL Lecture c

11 Same Basic Operations Add an entry Retrieve an entry Delete an entry
Modify an entry There are four basic operations in an information system - adding, retrieving, deleting and modifying information. SQL statements can be used to change the contents of database information, providing more ease and organization in the maintenance of large amounts of data. Health IT Workforce Curriculum Version 3.0/Spring 2012 Introduction to Information and Computer Science Databases and SQL Lecture c

12 Add an Entry Insert into <table> (columns) values (values);
First add company entries: sql> insert into company (name, address, city, state) -> values ('Community Hospital, Inc.', '1312 Main', 'Portland', 'OR'); -> values ('Oakland Providers LLC', '14 12th St.', 'Oakland', 'CA'); The first operation, add, is an insert command in SQL. The table where information is to be inserted and the specific values for the row must be identified. This slide shows the steps to add company entries. The value for each entry in the values field lines up with the equivalent entry in the column field. For example, “city” is specified as the third column in the first insert statement and “Portland” is being used in the third column in the values field. It is imperative that the order of the values matches the order of the columns to prevent inserting wrong values into the columns. Note that a value is not specified for the id column; this was set to be auto increment when the table was created, so the system automatically enters the value. Also notice that the text strings are surrounded by single quotation marks – this is how the SQL command line interpreter recognizes the beginning and end of text. Health IT Workforce Curriculum Version 3.0/Spring 2012 Introduction to Information and Computer Science Databases and SQL Lecture c

13 Add Persons Now add people and their associated companies:
sql> insert into person (first_name, last_name, company_id) -> values ('Bill', 'Robeson', 1); -> values ('Walter', 'Schmidt', 2); -> values ('Mary', 'Stahl', 2); -> values ('Albert', 'Brookings', 1); -> values ('Catherine', 'David', 2); Now that the companies are entered into the database, the individuals associated with the companies can be added. For each entry, the first and last names need to be specified, again using single quotes around the text strings, and the company key. Note that this is now much simpler as only the company key associated with the person needs to be entered, rather than all of the company information. The next slide will describe how the values of 1 and 2 for the company ids are obtained. Health IT Workforce Curriculum Version 3.0/Spring 2012 Introduction to Information and Computer Science Databases and SQL Lecture c

14 Retrieve an Entry 6.17 Figure: Retrieve an entry (PD-US).
Retrieval, or selecting specific information to view, is another basic operation.. In this slide, the SQL statement “sql> select * from company” provides all of data for every column in the table. The asterisk instructs the system to show all information. Notice that each company is numbered in the id column – this happens automatically when the auto_increment option is utilized as illustrated in an earlier slide. 6.17 Figure: Retrieve an entry (PD-US). Health IT Workforce Curriculum Version 3.0/Spring 2012 Introduction to Information and Computer Science Databases and SQL Lecture c

15 Add Sorting 6.18 Figure: Add sorting (PD-US, 2011).
When using a file system, it can be a challenge to sort contact information by last name. By adding “order by” to the SQL statement, the system will display the information sorted by last name while maintaining the original id and company_id values. 6.18 Figure: Add sorting (PD-US, 2011). Health IT Workforce Curriculum Version 3.0/Spring 2012 Introduction to Information and Computer Science Databases and SQL Lecture c

16 Add Selectivity 6.19 Figure: Add selectivity (PD-US, 2011).
A search, however, may not require that all data in a record be returned. Instead of an asterisk to request everything, the names of the desired columns can be indicated in the SQL statement. This slide shows retrieval of only the first and last names of the table records. To only retrieve the first and last names of people who are associated with a specific company – company id 1, for example - a “where company_id = 1” clause is added to the statement. Since “id 1” in this table is Community Hospital, the resulting table will show that Bill Robeson and Albert Brookings are associated with that organization. 6.19 Figure: Add selectivity (PD-US, 2011). Health IT Workforce Curriculum Version 3.0/Spring 2012 Introduction to Information and Computer Science Databases and SQL Lecture c

17 Retrieve from Multiple Tables
Previous slides have demonstrated how to retrieve information from just one table; this slide shows how to retrieve information from both tables at once. This requires a more complex SQL statement that joins the tables and specifies how the tables are related. When the two tables were created, primary keys were identified for both tables and the company id column in the person table connected to the id column in the company table. The SQL command, “select * from person join company on person dot company underscore id = company.id”, connects the two tables and provides complete information on all of the contacts and the companies. 6.20 Figure: Retrieve from multiple tables (PD-US, 2011). Health IT Workforce Curriculum Version 3.0/Spring 2012 Introduction to Information and Computer Science Databases and SQL Lecture c

18 Create a Complex SQL Statement
This slide demonstrates an SQL statement that combines several actions - retrieval of a person’s name, the name of their associated company, all sorted by last name. The SQL statement shown here selects the columns to display, indicates that information is to be collected from multiple tables, and specifies the sort order. SQL provides a large amount of flexibility in examining the data stored in a database. While this lecture has explored only a few of the options for retrieving data; there are many more SQL retrieval options that provide the user with greater control of how data is viewed in a database. 6.21 Figure: Create a Complex SQL Statement (PD-US, 2011). Health IT Workforce Curriculum Version 3.0/Spring 2012 Introduction to Information and Computer Science Databases and SQL Lecture c

19 Delete an Entry delete from <table> where <constraints>;
Remove Mary from contact list: sql> delete from person where first_name='Mary' and last_name='Stahl'; 1 row affected The third basic function is deleting data, as shown on this slide. The SQL delete statement identifies the table where data will be deleted and the specific entry that is being deleted. Technically, the “where” constraint is optional, but without it all information in the table will be deleted, so a very specific SQL statement will ensure that only the specified data will be removed. There are many ways to do this; just “Mary” could be specified, but in case there are multiple Marys, it is best to use both the first and last names. The “id” column could have been specified instead, once Mary’s id value was retrieved. Because it is the unique key into the table, this would guarantee that the correct row was removed. . Health IT Workforce Curriculum Version 3.0/Spring 2012 Introduction to Information and Computer Science Databases and SQL Lecture c

20 Modify an Entry update <table> set <column>=<data> where <constraints>; The final basic function is modifying data. SQL uses an update statement for this, where the table is identified, the columns with the data that require change are identified, the new data is identified, and the rows that are being modified are identified. Health IT Workforce Curriculum Version 3.0/Spring 2012 Introduction to Information and Computer Science Databases and SQL Lecture c

21 New Company Name 6.22 Figure: Modify company name (PD-US, 2011).
This slide shows a two-step process to modify a company name from “Community Hospital, Inc.” to “Community General.” First, the unique row id will be identified using a select statement that will retrieve information on all the companies in the database. This search shows that Community Hospital, Inc. has a row id of 1, which can now be referenced in the SQL command to change the data for the hospital name. The update statement specifies company as the table, sets the contents of the name column to “Community General”, and specifies that the modification is to take place in the row with the id for the hospital. Notice again the use of single quotes to identify a text string. It is also important to know that there is no limit on the number of rows that can be changed in a single update statement. As with the delete statement, it is very important to correctly identify the rows toned to be changed by using the “where” clause. 6.22 Figure: Modify company name (PD-US, 2011). Health IT Workforce Curriculum Version 3.0/Spring 2012 Introduction to Information and Computer Science Databases and SQL Lecture c

22 New Company Name 6.23 Figure: New company name (PD-US, 2011).
Once the company name has been changed in the appropriate row in the company table, the change to the row information must be verified by issuing the SQL select statement and looking at the command output to ensure that the name change from “Community Hospital, Inc.” to “Community General” was successful. 6.23 Figure: New company name (PD-US, 2011). Health IT Workforce Curriculum Version 3.0/Spring 2012 Introduction to Information and Computer Science Databases and SQL Lecture c

23 Verify Again 6.24 Figure: Verify again (PD-US, 2011).
A final query, that pulls data from both the person and company tables, as shown on this slide, can also verify that the information has been successfully modified. 6.24 Figure: Verify again (PD-US, 2011). Health IT Workforce Curriculum Version 3.0/Spring 2012 Introduction to Information and Computer Science Databases and SQL Lecture c

24 Databases and SQL Summary – Lecture c
SQL is a language for creating, accessing and updating databases Create tables Insert data Update data Retrieve (select) data Delete data This concludes Lecture (c) of Databases and SQL. In summary, this lecture demonstrated how SQL is used for creating, accessing and updating databases. Using a contacts database as an example, SQL statements were used to create tables, insert data, update data, retrieve or select data and delete data. Health IT Workforce Curriculum Version 3.0/Spring 2012 Introduction to Information and Computer Science Databases and SQL Lecture c

25 Databases and SQL References – Lecture c
Chen, P. P.-S. (1976). The Entity-Relationship Model - Toward a Unified View of Data. ACM Transactions on Database Systems, 1(1). International Organization for Standardization. (2008). Information technology -- Database languages -- SQL (No. ISO/IEC 9075-(1-4,9-11,13,14)). Kent, W. (1983). A simple guide to five normal forms in relational database theory. Communications of the ACM, 26(2). Charts, Tables, Figures: 6.14 Table: SQL Basic Data Types (PD-US, 2011). 6.15 Figure: View tables (PD-US, 2011). 6.16 Figure: View table columns (PD-US, 2011). 6.17 Figure: Retrieve an entry (PD-US, 2011). 6.18 Figure: Add sorting (PD-US, 2011). 6.19 Figure: Add selectivity (PD-US, 2011). 6.20 Figure: Retrieve from multiple tables (PD-US, 2011). 6.21 Figure: Create a Complex SQL Statement (PD-US, 2011). 6.22 Figure: Modify company name (PD-US, 2011). 6.23 Figure: New company name (PD-US, 2011). 6.24 Figure: Verify again (PD-US, 2011). References slide. No Audio. Health IT Workforce Curriculum Version 3.0/Spring 2012 Introduction to Information and Computer Science Databases and SQL Lecture c


Download ppt "Introduction to Information and Computer Science"

Similar presentations


Ads by Google