1 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)
SQL’s Data Definition Language (DDL) n DDL statements define, modify and remove objects from data dictionary tables maintained by the DBMS n Whenever you.
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 Oracle9i1 Advanced SQL And PL/SQL Topics Chapter 9.
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.
Introduction to Structured Query Language (SQL)
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.
Database Management System LICT 3011 Eyad H. Elshami.
Using SQL Queries to Insert, Update, Delete, and View Data © Abdou Illia MIS Spring 2015 Wednesday 1/28/2015 Chapter 3A.
Your Oracle Account UserName is the same as your UWP username Followed Not case sensitive Initial Password: UWPstudent Password is case sensitive.
Oracle Data Definition Language (DDL)
Copyright © Curt Hill SQL The Data Definition Language.
Session 5: Working with MySQL iNET Academy Open Source Web Development.
Chapter 9 SQL and RDBMS Part C. SQL Copyright 2005 Radian Publishing Co.
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.
SQL data definition using Oracle1 SQL Data Definition using Oracle.
© 2007 by Prentice Hall3-1 Introduction to Oracle 10g Chapter 3 Creating, Modifying, Renaming, and Deleting Database Tables James Perry and Gerald Post.
Oracle Data Definition Language (DDL) Dr. Bernard Chen Ph.D. University of Central Arkansas Fall 2008.
Oracle Database Administration
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.
Copyright © 2004, 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 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.
Intro to SQL| MIS 2502  Spacing not relevant › BUT… no spaces in an attribute name or table name  Oracle commands keywords, table names, and attribute.
6 1 Lecture 8: Introduction to Structured Query Language (SQL) J. S. Chou, P.E., Ph.D.
ITBIS373 Database Development Lecture 2 - Chapter 2 Creating And Modifying Database Tables.
ITBIS373 Database Development Lecture 3a - Chapter 3: Using SQL Queries to Insert, Update, Delete, and View Data.
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.
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.
Lecture5: SQL Overview, Oracle Data Type, DDL and Constraints Ref. Chapter6 Lecture4 1.
9 Copyright © Oracle Corporation, All rights reserved. Creating and Managing Tables.
1 Chapter 2: Creating and Modifying Database Objects.
Sql DDL queries CS 260 Database Systems.
©Silberschatz, Korth and Sudarshan1 Structured Query Language (SQL) Data Definition Language Domains Integrity Constraints.
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.
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
Managing Tables, Data Integrity, Constraints by Adrienne Watt
SQL: Schema Definition and Constraints Chapter 6 week 6
ORACLE SQL Developer & SQLPLUS Statements
DATABASE MANAGEMENT SYSTEM
SQL data definition using Oracle
Oracle Data Definition Language (DDL)
Chapter 2: Creating And Modifying Database Tables
Introduction to Oracle
SQL (Structured Query Language)
Presentation transcript:

1 Chapter 2: Creating and Modifying Database Tables

Guide to Oracle 10g2 The Oracle 10g Client/Server Database Oracle 10g Latest release of Oracle Corporation’s relational database Client/server database Server side DBMS server process Oracle Net Utility that enables network communication between client and server

Guide to Oracle 10g3 Client/Server Architecture for Oracle 10g DBMS

Guide to Oracle 10g4 The Oracle 10g Client/Server Database (continued) Oracle Application Server Used to create World Wide Web pages that allow users to access Oracle databases Oracle client products: SQL*Plus Oracle 10g Developer Suite Enterprise Manager

5 Database Objects An Oracle database consists of multiple user accounts Each user account owns database objects Tables Views Stored programs Etc.

6  Query: command to perform operation on database object  Create  Modify  View  Delete  Structured Query Language (SQL)  Standard query language for relational databases Database Queries

7 SQL Command Types Data Definition Language (DDL) Used to create and modify the structure of database objects Data Manipulation Language (DML) Used to insert, update, delete, and view database data

8 DDL Commands Used to create and modify the structure of database objects CREATE ALTER DROP DDL commands execute as soon as they are issued, and do not need to be explicitly saved

