Presentation is loading. Please wait.

Presentation is loading. Please wait.

CIT 214 Introduction to Database Management

Similar presentations


Presentation on theme: "CIT 214 Introduction to Database Management"— Presentation transcript:

1 CIT 214 Introduction to Database Management
Structured Query Language: Introductions, Data Definitions and Data Administration

2 Objectives 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 of data to tables View table data Understand and use Integrity Constraints Understand and use System Commands

3 SQL Data in relational database tables are inserted, retrieved, and modified using commands called queries. Queries are performed using high-level query languages that use standard English commands such as INSERT, UPDATE, DELETE, and SELECT.

4 If watching archived, click on the Notes tab for additional info.
Oracle Environment Enterprise Manager Security Manager Storage Manager Instance Manager Oracle Web Application Server SQL*Plus  Query Builder PL/SQL & Procedure Builder Developer Form Builder Report Builder Graphic Builder SQL *Plus: For creating and testing command-line SQL Queries used to create, update and delete database tables, views and sequences. Query Builder: For creating Queries using QBE PL/SQL and Procedure Builder: For creating procedural programs that process database data. Developer: For developing database applications. Developer consist of the following tools: Form Builder: For creating graphical forms and menus for user applications Report Builder: For creating reports for displaying and printing data Graphic Builder: For creating graphics charts based on database data. Enterprise Manager: For managing and tuning the database. Enterprise Manager uses the following tools: Security Manager: For creating and managing user accounts Storage Manager: For creating and managing tablespaces Instance Manager: For starting, shutting down, and tuning the database Oracle Web Application Server: For creating a World Wide Web site that allows users to access Oracle databases and create dynamic Web pages that serve as a database interface. If watching archived, click on the Notes tab for additional info.

5 SQL Commands SQL commands are free format
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

6 Naming Conventions Names cannot be longer than 30 Characters in length. The names must start with a letter. The names can contain letters, numbers, and underscores. The names cannot contain spaces

7 If watching archived, click on the Notes tab for additional info.
Data Types Character Data Type VARCHAR2  CHAR  Number Data Type Integer  Fixed-Point (Precision, Scale)  Floating-Point  Date Data Type LOB Data Type Data Types are used for two primary reasons. First, assigning a data type provides a means for error checking. Secondly, Data Types also cause storage space to be used more efficiently by optimizing the way specific types of data are stored. VARCHAR2: Variable length data accepted. Maximum of 4,000 characters. Accepts variable length data up to the specified maximum. If entered data are smaller that the specified size, only the data entered is stored. CHAR: Holds fixed length character data up to a maximum size of 255 characters. If the value entered is less than the specified field size, then trailing spaces are added. If no field size is specified, the default size value is one character. NCHAR: Similar to CHAR data type, except that it supports 16-digit (2 byte) binary characters codes. Long: Used to store large amounts of variable length character data up to 2 gigabytes. You can include only one Long field in a table. Integer: Whole number with no digits to the right of the decimal point. Number(5) Contains a specific number of decimal places. Number (5,2) 5 digits with 2 to the right of the decimal point. The decimal point is not included in the precision value. (Precision = total number of digits and Scale = the number of digits to the right of the decimal point) Floating-Point: Variable number of decimal places. The decimal point can appear anywhere, from before the first digit, to after the last digit or not at all. Number (2.5, 22.44, .5, 9) Date: Stores dates from January 1, 4712 BC to December 31, 4712 AD. The data type can store the century, year, month, day, hour, minute and second. Default format is DD-MON-YY. The default time format is HH:MI:SS A.M. BLOB: Binary LOB CLOB: Character LOB BFILE: Binary File-Stores a reference to a binary file located outside of the database NCLOB: Character LOB that supports 2-byte character codes. If watching archived, click on the Notes tab for additional info.

8 Data Type Examples Varchar2(10) Entering the word Bill
Storing the word Bill = Bill (The Space is Reallocated from 10 to 4 characters) Char(10) Entering the word Bill Storing the word Bill = Bill###### where # = Added Spaces (The Space is not reallocated, it takes up all 10 spaces) Number(Precision, Scale) Precision = Total Number of Digits Scale = Total Number of Digits to the right of the decimal place. Examples: Integer = Number(5) - Biggest number would be Fixed point = Number(5,2) - Biggest number would be Floating Point = Number - Could be 99.9 or or 9.999

9 SQL Editing Commands LIST: Run the command currently in the buffer (L) <line number>: Changes the current line and displays the number. APPEND <text>: Add text at the end of the current line (A) CHANGE /old text/new text : Change text in current line (C) DELETE : Delete the current line (DEL) INSERT <text>: Insert line following current line (I) LIST <line number>: Shows the indicated line number text. (L) / : Executes the contents of the edit buffer Use these commands at your own risk, because they are like the old DOS Commands – Very cryptic and difficult to use.

