Chapter 2: Creating And Modifying Database Tables

Slides:



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

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.
Introduction to Structured Query Language (SQL)
SQL components In Oracle. SQL in Oracle SQL is made up of 4 components: –DDL Data Definition Language CREATE, ALTER, DROP, TRUNCATE. Creates / Alters.
1 Chapter 2: Creating and Modifying Database Tables.
Guide to Oracle 10g1 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.
Murali Mani SQL DDL and Oracle utilities. Murali Mani Datatypes in SQL INT (or) INTEGER FLOAT (or) REAL DECIMAL (n, m) CHAR (n) VARCHAR (n) DATE, TIME.
Creating Database Tables CS 320. Review: Levels of data models 1. Conceptual: describes WHAT data the system contains 2. Logical: describes HOW the database.
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.
1 Chapter 2: Creating and Modifying Database Tables.
Oracle Data Definition Language (DDL)
A Guide to SQL, Eighth Edition Chapter Three Creating Tables.
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.
Database Design lecture 3_1 1 Database Design Lecture 3_1 Data definition in SQL.
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 7 SQL HUANG XUEHUA. SQL SQL server2005 introduction Install components  management studio.
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.
10 Copyright © 2009, Oracle. All rights reserved. Using DDL Statements to Create and Manage Tables.
Copyright © 2004, Oracle. All rights reserved. Using DDL Statements to Create and Manage Tables.
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.
7 1 Chapter 7 Introduction to Structured Query Language (SQL) Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel.
9 Copyright © Oracle Corporation, All rights reserved. Creating and Managing Tables.
ITBIS373 Database Development Lecture 2 - Chapter 2 Creating And Modifying Database Tables.
A Guide to MySQL 3. 2 Introduction  Structured Query Language (SQL): Popular and widely used language for retrieving and manipulating database data Developed.
Database Systems Design, Implementation, and Management Coronel | Morris 11e ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or.
SQL Fundamentals  SQL: Structured Query Language is a simple and powerful language used to create, access, and manipulate data and structure in the database.
Tables and Constraints Oracle PL/SQL. Datatypes The SQL Data Definition Language Commands (or DDL) enable us to create, modify and remove database data.
Oracle 11g: SQL Chapter 4 Constraints.
Database Lab Lecture 1. Database Languages Data definition language ( DDL ) Data definition language –defines data types and the relationships among them.
Chapter 4 Constraints Oracle 10g: SQL. Oracle 10g: SQL 2 Objectives Explain the purpose of constraints in a table Distinguish among PRIMARY KEY, FOREIGN.
1 Chapter 2: Creating and Modifying Database Objects.
Sql DDL queries CS 260 Database Systems.
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.
Altering Tables and Constraints Database Systems Objectives Add and modify columns. Add, enable, disable, or remove constraints. Drop a table. Remove.
Relational Database Management System(RDBMS) Structured Query Language(SQL)
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.
Physical Model Lecture 11. Physical Data Model The last step is the physical design phase, In this phase data is – Store – Organized and – Access.
3 A Guide to MySQL.
CIT 214 Introduction to Database Management
Guide to Oracle 10g Chapter 2: Creating and Modifying Database Tables.
CS 3630 Database Design and Implementation
Chapter 5 Introduction to SQL.
Managing Tables, Data Integrity, Constraints by Adrienne Watt
SQL: Schema Definition and Constraints Chapter 6 week 6
SQL and SQL*Plus Interaction
Open Source Server Side Scripting Permissions & Users
ORACLE SQL Developer & SQLPLUS Statements
Chapter 7 Working with Databases and MySQL
Chapter 8 Working with Databases and MySQL
SQL data definition using Oracle
Oracle Data Definition Language (DDL)
Using DDL Statements to Create and Manage Tables
Session - 6 Sequence - 1 SQL: The Structured Query Language:
Contents Preface I Introduction Lesson Objectives I-2
Using SQL*Plus.
Using DDL Statements to Create and Manage Tables
Data Definition Language
Chapter # 7 Introduction to Structured Query Language (SQL) Part I.
Session - 6 Sequence - 1 SQL: The Structured Query Language:
Introduction to Oracle
SQL (Structured Query Language)
Presentation transcript:

Chapter 2: Creating And Modifying Database Tables Guide to Oracle 10g

Objectives After completing this chapter, you should be able to: Use structured query language (SQL) commands to create, modify, and drop database tables Explain Oracle 10g user schemas Define Oracle 10g database tables Create database tables using SQL*Plus Debug Oracle 10g SQL commands and use online help resources available through the Oracle Technology Network (OTN) Guide to Oracle 10g

Objectives (continued) View information about your database tables using Oracle 10g data dictionary views Modify and delete database tables using SQL*Plus Guide to Oracle 10g

Introduction to SQL Structured query language (SQL) Standard query language for relational databases Consists of about 30 commands Enables users to create database objects and manipulate and view data SQL-99 Most recent version Guide to Oracle 10g