9 DML Commands Used to insert, view, and modify database data INSERT UPDATE DELETE SELECT DML commands need to be explicitly saved or rolled back COMMIT ROLLBACK

10 User Accounts Each Oracle database user has a user schema Area in the database where the user’s database objects are stored Identified by a unique username and protected by a password Each user schema is granted specific privileges

11 Types of Database Privileges System Privileges Control the operations that the user can perform within the database Connecting to the database (Create Session), creating new tables, shutting down the database, etc. Object Privileges Granted on individual database objects Controls operations that a user can perform on a specific object (insert data, delete data, etc.) When you create an object in your user schema, you can then grant object privileges on that object to other database users

12  Oracle SQL command line utility for issuing SQL commands  Starting SQL Plus Break Time: SQL Plus LOGON to YOUR Oracle Account

13 How to Access Your Oracle Account User Name: Password: Host string: 1. Click the START button, point to Programs 2. Select Oracle –Oracle10g, then 3. Click Application Development, then 4. Select SQL PLUS

14 Creating New User Accounts Done by DBA Syntax: CREATE USER username IDENTIFIED BY password;

15 Oracle Naming Standard Oracle database objects must adhere to the Oracle Naming Standard 1 to 30 characters long Must begin with a character Can contain characters, numbers, and the symbols $, _, and #

16  To create a table, you must specify:  Table name  Field names  Field data types  Field sizes  Constraints Defining Database Tables

17  Must follow the Oracle Naming Standard  Each table in a user schema must have a unique name within that user schema  Each field in a table must have a unique name within that table Table and Field Names