10 SQL Alternative Editing
Easier to create all of your commands in a text editor such as Notepad and then copy and paste them into SQL Plus. Allows you to save your work easier and if your commands do not work properly in SQL, then you can easily edit them in Notepad and then copy them back into SQL Plus.

11 Creating a Table Describe the layout of each table in the database
Use CREATE TABLE command TABLE is followed by the table name Follow this with the names and data types of the columns in the table Data types define type and size of data

12 Create Table Command CREATE TABLE LOCATION (locid Number(5) PRIMARY KEY, bldg_code Varchar2(10) , room Varchar2(6) , capacity Number(5) );

13 If watching archived, click on the Notes tab for additional info.
Integrity Rules in SQL Integrity Constraints are used to apply business rules for the database tables. The constraints available in SQL are Foreign Key, Not Null, Unique, Check. When applying the various Integrity Constraints it is advisable to add the “Constraint” Clause for documentation and debugging purposes. The various type of business rules that may need to be applied could be as follows: PRIMARY KEY - Each record in a table needs to be unique, so we would set up a Primary Key for each field. FOREIGN KEY - A given record in one table needs to be related to other records in another table, so we would set up the foreign key field. NOT NULL – There may be some fields in a given table that are required to have data entered in them, so we would set up as not null. UNIQUE – There may be a field that is not the primary key that we want to be unique and not repeat. CHECK – There could be a given field that we wanted to only hold certain data values, so the check constraint would be applied. If watching archived, click on the Notes tab for additional info.

14 If watching archived, click on the Notes tab for additional info.
Integrity Rules in SQL Constraints can be defined in two ways  The constraints can be specified immediately after the column definition. This is called column-level definition.  The constraints can be specified after all the columns are defined. This is called table-level definition. The Chapter on Database Administration focuses on table-level definitions, but we will focus on column-level definitions in this lecture. When applying the various Integrity Constraints it is advisable to add the “Constraint” Clause for documentation and debugging purposes. The various type of business rules that may need to be applied could be as follows: PRIMARY KEY - Each record in a table needs to be unique, so we would set up a Primary Key for each field. FOREIGN KEY - A given record in one table needs to be related to other records in another table, so we would set up the foreign key field. NOT NULL – There may be some fields in a given table that are required to have data entered in them, so we would set up as not null. UNIQUE – There may be a field that is not the primary key that we want to be unique and not repeat. CHECK – There could be a given field that we wanted to only hold certain data values, so the check constraint would be applied. If watching archived, click on the Notes tab for additional info.

15 General Integrity Constraints (Syntax)
The <Constraint Name> is arbitrary, but we will adhere to the following naming convention when possible. Syntax: TableName_ColumnName_ConstraintID The Constraint IDs are as follows: Primary Key = PK Foreign Key = FK Not Null = NN Legal Values (Check)= CK Unique = UN

16 Primary Key Integrity Constraint
This constraint defines a column or combination of columns which uniquely identifies each row in the table. CREATE TABLE employee ( id number(5) CONSTRAINT employee_id_pk PRIMARY KEY, name char(20), dept char(10), age number(2), salary number(10), location char(10) ); Primary Key Integrity Constraint

17 Foreign Key Integrity Constraint
This constraint identifies any column referencing the PRIMARY KEY in another table. It establishes a relationship between two columns in the same table or between different tables. For a column to be defined as a Foreign Key, it should be defined as a Primary Key in the table which it is referring.

18 Foreign Key Integrity Constraint
Main Table CREATE TABLE product ( product_id number(5) CONSTRAINT product_pd_id_pk PRIMARY KEY, product_name char(20), supplier_name char(20), unit_price number(10)); Secondary Table CREATE TABLE items ( order_id number(5) CONSTRAINT items_od_id_pk PRIMARY KEY, product_id number(5) CONSTRAINT items_pd_id_fk REFERENCES, product(product_id), product_name char(20), supplier_name char(20), unit_price number(10)); [CONSTRAINT constraint_name] REFERENCES Referenced_Table_name(column_name) First the Primary Table “product” is defined and created. Then the Secondary Table “items” is defined and created. This is a 1 to many relationship. One product can be related to one or many items. Foreign Key Integrity Constraint If watching archived, click on the Notes tab for additional info.

19 Not Null Integrity Constraint
This constraint ensures all rows in the table contain a definite value for the column which is specified as not null. This means a null value is not allowed in that field. CREATE TABLE employee ( id number(5), name char(20) CONSTRAINT employee_name_nn Not Null, dept char(10), age number(2), salary number(10), location char(10) ); Not Null Integrity Constraint