Introduction to SQL (continued) Structured query language (SQL) (continued) Basic categories for SQL commands Data definition language (DDL) Data manipulation language (DML) Reserved words SQL command words Guide to Oracle 10g

Oracle 10g User Accounts User account User schema Database objects Created for each user Identified using unique username and password User schema Area of database belonging to user Database objects Also called schema objects Objects in user schema Guide to Oracle 10g

Defining Oracle 10g Database Tables Primary data objects in relational database Constraints Restrictions on data values that column can store Oracle naming standard Rules that Oracle corporation has established for naming all database objects Guide to Oracle 10g

Defining Oracle 10g Database Tables (continued) Oracle naming standard One to 30 characters long Contain letters, numbers, and special symbols $, _, and # Begin with character CREATE TABLE SQL syntax CREATE TABLE tablename (columnname1 data_type, columnname2 data_type, …) Guide to Oracle 10g

Oracle 10g Data Types Data type Specifies kind of data that column stores Provides means for error checking Enable DBMS to use storage space more efficiently Basic types Character Number Date/time Large object Guide to Oracle 10g

Character Data Types VARCHAR2 CHAR Variable-length character data Syntax columnname VARCHAR2(maximum_size) CHAR Fixed-length character data columnname CHAR[(maximum_size)] Guide to Oracle 10g

Character Data Types (continued) Unicode Standardized technique that provides way to encode data in diverse languages NVARCHAR2 Counterpart of VARCHAR2 Uses Unicode coding NCHAR Counterpart of CHAR Uses Unicode encoding Guide to Oracle 10g

Number Data Types NUMBER Precision Used for all numeric data Syntax columnname NUMBER [([precision,] [scale])] Precision Total number of digits both to left and right of decimal point Guide to Oracle 10g

Number Data Types (continued) Integer number syntax columnname NUMBER(precision) Fixed-point number Contains specific number of decimal places Column declaration specifies both precision and scale Example price NUMBER(5, 2) Guide to Oracle 10g

Number Data Types (continued) Floating-point number Contains variable number of decimal places Syntax columnname NUMBER Example s_gpa NUMBER Guide to Oracle 10g

Date And Time Data Types Datetime data subtypes Store actual date and time values DATE TIMESTAMP Interval data subtypes Store elapsed time interval between two datetime values INTERVAL YEAR TO MONTH INTERVAL DAY TO SECOND Guide to Oracle 10g

Date And Time Data Types (continued) Stores dates from December 31, 4712 BC to December 31, AD 4712 Default date format DD-MON-YY Default time format HH:MI:SS AM Syntax: columnname DATE Guide to Oracle 10g

Date And Time Data Types (continued) TIMESTAMP Stores date values similar to DATE data type Also stores fractional seconds Syntax columnname TIMESTAMP (fractional_seconds_precision) Guide to Oracle 10g

Date And Time Data Types (continued) INTERVAL YEAR TO MONTH Stores time interval expressed in years and months Syntax +|– elapsed_years-elapsed_months Guide to Oracle 10g

Date And Time Data Types (continued) INTERVAL DAY TO SECOND Stores time interval expressed in days, hours, minutes, and seconds Syntax +|– elapsed_days elapsed_hours:elapsed_minutes:elapsed_seconds Guide to Oracle 10g

Large Object (LOB) Data Types Store binary data such as: Digitized sounds or images References to binary files from word processor or spreadsheet General syntax columnname Lob_data_type Guide to Oracle 10g

Large Object (LOB) Data Types (continued) Guide to Oracle 10g

Constraints Rules that restrict data values that can be entered into column Types of constraints: Integrity constraints Value constraints Table constraint Restricts data value with respect to all other values in table Guide to Oracle 10g

Constraints (continued) Column constraint Limits value that can be placed in specific column Irrespective of values that exist in other table rows Constraint definitions should be placed either: At end of CREATE TABLE command after table columns declared Within each column definition Guide to Oracle 10g

Constraints (continued) Constraint naming convention tablename_columnname_constraintid Guide to Oracle 10g

Common ConstraintID Abbreviations Guide to Oracle 10g

Integrity Constraints Primary key Syntax (within table definition) CONSTRAINT constraint_name PRIMARY KEY Syntax (at end of table definition) CONSTRAINT constraint_name PRIMARY KEY (columnname) Guide to Oracle 10g

Integrity Constraints (continued) Foreign key Column constraint Specifies that value user inserts in column must exist as primary key in referenced table Syntax (placed at end of table definition) CONSTRAINT constraint_name FOREIGN KEY (columnname) REFERENCES primary_key_tablename (primary_key_columnname) Guide to Oracle 10g

Integrity Constraints (continued) Foreign key (continued) Syntax (placed within table definition) CONSTRAINT constraint_name REFERENCES primary_key_tablename (primary_key_columnname) Composite key Syntax PRIMARY KEY (columnname1, columnname2 …) Guide to Oracle 10g

