Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 An Introduction to SQL. 2 Objectives  Understand the concepts and terminology associated with relational databases  Create and run SQL commands 

Similar presentations


Presentation on theme: "1 An Introduction to SQL. 2 Objectives  Understand the concepts and terminology associated with relational databases  Create and run SQL commands "— Presentation transcript:

1 1 An Introduction to SQL

2 2 Objectives  Understand the concepts and terminology associated with relational databases  Create and run SQL commands  Create tables using SQL  Identify and use data types to define the columns in SQL tables  Understand and use nulls  Add rows to tables

3 3 Getting Started with Oracle putty ubunix.buffalo.edu >setenv EDITOR nano >source /util/bin/coraenv >sqlplus

4 4 Introduction  Mid-1970s: SQL developed under the name SEQUEL at IBM by San Jose research facilities to be the data manipulation language for IBM’s prototype relational model DBMS, System R  1980: language renamed SQL to avoid confusion with an unrelated hardware product called SEQUEL  Currently: SQL used as the data manipulation language for IBM’s current relational DBMS, DB2  Most relational DBMSes use a version of SQL

5 5 Common Shorthand Representation  Write the table name and then, within parentheses, list all the columns (fields) in the table  SQL is not case sensitive  Type commands using uppercase or lowercase letters Exception: when inserting character values into a table, use the correct case

6 6 Shorthand of Premier Products Database  SALES_REP (SLSREP_NUMBER, LAST, FIRST, STREET, CITY, STATE, ZIP_CODE, TOTAL COMMISSION, COMMISSION_RATE)  CUSTOMER (CUSTOMER_NUMBER, LAST, FIRST, STREET, CITY, STATE, ZIP_CODE, BALANCE, CREDIT_LIMIT, SLSREP_NUMBER)  ORDERS (ORDER_NUMBER, PART_NUMBER, NUMBER_ORDERED, QUOTED_PRICE)  ORDER_LINE (ORDER_NUMBER, PART_NUMBER, NUMBER_ORDERED, QUOTED_PRICE)  PART (PART_NUMBER, PART_DESCRIPTION, UNITS_ON_HAND, ITEM_CLASS, WAREHOUSE_NUMBER, UNIT_PRICE)

7 7 Qualifying Names  To associate the correct table with the column name: Write both the table name and the column name, separated by a period CUSTOMER.SLSREP_NUMBER SALES_REP.SLSREP_NUMBER  This technique of including the table name with the column name is known as qualifying the names

8 8 SQL Commands  Commands are free format no rule says that a particular word must begin in a particular position on a line manner in which the command is written simply makes the command more readable  Press the Enter key at the end of each line and then continue typing the command on the next line  Indicate the end of a command line by typing a semicolon (required in Oracle, but it is not universal)

9 9 Database Creation  Describe the layout of each table to be contained in the database  The SQL command used to describe the layout of a table is CREATE TABLE followed by the name of the table to be created and the names and data types of the columns that comprise the table in parentheses  Data type indicates the type of data that the column can contain (for example, characters, numbers, or dates)

10 10 Data Types  Char or Varchar2  Date  Decimal  Integer  Smallint

11 11 Typical Column Naming Conventions  The name cannot be longer than 18 characters (in Oracle, names can be up to 30 characters in length)  The name must start with a letter  The name can contain letters, numbers, and underscores ( _ )  The name cannot contain spaces

12 12 Example 1  Describe the layout of the SALES_REP table to the DBMS  CREATE TABLE SALES_REP (SLSREP_NUMBER CHAR(2), LAST CHAR(10), FIRST CHAR (5), STREET CHAR(15), CITY CHAR(15), STATE CHAR(2), ZIP_CODE CHAR(5), TOTAL_COMMISSION DECIMAL(7,2), COMMISSION_RATE DECIMAL(3,2));

13 13 Editing  In Oracle, the most recent command entered is stored in the command buffer  Edit the command in the buffer by using the special editing commands

14 14 After Editing  Use the L command to list the codified SQL command, to verify that changes are correct  Type a semicolon after the command to run (execute) it immediately  Type RUN to display the command before it is executed  Type RUN followed by a slash (/) to execute the command without first displaying it

15 15 Dropping a Table  Use the DROP TABLE command to delete a table  The command DROP TABLE is followed by the name of the table you want to delete and a semicolon. DROP TABLE SALES_REP;  Note when a table is dropped, any data that you entered into the table is dropped

16 16 Nulls  Occasionally, when you enter a new row into a table or modify an existing row, the values for one or more columns are unknown or unavailable A customer who does not have a sales rep  This special value is called a null data value, or simply null.

17 17 Implementation of Nulls CREATE TABLE SALES_REP (SLSREP_NUMBER CHAR(2) NOT NULL, LAST CHAR(10) NOT NULL, FIRST CHAR (5) NOT NULL, STREET CHAR(15), CITY CHAR(15), STATE CHAR(2), ZIP_CODE CHAR(5), TOTAL_COMMISSION DECIMAL(7,2), COMMISSION_RATE DECIMAL(3,2) );

18 18 Loading a Table with Data  Add the necessary rows to each table using the INSERT command  When adding rows to character (CHAR) columns, make sure to enclose the values in single quotation marks (for example, ‘Jones’)

19 19 INSERT Command with Nulls  To enter a null value into a table, use a special format of the INSERT command  In this special format, identify the names of the columns that will accept non-null values, and then list only these non-null values after the VALUES command

20 20 SELECT/UPDATE/DELETE Command  View data in the table to make sure that it is entered correctly by using the SQL SELECT command  After reviewing the data in the table changes may have to be made to the value in a column  To delete a record, use the DELETE command

21 21.SQL files  Use an editor to create a file containing the CREATE TABLE and INSERT commands to save from having to retype commands  To create a file in Oracle, type EDIT followed by the name of the file to create  Oracle assigns the file the extension.SQL automatically  Must have EDITOR environment variable set in UNIX for this to work

22 22 cre_cust.SQL  To run the cre_cust.SQL file Save the file Close the editor Type @cre_cust (and full path if stored in another folder) After creating the table, another file could be created containing all the necessary INSERT commands to add the necessary records to the table  Each command in the file must end with a semicolon

23 23 Describing a Table  Use the DESCRIBE command to describe the layout of a table  The DESCRIBE command lists all the columns in the table and their corresponding data types


Download ppt "1 An Introduction to SQL. 2 Objectives  Understand the concepts and terminology associated with relational databases  Create and run SQL commands "

Similar presentations


Ads by Google