Dale Roberts Department of Computer and Information Science, School of Science, IUPUI Dale Roberts, Lecturer Computer Science, IUPUI

Slides:



Advertisements
Similar presentations
Tuning: overview Rewrite SQL (Leccotech)Leccotech Create Index Redefine Main memory structures (SGA in Oracle) Change the Block Size Materialized Views,
Advertisements

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)
Manipulating Data Schedule: Timing Topic 60 minutes Lecture
Virtual training week 4 structured query language (SQL)
Copyright  Oracle Corporation, All rights reserved. 10 Creating and Managing Tables.
Database Programming Sections 13. Marge Hohly  1. Which statements are True about the following sequence? The sequence was used to generate numbers.
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.
Oracle8 - The Complete Reference. Koch & Loney1 Chapter 17 Creating, Dropping, and Altering Tables and Views Presented by Victor Matos.
Introduction to Structured Query Language (SQL)
Module 9: Managing Schema Objects. Overview Naming guidelines for identifiers in schema object definitions Storage and structure of schema objects Implementing.
 SQL stands for Structured Query Language.  SQL lets you access and manipulate databases.  SQL is an ANSI (American National Standards Institute) standard.
Chapter 6 Additional Database Objects
Oracle9i Database Administrator: Implementation and Administration 1 Chapter 9 Index Management.
SQL data definition using Oracle1 SQL Data Definition using Oracle.
Database Technical Session By: Prof. Adarsh Patel.
SQL data definition using Oracle1 SQL Data Definition using Oracle.
CHAPTER 7 Tables and Constraints. Logical Progression of Steps in Creating a Database Environment 1. Install Oracle database binaries (Chapter 1) 2. Create.
Chapter 6 Additional Database Objects Oracle 10g: SQL.
10 Copyright © 2009, Oracle. All rights reserved. Using DDL Statements to Create and Manage Tables.
1 Copyright © 2006, Oracle. All rights reserved. Using DDL Statements to Create and Manage Tables.
SQL SQL Server : Overview SQL : Overview Types of SQL Database : Creation Tables : Creation & Manipulation Data : Creation & Manipulation Data : Retrieving.
1 Chapter 14 DML Tuning. 2 DML Performance Fundamentals DML Performance is affected by: – Efficiency of WHERE clause – Amount of index maintenance – Referential.
Lecture 2: Using DDL Statements to Create and Manage Tables & Indexes
1 Creating and Modifying Database Objects. 2 An Oracle database consists of multiple user accounts Each user account owns database objects Tables Views.
10 Creating and Managing Tables Objectives At the end of this lesson, you will be able to: Describe the main database objects Create tables Describe.
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.
Copyright  Oracle Corporation, All rights reserved. 10 Creating and Managing Tables.
SQL: DDL. SQL Statements DDL - data definition language –Defining and modifying data structures (metadata): database, tables, views, etc. DML - data manipulation.
6 1 Lecture 8: Introduction to Structured Query Language (SQL) J. S. Chou, P.E., Ph.D.
Topic 1: Introduction to SQL. SQL stands for Structured Query Language. SQL is a standard computer language for accessing and manipulating databases SQL.
Database structure and space Management. Database Structure An ORACLE database has both a physical and logical structure. By separating physical and logical.
Database Systems Design, Implementation, and Management Coronel | Morris 11e ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or.
SQL Basics. What is SQL? SQL stands for Structured Query Language. SQL lets you access and manipulate databases.
Database structure and space Management. Segments The level of logical database storage above an extent is called a segment. A segment is a set of extents.
Tables and Constraints Oracle PL/SQL. Datatypes The SQL Data Definition Language Commands (or DDL) enable us to create, modify and remove database data.
Database Lab Lecture 1. Database Languages Data definition language ( DDL ) Data definition language –defines data types and the relationships among them.
Prince Sultan University Dept. of Computer & Information Sciences CS 340 Introduction to Database Systems.
SQL: Part 1 Original materials supplied by the Oracle Academic Initiative (OAI). Edited for classroom use by Professor Laku Chidambaram. Not for commercial.
9 Copyright © 2004, Oracle. All rights reserved. Using DDL Statements to Create and Manage Schema Objects.
8 Copyright © 2007, Oracle. All rights reserved. Managing Schema Objects.
Database Programming Sections 11 & 12 –Sequences, Indexes, and Synonymns.
Week 4 Lecture 2 Advanced Table Management. Learning Objectives  Create tables with large object (LOB) columns and tables that are index-organized 
9 Copyright © Oracle Corporation, All rights reserved. Creating and Managing Tables.
SQL CREATING AND MANAGING TABLES lecture4 1. Database Objects ObjectDescription TableBasic unit of storage; composed of rows and columns ViewLogically.
13 Copyright © Oracle Corporation, All rights reserved. Maintaining Data Integrity.
Chapter 5 : Integrity And Security  Domain Constraints  Referential Integrity  Security  Triggers  Authorization  Authorization in SQL  Views 
Altering Tables and Constraints Database Systems Objectives Add and modify columns. Add, enable, disable, or remove constraints. Drop a table. Remove.
Creating and Managing Tables 14. ObjectivesObjectives After completing this lesson, you should be able to do the following: After completing this lesson,
Relational Database Management System(RDBMS) Structured Query Language(SQL)
Database Programming Sections 12 – Sequences, Indexes, and Synonymns.
IMS 4212: Constraints & Triggers 1 Dr. Lawrence West, Management Dept., University of Central Florida Stored Procedures in SQL Server.
Dale Roberts 1 Department of Computer and Information Science, School of Science, IUPUI Dale Roberts, Lecturer Computer Science, IUPUI
CSCI N311: Oracle Database Programming 5-1 Chapter 15: Changing Data: insert, update, delete Insert Rollback Commit Update Delete Insert Statement –Allows.
7 1 Database Systems: Design, Implementation, & Management, 7 th Edition, Rob & Coronel 7.6 Advanced Select Queries SQL provides useful functions that.
Constraints and Views Chap. 3-5 continued (7 th ed. 5-7)
Oracle 11g: SQL Chapter 5 Data Manipulation and Transaction Control.
Fundamentals of DBMS Notes-1.
The Basics of Data Manipulation
Database structure and space Management
SQL Creating and Managing Tables
DATABASE MANAGEMENT SYSTEM
SQL Creating and Managing Tables
SQL Creating and Managing Tables
CS4222 Principles of Database System
The Basics of Data Manipulation
Oracle Data Definition Language (DDL)
Contents Preface I Introduction Lesson Objectives I-2
Presentation transcript:

