CIT 214 Introduction to Database Management

Slides:



Advertisements
Similar presentations
A Guide to Oracle9i1 Creating and Modifying Database Tables Chapter 2.
Advertisements

Session 2Introduction to Database Technology Data Types and Table Creation.
9 Creating and Managing Tables. Objectives After completing this lesson, you should be able to do the following: Describe the main database objects Create.
Data Definition Language (DDL)
Let’s try Oracle. Accessing Oracle The Oracle system, like the SQL Server system, is client / server. For SQL Server, –the client is the Query Analyser.
1 A GUIDE TO ORACLE8 CHAPTER 2: Creating and ModifyingDatabaseTables 2.
A Guide to Oracle9i1 Introduction To Forms Builder Chapter 5.
1 Chapter 2: Creating and Modifying Database Tables.
A Guide to SQL, Seventh Edition. Objectives Understand the concepts and terminology associated with relational databases Create and run SQL commands in.
Creating Database Tables © Abdou Illia MIS Spring /21/2015.
Concepts of Database Management Sixth Edition
Introduction to Structured Query Language (SQL)
A Guide to MySQL 3. 2 Objectives Start MySQL and learn how to use the MySQL Reference Manual Create a database Change (activate) a database Create tables.
A Guide to SQL, Seventh Edition. Objectives Understand, create, and drop views Recognize the benefits of using views Grant and revoke user’s database.
Using SQL Queries to Insert, Update, Delete, and View Data © Abdou Illia MIS Spring 2015 Wednesday 1/28/2015 Chapter 3A.
Oracle Data Definition Language (DDL)
A Guide to SQL, Eighth Edition Chapter Three Creating Tables.
1 An Introduction to SQL. 2 Objectives  Understand the concepts and terminology associated with relational databases  Create and run SQL commands 
ASP.NET Programming with C# and SQL Server First Edition
AL-MAAREFA COLLEGE FOR SCIENCE AND TECHNOLOGY INFO 232: DATABASE SYSTEMS CHAPTER 7 INTRODUCTION TO STRUCTURED QUERY LANGUAGE (SQL) Instructor Ms. Arwa.
Chapter 4 The Relational Model 3: Advanced Topics Concepts of Database Management Seventh Edition.
SQL data definition using Oracle1 SQL Data Definition using Oracle.
CS 3630 Database Design and Implementation. Your Oracle Account UserName is the same as your UWP username Followed Not case sensitive Initial.
CHAPTER:14 Simple Queries in SQL Prepared By Prepared By : VINAY ALEXANDER ( विनय अलेक्सजेंड़र ) PGT(CS),KV JHAGRAKHAND.
SQL data definition using Oracle1 SQL Data Definition using Oracle.
Oracle Data Definition Language (DDL) Dr. Bernard Chen Ph.D. University of Central Arkansas Fall 2008.
Sizing Basics  Why Size?  When to size  Sizing issues:  Bits and Bytes  Blocks (aka pages) of Data  Different Data types  Row Size  Table Sizing.
1 Structured Query Language (SQL). 2 Contents SQL – I SQL – II SQL – III SQL – IV.
1 Creating and Modifying Database Objects. 2 An Oracle database consists of multiple user accounts Each user account owns database objects Tables Views.
11 3 / 12 CHAPTER Databases MIS105 Lec15 Irfan Ahmed Ilyas.
1 A Guide to SQL Chapter 2. 2 Introduction Mid-1970s: SQL developed under the name SEQUEL at IBM by San Jose research facilities to be the data manipulation.
A Guide to MySQL 3. 2 Introduction  Structured Query Language (SQL): Popular and widely used language for retrieving and manipulating database data Developed.
Guide to Oracle 10g ITBIS373 Database Development Lecture 4a - Chapter 4: Using SQL Queries to Insert, Update, Delete, and View Data.
SQL Fundamentals  SQL: Structured Query Language is a simple and powerful language used to create, access, and manipulate data and structure in the database.
Oracle 11g: SQL Chapter 4 Constraints.
Chapter 4 Constraints Oracle 10g: SQL. Oracle 10g: SQL 2 Objectives Explain the purpose of constraints in a table Distinguish among PRIMARY KEY, FOREIGN.
SQL CREATING AND MANAGING TABLES lecture4 1. Database Objects ObjectDescription TableBasic unit of storage; composed of rows and columns ViewLogically.
1 Chapter 2: Creating and Modifying Database Objects.
Sql DDL queries CS 260 Database Systems.
Personal Oracle8i Create a new user Create a new table Enter data into a new table Export & import data Start and exit SQL Plus SQL Plus Syntax.
Oracle 9i. Agenda Start and exit SQL Plus (General) Start and exit SQL Plus (Tah 1006) Syntax Create a new user Create a new table Enter data into a new.
Academic Year 2015 Autumn. MODULE CC2006NI: Data Modelling and Database Systems Academic Year 2015 Autumn.
Starting with Oracle SQL Plus. Today in the lab… Connect to SQL Plus – your schema. Set up two tables. Find the tables in the catalog. Insert four rows.
Basic SQL*Plus edit and execute commands SQL*Plus buffer and built-in editor holds the last SQL statement Statements are created in free-flow style and.
Data Integrity & Indexes / Session 1/ 1 of 37 Session 1 Module 1: Introduction to Data Integrity Module 2: Introduction to Indexes.
3 A Guide to MySQL.
Fundamentals of DBMS Notes-1.
Guide to Oracle 10g Chapter 2: Creating and Modifying Database Tables.
CS 3630 Database Design and Implementation
Database Constraints Ashima Wadhwa.
Working in the Forms Developer Environment
TABLES AND INDEXES Ashima Wadhwa.
Managing Tables, Data Integrity, Constraints by Adrienne Watt
Chapter 6 - Database Implementation and Use
SQL and SQL*Plus Interaction
SQL Creating and Managing Tables
Unit 16 – Database Systems
Using SQL*Plus.
ORACLE SQL Developer & SQLPLUS Statements
DATABASE MANAGEMENT SYSTEM
SQL Creating and Managing Tables
SQL data definition using Oracle
A Guide to SQL, Eighth Edition
Oracle Data Definition Language (DDL)
ORACLE.
Chapter 2: Creating And Modifying Database Tables
Using SQL*Plus.
IST 318 Database Administration
Unit J: Creating a Database
Introduction to Oracle
Presentation transcript:

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

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

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.

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.

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

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

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.

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 99999 Fixed point = Number(5,2) - Biggest number would be 999.99 Floating Point = Number - Could be 99.9 or 99999 or 9.999

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.

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.

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

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

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. http://beginner-sql-tutorial.com/sql-integrity-constraints.htm If watching archived, click on the Notes tab for additional info.

If watching archived, click on the Notes tab for additional info. Integrity Rules in SQL http://beginner-sql-tutorial.com/sql-integrity-constraints.htm 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.

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

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 http://beginner-sql-tutorial.com/sql-integrity-constraints.htm

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. http://beginner-sql-tutorial.com/sql-integrity-constraints.htm

Foreign Key Integrity Constraint http://beginner-sql-tutorial.com/sql-integrity-constraints.htm 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.

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 http://beginner-sql-tutorial.com/sql-integrity-constraints.htm

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 http://beginner-sql-tutorial.com/sql-integrity-constraints.htm

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 http://beginner-sql-tutorial.com/sql-integrity-constraints.htm

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.

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

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

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’, ‘7155552345’, ‘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.

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;

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

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.

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

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.

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 http://beginner-sql-tutorial.com/sql-commands.htm

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