18 Creating a Table CREATE TABLE tablename (fieldname1 data_type, (fieldname2 data_type, …)

19  Data type: specifies type of data stored in a field  Date, character, number. LONG, RAW, LONG RAW, BLOB  Uses  Error checking  Efficient use of storage space Oracle Data Types

20  VARCHAR2 columnname VARCHAR2(max_size)  Variable-length character strings  Max_size can be between 1 and 4,000 characters  Must specify the size  No trailing blank spaces are added  If more than max_size data is inserted, an error occurs.  Example declaration: student_name VARCHAR2(30) Oracle Character Data Types

21  CHAR columnname CHAR(max_size)  Fixed-length character data  Max_size can be between 1 and 2000 characters  Max_size is optional. Default is 1.  Adds trailing blank spaces to pad width  If more than max_size data is inserted, an error occurs.  Example declaration: student_gender CHAR(2) Character Data Types

22 Character Subtypes Examples: VARCHAR2(5)‘Smith’ or ‘Smi’ CHAR(5)‘Smith’ or ‘Smi ’

23 Question: Which query will possibly generate student information? What data type should be used if there is any chance that all column spaces will NOT be filled? Answer: VARCHAR2 s_last VARCHAR2(15); SELECT s_last, s_first, s_address FROM student WHERE s_last = ‘Smith’; s_last CHAR(15); SELECT s_last, s_first, s_address FROM student WHERE s_last = ‘Smith’;

24 3. NVARCHAR2 and NCHAR Analogous to VARCHAR2 and CHAR but use Unicode rather than ASCII Used to hold character data in languages other than English (Japanese). Character Data Types

25  NUMBER  stores negative, positive, fixed, and floating point numbers values between and  General declaration format: variable_name NUMBER(precision, scale) Number Data Type

26  Number type (integer, fixed point, floating point) specified by precision and scale  Precision: total number of digits on either side of the decimal point. It does not include the decimal point itself or any commas or any formatting symbols.  Scale: number of digits to right of decimal point NUMBER Data Types

27  Whole number with no digits to right of decimal point  Precision is maximum width  Scale is omitted  Sample declaration: s_age NUMBER (2) Integer Numbers

28  Contain a specific number of decimal places  Precision is maximum width  Scale is number of decimal places  Sample declaration: item_price NUMBER(5, 2) (decimal point is not included) Fixed Point Numbers

29  Contain a variable number of decimal places  Precision and scale are omitted  Sample declaration: s_GPA NUMBER Floating Point Numbers

30  DATE  Stores dates from 1/1/4712 BC to 12/31/4712 AD  Stores both a date and time component  Default date format: DD-MON-YY HH:MI:SS AM  example: 05-JUN-03 12:00:00 AM  Sample declaration: s_dob DATE Date Data Type

31  If no time value is given when a new date is inserted, default value is 12:00:00 AM  If no date value is given when a new time is inserted, default date is first day of current month Specifying Date and Time Values

32  The same as Date DT, but it stores also fractional seconds.  Field Timestamp(Fr_Se_Precision)  E.g: ship_dt Timestamp(2)  Fractional Seconds Precision default value is 6 (If omitted). TIMESTAMP Data Type

33  Field Interval Year(Y_Pr) To Month.  Y_Pr: Year Precision(Default: 6).  E.g: elapsed Interval Year(2) To Month.  Possible Values: :add 2 years and 11 months to a known date :subtract 11 years and 4 months. Interval Year to Month Data Type

34  Field Interval Day(D_Pr) To Second(Fr_Se_pr).  D_Pr: Day Precision(Default : 2).  Fr_Se_Pr: Fractional Seconds Precision (Default : 6).  Possible value: :20:32.00 (Days Hours:Minutes:Seconds.Fractions) Interval Day to Second Data Type

35 Large Object (LOB) Data Types Binary Large Object (BLOB) Stores up to 4 GB of binary data Character Large Object (CLOB) Stores up to 4 GB of character data BFILE Stores a reference to a binary file maintained in the operating system NCLOB Character LOB that supports 16-bit character code

36 6. Large Object (LOB) Data Types Ex: f_image BLOB;

37 Declaring LOB Data Fields Item size is not specified Examples: item_image BLOB item_image BFILE

38  Syntax: CREATE TABLE table_name (fieldname1 datatype, fieldname2 datatype, …);  Example: CREATE TABLE my_students (s_id NUMBER(6), s_name VARCHAR2(30), s_dob DATE, s_class CHAR(2)); Creating a Database Table

39 Constraints Rules that restrict the values that can be inserted into a field Types of constraints Integrity: define primary and foreign keys Value: specify values or ranges of values that can be inserted

40 Constraint Levels Table constraint Restricts the value of a field with respect to all other table records Example: primary key value must be unique for each record Column constraint Restricts values in a specific column Example: values in an S_GENDER field must be ‘M’ or ‘F’

41  Internal name used by DBMS to identify the constraint  Each constraint name in a user schema must be unique  If you do not name a constraint, the system will automatically generate an unintuitive name Constraint Names

42  Constraint naming convention: tablename_fieldname_constraintID  Constraint ID values:  Primary key: pk  Foreign key: fk  Check condition: cc  Not NULL: nn  Unique: uk  Example constraint name: my_students_s_id_pk Constraint Names

43  Table-level  Defining a primary key: CONSTRAINT constraint_name PRIMARY KEY  Example: s_id NUMBER(6) CONSTRAINT student_s_id_pk PRIMARY KEY Primary Key Constraints

44  Can be defined when field is declared Primary Key Constraints

45  Can also be defined after all table field definitions are completed Primary Key Constraints

46  Syntax: CONSTRAINT constraint_name PRIMARY KEY (field1, field2)  Must be defined after fields that compose key are defined Composite Primary Keys

47  Table-level  Can only be defined after field is defined as a primary key in another table  Syntax: CONSTRAINT constraint_name REFERENCES primary_key_table_name (field_name) Foreign Key Constraints

48  Can be defined when field is declared Foreign Key Constraints

49  Can also be defined after all table field definitions are completed Foreign Key Constraints

50  Column-level  Restricts data values that can be inserted in a field  In general, avoid value constraints because they make the database very inflexible Value Constraints

51  Check condition: restricts to specific values  Example: s_gender (M or F) CONSTRAINT my_students_s_gender_cc CHECK (s_gender = ‘M’) OR (s_gender = ‘F’)  Not NULL: specifies that a field cannot be NULL  Example: CONSTRAINT my_students_s_dob_nn NOT NULL Types of Value Constraints

52  Default: specifies a default value that is inserted automatically  Example: s_state CHAR(2) DEFAULT ‘WI’  Unique  Table constraint  Specifies that a non-primary key field must have a unique value CONSTRAINT consultant_c_ _uk UNIQUE (c_ ) Types of Value Constraints

53  Oracle SQL command line utility for issuing SQL commands  Starting SQL*Plus SQL*Plus

54  All commands must be terminated with a semicolon  Use a text editor and copy and paste commands  Character data is case sensitive and must be in single quotes ‘M’ ‘Sarah’ Using SQL*Plus

55  Type exit at SQL> prompt or  Click Close button on SQL*Plus window Exiting SQL*Plus

56  Ora.hlp file  Oracle Technology Network (OTN)  Oracle Help Resources

57  Viewing a table’s structure DESCRIBE table_name; Viewing Table Information

58 Oracle Data Dictionary Contains tables that describe the database structure Is in the SYSTEM user schema Is automatically updated as users create and modify tables Cannot be updated directly Contains views that allow users to retrieve information about the database structure

59 Data Dictionary Views Views present data in different formats depending on the privileges of the user USER: shows all objects belonging to the current user ALL: shows all objects belonging to the current user, as well as objects current user has privileges to manipulate DBA: allows users with DBA privileges to view objects of all database users

60 Querying the Data Dictionary Views Syntax: SELECT field1, field2, … FROM privilege_viewname;

61 Summary of Oracle Data Dictionary Views OBJECTSAll database objects TABLESDatabase tables INDEXESTable indexes created to improve query performance VIEWSDatabase views SEQUENCESSequences created to automatically generate surrogate key values USERSDatabase users CONSTRAINTSTable constraints CONS_CONSTRAINTSTable columns that have constraints IND_COLUMNSIndexed columns TAB_COLUMNSAll table columns

62  Unrestricted actions 1.Renaming tables  GF: Rename Old_tableName To new_tableName;  Ex  Rename Faculty To New_Faculty Modifying Tables

63 ALTER TABLE T_name Modifying Tables Action Rename Add ModifyDrop E / D F_old_name To F_new_name (Field_name DataType [Constraint]) (Field_name TYPE ) New_one Decrease_size Increase_size Column Field_name Constraint Const_name Enable Const_name Disable Const_name Constraint Cons_specif

64  Unrestricted actions 2.Adding new columns  GF: ALTER TABLE table_name ADD (Field Data type Constraint);  Ex: ALTER Table Faculty Add (Start_date date); Modifying Tables

65  Unrestricted actions 3.Increasing column sizes  ALTER TABLE table_name MODIFY (column new_datType); 4.Dropping columns  ALTER TABLE table_name DROP COLUMN column_name; 5.Dropping constraints  ALTER TABLE table_name DROP COSRAINT constraint_name; Modifying Tables

66  Restricted actions  Dropping tables  Only allowed if table does not contain any fields that are referenced as foreign keys, or if foreign key constraints are dropped  Changing a column’s data specification  Only allowed if existing data is compatible with new data specification  Decreasing column sizes  Only allowed if column does not contain any data  Adding constraints  Only allowed if existing data meets requirements of new constraint Modifying Tables

67 Altering Tables Adding a new field: ALTER TABLE tablename ADD (fieldname field_specification);

68 Altering Tables Modifying an existing field: ALTER TABLE tablename MODIFY (fieldname new_field_specification);

69 Altering Tables Deleting an existing field: ALTER TABLE tablename DROP COLUMN fieldname;

70 Altering Tables Enabling and Disabling Constraints: ALTER TABLE tablename Enable|Disable CONSTRAINT constraint_name; E.g: Alter Table faculty Disable Constraint faculty_f_id_fk;

71 Deleting Tables Syntax to delete table if no table fields are referenced as foreign keys: DROP TABLE tablename;  Syntax to delete table and constraints if table contains fields that are referenced as foreign keys: DROP TABLE tablename CASCADE CONSTRAINTS;