Creating Database Tables © Abdou Illia MIS 4200 - Spring 2015 1/21/2015.

Slides:



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

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.
Introduction to Structured Query Language (SQL)
Sanjay Goel, School of Business, University at Albany, SUNY 1 SQL- Data Definition Language ITM 692 Sanjay Goel.
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.
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.
Introduction to SQL  SQL or sequel  It is a standardised language with thousands of pages in the standard  It can be in database system through GUI,
1 Chapter 2: Creating and Modifying Database Tables.
Oracle Data Definition Language (DDL)
A Guide to SQL, Eighth Edition Chapter Three Creating Tables.
Chapter 9 SQL and RDBMS Part C. SQL Copyright 2005 Radian Publishing Co.
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.
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.
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 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.
7 1 Chapter 7 Introduction to Structured Query Language (SQL) Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel.
SQL: DDL John Ortiz Cs.utsa.edu.
9 Copyright © Oracle Corporation, All rights reserved. Creating and Managing Tables.
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.
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.
Advanced Database CS-426 Week 1 - Introduction. Database Management System DBMS contains information about a particular enterprise Collection of interrelated.
Introduction to Databases Queries CS 146. Sample Database: CANDY_CUSTOMER CANDY_PURCHASE CANDY_CUST_TYPE CANDY_PRODUCT.
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.
# 1# 1 Creating Tables, Setting Constraints, and Datatypes What is a constraint and why do we use it? What is a datatype? What does CHAR mean? CS 105.
Lecture5: SQL Overview, Oracle Data Type, DDL and Constraints Ref. Chapter6 Lecture4 1.
IS 380 Introduction to SQL This lectures covers material from: database textbook chapter 3 Oracle chapter: 3,14,17.
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.
1 CS 430 Database Theory Winter 2005 Lecture 11: SQL DDL.
Academic Year 2015 Autumn. MODULE CC2006NI: Data Modelling and Database Systems Academic Year 2015 Autumn.
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.
Lecture # 24 Introduction to SQL Muhammad Emran Database Systems.
Physical Model Lecture 11. Physical Data Model The last step is the physical design phase, In this phase data is – Store – Organized and – Access.
CIT 214 Introduction to Database Management
Guide to Oracle 10g Chapter 2: Creating and Modifying Database Tables.
Managing Tables, Data Integrity, Constraints by Adrienne Watt
SQL: Schema Definition and Constraints Chapter 6 week 6
SQL data definition using Oracle
Oracle Data Definition Language (DDL)
Session - 6 Sequence - 1 SQL: The Structured Query Language:
Chapter 2: Creating And Modifying Database Tables
Chapter # 7 Introduction to Structured Query Language (SQL) Part I.
Introduction to Oracle
SQL (Structured Query Language)
Presentation transcript:

Creating Database Tables © Abdou Illia MIS Spring /21/2015

2 Objectives  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  View information about your database tables using Oracle 10g data dictionary views

3 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, SQL-2003, and SQL-2008 Most recent versions Most vendors do not fully comply with SQL-2008 (but comply with SQL-92, SQL-99) –Basic categories for SQL commands Data definition language (DDL) Data manipulation language (DML) CH2:42-58

4 Personal DBMS  With personal DBMS … –You are usually the only user –You start the database application –You create a new database –The DBMS saves the database file in your workstation’s file system –You create database objects (tables, etc.)  Examples: –MS Access –Oracle Personal edition. But this DBMS derives most of its features from the Oracle C/S version CH2:42-58

5 Oracle 10g Enterprise edition  A Client/server DBMS  User account –Created for each user –Identified using unique username and password  User schema –Area of database belonging to user  Database objects –Example: tables, forms, reports, … –Also called schema objects –Objects created by user reside in their user schema CH2:42-58

6 Defining Oracle Database Tables  Tables –Primary data objects in relational database –When you create a new table, you must specify… Table name ● Each field (i.e. column) name Each field data type/size ● You may specify constraints  Constraints –Restrictions on data values that column can store  Oracle naming standard –Rules established by Oracle corp. for naming objects –One to 30 characters long –Contain letters, numbers, and special symbols $, _, and # –Begin with character –Example: S_ID, f_id, PRICE, PRICE$, Not #ITEM. CH2:42-58

7 Defining Oracle Database Tables (cont.)  CREATE TABLE SQL syntax CREATE TABLE tablename (columnname1 data_type, columnname2 data_type, …);  Example: CREATE TABLE student (s_id CHAR(5), s_first VARCHAR2(20));  Basic data types Character ● Number ● Date/time ● Large object CH2:42-58

8 Character Data Types  VARCHAR2 –Variable-length character data (up to 4000 characters) –Syntax: columnname VARCHAR2(maximum_size) –If user enters data value less than maximum_size, DBMS only stores actual character values  CHAR –Fixed-length character data (default = 2000) –Syntax: columnname CHAR(maximum_size) –If user enters data value less than maximum_size, DBMS adds trailing blank spaces to the end of entry  Oracle stores CHAR and VARCHAR2 data using the ASCII coding Q: s_last CHAR(20) was used to define the data type for s_last in the Student table. How many characters will Oracle save to the disk if the user enters illia as the student’s last name?