Integrity Constraints (continued) Value constraints Column-level constraints Restrict data values that users can enter Commonly used value constraints CHECK conditions NOT NULL constraint DEFAULT constraint UNIQUE constraint Guide to Oracle 10g

Creating Database Tables Using SQL*Plus Start SQL*Plus Type username and password Type SQL commands at SQL prompt End each command with semicolon (;) Press Enter to submit commands Guide to Oracle 10g

Creating Database Tables Using SQL*Plus (continued) SQL*Plus interpreter Checks command for syntax errors Submits command to database SQL commands are not case sensitive When creating database tables that contain foreign key references Must first create table in which foreign key is primary key Guide to Oracle 10g

SQL Command to Create the LOCATION Table Guide to Oracle 10g

Creating and Editing SQL Commands Using a Text Editor Good approach for entering commands: Type commands into text editor such as Notepad Copy commands, then paste into SQL*Plus Execute commands Script Text file that contains several related SQL commands Guide to Oracle 10g

Using Oracle Online Help Resources to Debug SQL Commands Syntax error SQL*Plus interpreter displays error information Line number within command that caused error Position of error within line Error code and description of error Guide to Oracle 10g

Using Oracle Online Help Resources To Debug SQL Commands (continued) Oracle 10g error codes have: 3-character prefix (such as ORA) 5-digits Causes of SQL command errors are not always readily apparent Need to retrieve more information about error Connect to Oracle Technology Network (OTN) Web Site and search for error code Guide to Oracle 10g

Using Oracle Online Help Resources To Debug SQL Commands (continued) Last resort debugging technique Create table multiple times Each time adding column declaration Repeat process until you find declaration causing error Drop table command To delete table syntax: DROP TABLE tablename Guide to Oracle 10g

Exiting SQL*Plus Type exit at SQL prompt Click File on menu bar, and then click Exit Click Close button on program window title bar Guide to Oracle 10g

Creating a Table with a Foreign Key Constraint Guide to Oracle 10g

Viewing Information about Tables DESCRIBE command View column names and data types of table Syntax DESCRIBE tablename Oracle 10g data dictionary Consists of tables that contain information about structure of database Guide to Oracle 10g

Viewing Information about Tables (continued) Oracle10g data dictionary (continued) System creates data dictionary in user schema named SYS Users do not directly manipulate data dictionary View Database object DBMS bases on actual database table Enables DBMS to present table data in different format based on needs of users Guide to Oracle 10g

Viewing Information about Tables (continued) Data dictionary views categories USER ALL DBA Syntax SELECT view_columnname1, view_columnname2 … FROM prefix_object; Guide to Oracle 10g

Database Objects with Data Dictionary Views Guide to Oracle 10g

Modifying and Deleting Database Tables Plan tables carefully to avoid having to change structure of database tables later Unrestricted action Some specifications of tables can always be modified Restricted action Table specifications that can be modified only in certain situations Guide to Oracle 10g

Unrestricted Actions when Modifying Database Tables Guide to Oracle 10g

Deleting and Renaming Existing Tables DROP TABLE command Delete table Syntax DROP TABLE tablename; DROP TABLE tablename CASCADE CONSTRAINTS; Guide to Oracle 10g

Deleting and Renaming Existing Tables (continued) RENAME TO command Syntax RENAME old_tablename TO new_tablename; Guide to Oracle 10g

Adding Columns to Existing Tables Add new column to table Syntax ALTER TABLE tablename ADD(columnname data_declaration constraints); Guide to Oracle 10g

Modifying Existing Column Data Definitions Modify existing column’s data declaration Syntax ALTER tablename MODIFY(columnname new_data_declaration); Guide to Oracle 10g

Deleting a Column Data stored in deleted column removed from database Syntax ALTER TABLE tablename DROP COLUMN columnname; Guide to Oracle 10g

Renaming a Column Syntax ALTER TABLE tablename RENAME COLUMN old_columnname TO new_columnname; Guide to Oracle 10g

Adding and Deleting Constraints Add constraint to existing table Syntax ALTER TABLE tablename ADD CONSTRAINT constraint_name constraint_definition; Remove existing constraint DROP CONSTRAINT constraint_name; Guide to Oracle 10g

Enabling and Disabling Constraints Constraint enabled DBMS enforces constraint when users attempt to add new data to database Disable existing constraint syntax ALTER TABLE tablename DISABLE CONSTRAINT constraint_name; Enable existing constraint syntax ENABLE CONSTRAINT constraint_name; Guide to Oracle 10g

Summary SQL commands include Data description language (DDL) commands Data manipulation language (DML) commands Each user account owns table and data objects in own area of database Called user schema When creating database table specify table name, column names, data type, and column sizes Guide to Oracle 10g

Summary (continued) Constraints restrict data values that users can enter into database columns When SQL commands have errors interpreter reports: Line number Position of character causing error Returns error code and description Use describe command to display table info Guide to Oracle 10g