Dale Roberts Department of Computer and Information Science, School of Science, IUPUI Dale Roberts, Lecturer Computer Science, IUPUI Data Definition Language 1

Dale Roberts Context Client Tool SQLPlus, Toad Database Management System SQL – Query an object – SELECT DML – Change data in an object – INSERT, UPDATE, DELETE DDL – Create objects - CREATE 2 SQLPlusToad

Dale Roberts Basic CREATE TABLE Physical implementation of a Relation. Syntax: CREATE TABLE tablename ( column_a type NOT NULL, column_btype,...); Data Types: CHAR(n) character data of fixed size up to 2000 characters. VARCHAR2(n)variable length character data up to 4000 characters. NUMBERinteger data NUMBER(n)integer data, length n NUMBER(m,n)floating point data, m = total number of digits (not width), n = digits after decimal point DATEdate/time data TIMESTAMPdate/time/fractions of a second NOT NULLnull constraint, requires a value for row to exist 3

Dale Roberts Other CREATE TABLE clauses Appendix shows all CREATE TABLE clauses pp Object tables XML types STORAGE clause – specifies how the data is stored in the database VARRAYs and Nested Tables LOB (Large OBject) parameters External Tables And many others 4

Dale Roberts Constraints Candidate Keys: Column(s) which uniquely identify rows in a table. Also called unique constraint. Primary Keys: Column(s) used to specify and enforce uniqueness among rows in a table. create table employee ( employee_id varchar(8) not null, column x,...., constraint emp_PK primary key (employee_id)); Foreign Keys: Column(s) used to reference rows in another table. These values are actually the primary keys in the other table. alter table employee add constraint emp_dept_FK foreign key (dept_id) references department(dept_id); Check Constraint: A constraint that forces a set of values (domain) for a column. NOT NULL columns automatically get a system generated check constraint. 5

Dale Roberts Constraint Example SQL> desc customer Name Null? Type ID NUMBER NAME VARCHAR2(40) STATE CHAR(2) SQL> desc state2 Name Null? Type STATE_CODE NOT NULL CHAR(2) STATE_NAME VARCHAR2(50) SQL> alter table state2 2 add constraint PK_State2 3 primary key (state_code); Table altered. SQL> alter table customer 2 add constraint cust_another 3 foreign key (state) 4 references 5 state2(state_code); Table altered. 6

Dale Roberts DROP vs TRUNCATE vs DELETE TABLE Dropping Tables : DROP TABLE tablename; Dropping a table removes the table, both the table’s data and definition are removed. Truncating Tables: TRUNC TABLE tablename; Truncating a table removes only the data, Table lock required, cannot be rolled back, implicit commit, the table’s definition is still available. Deleting Tables: DELETE tablename WHERE condition; Removes a set of data, Table lock not required, read-consistent view, rollback. 7