9 Character Data Types (continued)  Unicode coding –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 CH2:42-58

10 Number Data Types  NUMBER –Used for all numeric data –Syntax columnname NUMBER [([precision,] [scale])]  Example: –s_balance NUMBER (5,2) –s_gpa NUMBER (3,2) # of digits both to left and right of decimal point # of digits on the right side of decimal point CH2:42-58

11 Number Data subtypes  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)  Floating-point number –Contains variable number of decimal places –Decimal point may appear anywhere (.005, 2.34, etc.) –Syntax: columnname NUMBER –Example: s_gpa NUMBER Just the precision. No scale specified No precision, no scale CH2:42-58

12 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 CH2:42-58

13 Date And Time Data Types (continued)  DATE –Stores dates from Dec 31, 4712 BC to Dec 31, AD 4712 –Default date format: DD-MON-YY –Default time format: HH:MI:SS AM –Syntax: columnname DATE  TIMESTAMP –Stores date values similar to DATE data type –Also stores fractional seconds –Syntax: columnname TIMESTAMP (fractional_seconds_precision) –Example : shipment_date TIMESTAMP(2) If omitted, default is 6 decimal place

14 Date And Time Data Types (continued)  INTERVAL YEAR TO MONTH –Stores time interval expressed in years and months using the following syntax: +|– elapsed_years-elapsed_months –Example: specifies 2 years and 11 months –Example: time_enrolled INTERVAL YEAR TO MONTH CH2:42-58

15 Date And Time Data Types (continued)  INTERVAL DAY TO SECOND –Stores time interval expressed in days, hours, minutes, and seconds using the following syntax: Columnname INTERVAL DAY [(leading_precision)] TO SECOND [(fractional_seconds_precision)] Max allowed number of digits used to express the elapsed days Max allowed number of digits used to express the elapsed seconds CH2:42-58

16 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

17 Constraints  Table constraint –Restricts data value with respect to all other values in table like primary key must be unique, not NULL.  Column constraint –Limits value that can be placed in specific column –Irrespective of values that exist in other table rows  Types of constraints: –Integrity constraints –Value constraints CH2:42-58

CREATE TABLE location (loc_id NUMBER(6) CONSTRAINT location_loc_id_pk PRIMARY KEY), Room VARCHAR2(6); 18 Constraints (cont.)  Constraint naming convention –tablename_columnname_constraintid  Constraint definitions should be placed either: –At end of CREATE TABLE command after table columns declared –Within each column definition  Example: CREATE TABLE location (loc_id NUMBER(6), Room VARCHAR2(6), CONSTRAINT location_loc_id_pk PRIMARY KEY (loc_id); Q: Is there any syntax error (or something missing in the above CREATE TABLE statements?

19 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) CREATE TABLE location (loc_id NUMBER(6), Room VARCHAR2(6), CONSTRAINT location_loc_id_pk PRIMARY KEY (loc_id)); CREATE TABLE location (loc_id NUMBER(6) CONSTRAINT location_loc_id_pk PRIMARY KEY), Room VARCHAR2(6)); CH2:42-58

20 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) –Example of foreign key defined in the Faculty table: CONSTRAINT faculty_loc_id_fk FOREIGN KEY (loc_id) REFERENCES location (loc_id) CH2:42-58

21 Integrity Constraints (continued)  Foreign key (continued) –Syntax (placed within table definition) CONSTRAINT constraint_name REFERENCES primary_key_tablename (primary_key_columnname) –Example: loc_id NUMBER(6) CONSTRAINT faculty_loc_id_fk REFERENCES location (loc_id) CH2:42-58

22 Integrity Constraints (continued)  Composite key –Syntax CONSTRAINT constraint_name PRIMARY KEY (columnname1, columnname2 …) –Example: CREATE TABLE enrollment (s_id NUMBER(5) CONSTRAINT enrollment_s_id_fk REFERENCES student(s_id), c_sec_id NUMBER(8) CONSTRAINT enrollment_c_sec_id_fk REFERENCES course_section(c_sec__id), CONSTRAINT enrollment_s_id_c_sec_id_pk PRIMARY KEY (s_id, c_sec_id)); CH2:42-58

23 Value Constraints  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 CONSTRAINT student_s_class_cc CHECK ((s_class = ‘FR’) OR (s_class = ‘SO’) OR (s_class = ‘JR’) OR (s_class = ‘SR’)) CONSTRAINT course_credits_cc CHECK ((credits > 0) AND (credits < 12)) S_last VARCHAR2(30) CONSTRAINT student_s_last_nn NOT NULL s_state CHAR(2) DEFAULT ‘FL’