20 Unique Key Integrity Constraint
This constraint ensures that a column or a group of columns in each row have a distinct value. The column(s) can have null values but the values cannot be duplicated. CREATE TABLE employee ( id number(5), name char(20), dept char(10), age number(2), salary number(10), location char(10) CONSTRAINT employee_location_un Unique ); Unique Key Integrity Constraint

21 Check Integrity Constraint
This constraint defines a business rule on a column. All the rows must satisfy this rule. The constraint can be applied for a single column or a group of columns. CREATE TABLE employee ( id number(5), name char(20), dept char(10), age number(2), gender char(1) CONSTRAINT emp_gender_ck Check (gender in (‘M’, ‘F’)), salary number(10), location char(10)); Check Integrity Constraint

22 If watching archived, click on the Notes tab for additional info.
Create Table Command Illustrating the Use Of Multiple Integrity Constraint CREATE TABLE employee ( id number(5) CONSTRAINT employee_id_pk PRIMARY KEY, name char(20) CONSTRAINT employee_name_nn Not Null, dept char(10), age number(2), gender char(1) CONSTRAINT employee_gender_ck Check (gender in (‘M’, ‘F’)), salary number(10), location char(10) CONSTRAINT employee_location_un Unique ); Note: This table does not have a foreign key constraint, because there are not any fields in the table that would be the foreign key of another table. However, you could have other tables where you could have all of the integrity constraints defined or some of them. At bare minimum, you will always have to define a primary key for each table. If watching archived, click on the Notes tab for additional info.

23 Adding Rows to a Table Once tables are created in a database, data can be loaded into them by using the INSERT command The INSERT command adds rows to a table To use this command: Type INSERT INTO followed by the name of the table into which data is being added Type the VALUES command followed by the specific values to be inserted in parentheses

24 The INSERT Command with Nulls
To enter a null value into a table, a special format of the INSERT command must be used Identify the names of the columns that will accept non-null values, and then list only these non-null values after the VALUES command

25 If watching archived, click on the Notes tab for additional info.
The Insert Command INSERT INTO Location VALUES (53, ‘BUS’, ‘424’, 1); INSERT INTO Faculty (fid, flname, ffname, fmi, locid) VALUES (1, ‘Cox’, ‘Kim’, ‘J’, 53); INSERT INTO Student VALUES (101, ‘Umato’, ‘Brian’, ‘D’, ‘454 St. John’’s Street’, ‘Eau Claire’, ‘WI’, ‘54702’, ‘ ’, ‘SR’); Adds data to every field in the Location Table. Adds data only to the specified fields in the Faculty Table. If there are other fields in the Faculty Table, then they will store null values. When wanting to add data that has an apostrophe, you must enter two single quotes. This tells the system that you are not ending the string but wanting to actual store an apostrophe. If you put the double quote, then it will actually store a double quote. If watching archived, click on the Notes tab for additional info.

26 Viewing Table Data Use SELECT command
Can display all the rows and columns in a table SELECT * FROM followed by the name of the table and end with a semicolon Select * From Employee;

27 System Catalog Information about tables in the database is kept in the system catalog or the data dictionary The system catalog is a relational database Information can be retrieved by using the same types of queries which are used to retrieve data in a relational database The DBMS updates the system catalog automatically Users should not use SQL queries to update the catalog directly because this might produce inconsistent information

28 If watching archived, click on the Notes tab for additional info.
Constraint Names List all Constraints that you set up in your tables: Example: Select CONSTRAINT_NAME From USER_CONSTRAINTS Where TABLE_NAME = ‘CUSTOMER’; Note: All Words can be typed in either uppercase or lowercase and should be typed as they appear above. The word “CUSTOMER” listed in red is the name of an existing table and should be typed in ALL Uppercase letters. If you omit the “WHERE” clause, then the first two lines of code will list all of the user defined constraints from the database. The “WHERE” clause allows the user to query a specific table only. If watching archived, click on the Notes tab for additional info.

29 Table Names List all Table Names that you created in your database:
Example: Select TABLE_NAME From USER_TABLES;

30 Table Structure List the Columns and data types, along with all other table structures that are set up on a particular Table that you set up in your database: Example: DESCRIBE Customer; Note: All Words can be typed in either uppercase or lowercase. The word “Customer“ listed in red is the name of an existing table.

31 Summary Created and ran SQL Commands Created tables Using SQL
Identified and used data types to define the columns in SQL tables Introduced and used nulls Added rows of data to tables Viewed table data Introduced and used Integrity Constraints Introduced and used System Commands

32 THE END You have completed the Lecture for:
Structured Query Language: Introductions, Data Definitions and Data Administration


Download ppt "CIT 214 Introduction to Database Management"

Similar presentations


Ads by Google