Dale Roberts ALTER TABLE Tables can be altered to: change column definitions, add new columns (without NOT NULL), rename columns enable or disable constraints, change storage parameters, drop columns as of 8i. ALTER TABLE emp ADD (thriftplan NUMBER(7,2), loancode CHAR(1) NOT NULL); ALTER TABLE emp MODIFY (thriftplan NUMBER(9,2)); alter table emp drop column thriftplan; 8

Dale Roberts Views Special considerations about views: You cannot insert if the underlying table has any NOT NULL columns that are not in the view. You cannot insert/update if any one of the view’s columns referenced in the insert/update contains functions or calculations. You cannot insert/update/delete if the view contains group by, distinct, or a reference to rownum. INSTEAD OF triggers get around the above limitation. View Stability: dropping the underlying tables of a view prevents the view from working. altering the underlying tables of a view are OK. Order By in Views: ORDER BY does not work in views before Oracle 8i. GROUP BY still consolidates rows in views, a side affect of GROUP BY is ordering rows. READ ONLY: The read only clause prevents update/insert/delete of rows in a view. 9

Dale Roberts Creating a Table from Another Table  Includes the data: create table RAIN as select City, Precipitation from TROUBLE;  Does not include the data, only the table definition: create table RAIN as select City, Precipitation from TROUBLE where 1=2; 10

Dale Roberts Logging A critical design feature of database management systems is the safeguarding of data in the event of a system failure. All commercial databases achieve the high standard of recovery the database to the last committed transaction. Logging is the mechanism by which Oracle creates redo logs, two or more preallocated files that store all changes made to the database as they occur. It is common practice to place redo logs in a separate disk than the tablespaces. A separate concept archiving is use to copy redo logs for permanent storage. During recovery, both archive logs and redo logs are used to replay transactions from the last full backup to “roll forward” to the last committed transaction. 11

Dale Roberts Logging and Mass Updates Logging is critical for enabling recovery during production updates. Some activities occur when the database is not operational, for example during software releases, initial loads, or mass updates of data. Typically, if a failure occurs when the system is not operational, no recovery is required. In this case, it is useful to create tables with NOLOGGING, load the initial data or make a mass update, and then ALTER TABLE to turn on LOGGING. Use of NOLOGGING is important when working with large tables. It is the difference between a job running days versus a few hours. 12

Dale Roberts Partitioning Partitioning address key issues with the management of very large tables. Decompose tables into more manageable pieces called partitions. Partitioning is declarative meaning that no SQL must be rewritten. Data is managed at partition-level for loads, indexes creation, backup and recovery. If care is taken to partition by expressions in WHERE clause, queries can access a single partition. 13

Dale Roberts Partitioning Choices List – values by partition Range – range by partition Hash – random (balanced) partition Composite – Partitioning and subpartitioning 14

Dale Roberts Partition Example create table BOOKSHELF (CategoryName VARCHAR2(20), …) partition by range (CategoryName) (partition PART1 values less than (‘B’) (partition PART1 values less than (‘B’) tablespace PART1_TS, tablespace PART1_TS, partition PART2 values less than (MAXVALUE) partition PART2 values less than (MAXVALUE) tablespace PART2_TS); tablespace PART2_TS); 15

Dale Roberts Indexing Like the index in the back of your textbook, indexes provide a list of sorted values and the location of where to find more information about the value. Indexes require storage separate from the rows of a table. Individual tables rows are accessed via ROWID. Default index is B*-tree, depicted below, and discussed in detail in any data structures text. 16

Dale Roberts Types of Indexes Primary Key – Unique and not null – PK_* Unique Key – Unique and allows null – UK_* Foreign Key – Nonunique and references PK in related table – FK_* Nonuniqies index – Nonunique allows null – IX_* Bitmap indexes – useful for WHERE clauses looking up combinations of values. While maintaining B* adds to the cost of DML statement, maintaining bitmap index during DML is prohibitive. Do not use bitmap indexes on tables whose values change regularly. Important: Indexes are an example of an important Computer Science concept – the space vs time tradeoff. Also typical of database tradeoff DML vs SQL. 17

Dale Roberts Index Examples Unique: create unique index UK_BOOKSHELF_AUTHOR on BOOKSHELF_AUTHOR(Title, AuthorName) tablespace INDEX_TS; Support queries WHERE Title=value AND AuthorName = value; or WHERE Title=value; - -Leading value Function-based nonunique index create index IX_BOOKSHELF_2 on BOOKSHELF(Upper(Title)); Supports queries WHERE Upper(Title) = value; -- must match exactly 18

Dale Roberts Sequences Sequence are declared counters used to generate sequences of unique values. Often use to generate unique id during INSERT and UPDATE. create sequence CustomerId increment by 1 start with 1000; insert into CUSTOMER_DEMO(Name, Contact, ID) values (‘COLE CONSTRUCTION’, ‘Veronica’, CustomerId.NextVal); 19

Dale Roberts Acknowledgements Loney, Oracle Database 10g The Complete Reference 20