Tables and Constraints Oracle PL/SQL. Datatypes The SQL Data Definition Language Commands (or DDL) enable us to create, modify and remove database data.

Slides:



Advertisements
Similar presentations
Data Definition Language (DDL)
Advertisements

Virtual training week 4 structured query language (SQL)
Copyright © by Royal Institute of Information Technology Introduction To Structured Query Language (SQL) 1.
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.
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.
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.
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.
30-Jun-15 SQL A Brief Introduction. SQL SQL is Structured Query Language Some people pronounce SQL as “sequel” Other people insist that only “ess-cue-ell”
Structured Query Language. Brief History Developed in early 1970 for relational data model: –Structured English Query Language (SEQUEL) –Implemented with.
Database Management System LICT 3011 Eyad H. Elshami.
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,
Oracle Data Definition Language (DDL)
Structured Query Language S Q L. What is SQL It is a database programming language developed by IBM in the early 1970’s. It is used for managing and retrieving.
A Guide to SQL, Eighth Edition Chapter Three Creating Tables.
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.
Chapter Four Objectives Introduction to SQL Types of SQL statements Concepts of DDL & DML Creating Tables Restrictions on Tables Data Definition Language(DDL)
Structured Query Language. Brief History Developed in early 1970 for relational data model: –Structured English Query Language (SEQUEL) –Implemented with.
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.
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.
ACTION QUERIES (SQL COMMANDS ) STRUCTURED QUERY LANGUAGE.
SQL FUNDAMENTALS SQL ( Structured Query Language )
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.
SQL: DDL John Ortiz Cs.utsa.edu.
SQL: DDL. SQL Statements DDL - data definition language –Defining and modifying data structures (metadata): database, tables, views, etc. DML - data manipulation.
Topic 1: Introduction to SQL. SQL stands for Structured Query Language. SQL is a standard computer language for accessing and manipulating databases SQL.
SQL Fundamentals  SQL: Structured Query Language is a simple and powerful language used to create, access, and manipulate data and structure in the database.
SQL Basics. What is SQL? SQL stands for Structured Query Language. SQL lets you access and manipulate databases.
Database Lab Lecture 1. Database Languages Data definition language ( DDL ) Data definition language –defines data types and the relationships among them.
CREATE TABLE CREATE TABLE statement is used for creating relations Each column is described with three parts: column name, data type, and optional constraints.
Prince Sultan University Dept. of Computer & Information Sciences CS 340 Introduction to Database Systems.
9 Copyright © 2004, Oracle. All rights reserved. Using DDL Statements to Create and Manage Schema Objects.
Visual Programing SQL Overview Section 1.
IMS 4212: Data Manipulation 1 Dr. Lawrence West, MIS Dept., University of Central Florida Additional Data Manipulation Statements INSERT.
Lecture5: SQL Overview, Oracle Data Type, DDL and Constraints Ref. Chapter6 Lecture4 1.
SQL CREATING AND MANAGING TABLES lecture4 1. Database Objects ObjectDescription TableBasic unit of storage; composed of rows and columns ViewLogically.
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.
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)
Database Programming Sections 9 & 10 – DDL Data Definition Language,
CSCI N311: Oracle Database Programming 5-1 Chapter 15: Changing Data: insert, update, delete Insert Rollback Commit Update Delete Insert Statement –Allows.
SQL: Structured Query Language It enables to create and operate on relational databases, which are sets of related information stored in tables. It is.
LECTURE FOUR Introduction to SQL DDL with tables DML with tables.
Physical Model Lecture 11. Physical Data Model The last step is the physical design phase, In this phase data is – Store – Organized and – Access.
Lec-7. The IN Operator The IN operator allows you to specify multiple values in a WHERE clause. SQL IN Syntax SELECT column_name(s) FROM table_name WHERE.
Creating and Managing Tables. Database Objects ObjectDescription TableBasic unit of storage; composed of rows and columns ViewLogically represents subsets.
Fundamentals of DBMS Notes-1.
Managing Tables, Data Integrity, Constraints by Adrienne Watt
SQL: Schema Definition and Constraints Chapter 6 week 6
MIS2502: Data Analytics SQL – Putting Information Into a Database
SQL Creating and Managing Tables
SQL Creating and Managing Tables
SQL Creating and Managing Tables
SQL data definition using Oracle
MIS2502: Data Analytics SQL – Putting Information Into a Database
Oracle Data Definition Language (DDL)
Database Management System
Chapter 2: Creating And Modifying Database Tables
MIS2502: Data Analytics SQL 4– Putting Information Into a Database
SQL (Structured Query Language)
Presentation transcript:

Tables and Constraints Oracle PL/SQL

Datatypes The SQL Data Definition Language Commands (or DDL) enable us to create, modify and remove database data structures. These commands are called: CREATE,ALTER, DROP, RENAME and TRUNCATE. Data must be stored in the right format and should be easy to change and manipulate. The SQL Data Definition Language Commands (or DDL) enable us to create, modify and remove database data structures. These commands are called: CREATE,ALTER, DROP, RENAME and TRUNCATE. Data must be stored in the right format and should be easy to change and manipulate.

CHAR(n) Column length is fixed, where n denotes the number of characters in the column. If you do not specify the length, the default is 1. Maximum length is 2,000. You can include any character on the keyboard. If you specify a column as CHAR(10) and have just six characters of actual data, the remaining four characters are padded with blank spaces. Column length is fixed, where n denotes the number of characters in the column. If you do not specify the length, the default is 1. Maximum length is 2,000. You can include any character on the keyboard. If you specify a column as CHAR(10) and have just six characters of actual data, the remaining four characters are padded with blank spaces.

VARCHAR2(n) The column length is variable, where n denotes the maximum number of characters Minimum length is 1 and maximum length is 4,000. You can include any character on the keyboard. If you specify a column as VARCHAR2(20) and have the 10 characters of actual data, Oracle stores just 10 characters without padding the remaining 10 characters with spaces The column length is variable, where n denotes the maximum number of characters Minimum length is 1 and maximum length is 4,000. You can include any character on the keyboard. If you specify a column as VARCHAR2(20) and have the 10 characters of actual data, Oracle stores just 10 characters without padding the remaining 10 characters with spaces

NUMBER (n, m) Numbers have up to 38 significant digits. The character n denotes the total number of digits (precision) and is optional. If you do not specify a value for n, you can store any number up to 38 digits. The letter m denotes the number of digits to the right of the decimal point (scale). If the scale factor is less than the actual data, Oracle rounds the data to the scale specified for example, if the datatype is number (6, 2), the actual data, will be stored as Numbers have up to 38 significant digits. The character n denotes the total number of digits (precision) and is optional. If you do not specify a value for n, you can store any number up to 38 digits. The letter m denotes the number of digits to the right of the decimal point (scale). If the scale factor is less than the actual data, Oracle rounds the data to the scale specified for example, if the datatype is number (6, 2), the actual data, will be stored as

DATE This stores both date and time. The default format used to specify a date in Oracle is 'DD-MON-YY'; for example, 1st Jan, 2000, will be input as '01-JAN-00'. This stores both date and time. The default format used to specify a date in Oracle is 'DD-MON-YY'; for example, 1st Jan, 2000, will be input as '01-JAN-00'.

The CREATE TABLE Statement CREATE TABLE table_name ( column_name1 datatype [column_level constraint], column_name2 datatype [column_level constraint],... column_nameN datatype [column_level constraint], [table_level constraint] ); CREATE TABLE table_name [(column1, column2,... columnN)] AS SELECT statement; CREATE TABLE table_name ( column_name1 datatype [column_level constraint], column_name2 datatype [column_level constraint],... column_nameN datatype [column_level constraint], [table_level constraint] ); CREATE TABLE table_name [(column1, column2,... columnN)] AS SELECT statement;

naming convention must begin with a letter of the alphabet can be 1-30 characters long can contain a-z, A-Z, 0-9, $, #, and _ (underscore) cannot duplicate the name of another object owned by the same user cannot be an Oracle-reserved word must begin with a letter of the alphabet can be 1-30 characters long can contain a-z, A-Z, 0-9, $, #, and _ (underscore) cannot duplicate the name of another object owned by the same user cannot be an Oracle-reserved word

EXAMPLE CREATE TABLE my_customer (customer_id NUMBER(5), company_name VARCHAR2(32), contact_name VARCHAR2(34), address VARCHAR2(100), city VARCHAR2(32), state CHAR(2), zip NUMBER(5), phone VARCHAR2(12) ); CREATE TABLE my_customer (customer_id NUMBER(5), company_name VARCHAR2(32), contact_name VARCHAR2(34), address VARCHAR2(100), city VARCHAR2(32), state CHAR(2), zip NUMBER(5), phone VARCHAR2(12) );

EXAMPLE 2 CREATE TABLE my_customer2 AS SELECT * FROM customer; CREATE TABLE my_customer2 AS SELECT * FROM customer;

ALTER TABLE The ALTER table command is used to: –ADD, DROP, and MODIFY columns –ADD and DROP constraints –ENABLE or DISABLE constraints ALTER TABLE my_customer ADD (fax VARCHAR2(12)); ALTER TABLE my_customer DROP (phone); ALTER TABLE my_customer MODIFY (fax NUMBER(15)); The ALTER table command is used to: –ADD, DROP, and MODIFY columns –ADD and DROP constraints –ENABLE or DISABLE constraints ALTER TABLE my_customer ADD (fax VARCHAR2(12)); ALTER TABLE my_customer DROP (phone); ALTER TABLE my_customer MODIFY (fax NUMBER(15));

RENAME, TRUNCATE, DROP RENAME my_customer TO my_cust; TRUNCATE TABLE table_name; DROP TABLE table_name; RENAME my_customer TO my_cust; TRUNCATE TABLE table_name; DROP TABLE table_name;

Integrity Constraints Data integrity constraints you can define in Oracle are as follows: –Primary key –Foreign key –Not null –Unique –Check Data integrity constraints you can define in Oracle are as follows: –Primary key –Foreign key –Not null –Unique –Check

PRIMARY KEY CREATE TABLE my_customer (customer_id NUMBER(5) CONSTRAINT my_cust_id_pk PRIMARY KEY, company_name VARCHAR2(32), contact_name VARCHAR2(34), address VARCHAR2(100), city VARCHAR2(32), state CHAR(2), zip NUMBER(5), phone VARCHAR2(12)); CREATE TABLE my_customer (customer_id NUMBER(5) CONSTRAINT my_cust_id_pk PRIMARY KEY, company_name VARCHAR2(32), contact_name VARCHAR2(34), address VARCHAR2(100), city VARCHAR2(32), state CHAR(2), zip NUMBER(5), phone VARCHAR2(12));

PRIMARY KEY CREATE TABLE my_customer (customer_id NUMBER(5), company_name VARCHAR2(32), contact_name VARCHAR2(34), address VARCHAR2(100), city VARCHAR2(32), state CHAR(2), zip NUMBER(5), phone VARCHAR2(12), CONSTRAINT my_cust_id_pk PRIMARY KEY (customer_id)); CREATE TABLE my_customer (customer_id NUMBER(5), company_name VARCHAR2(32), contact_name VARCHAR2(34), address VARCHAR2(100), city VARCHAR2(32), state CHAR(2), zip NUMBER(5), phone VARCHAR2(12), CONSTRAINT my_cust_id_pk PRIMARY KEY (customer_id));

Foreign Key CREATE TABLE my_orders1 (order_id NUMBER(5) PRIMARY KEY, customer_id NUMBER(5) references my_customer(customer_id), order_date DATE, shipment_date DATE, sales_person_id NUMBER(5) CONSTRAINT sales_fk REFERENCES sales_person(sales_person_id)); CREATE TABLE my_orders1 (order_id NUMBER(5) PRIMARY KEY, customer_id NUMBER(5) references my_customer(customer_id), order_date DATE, shipment_date DATE, sales_person_id NUMBER(5) CONSTRAINT sales_fk REFERENCES sales_person(sales_person_id));

Unique Key null values are allowed CREATE TABLE my_orders4 (customer_id NUMBER(5) UNIQUE, order_date DATE, shipment_date DATE); CREATE TABLE my_orders5 (customer_id NUMBER(5) CONSTRAINT cust_uk UNIQUE, order_date DATE, shipment_date DATE ); null values are allowed CREATE TABLE my_orders4 (customer_id NUMBER(5) UNIQUE, order_date DATE, shipment_date DATE); CREATE TABLE my_orders5 (customer_id NUMBER(5) CONSTRAINT cust_uk UNIQUE, order_date DATE, shipment_date DATE );

Not Null CREATE TABLE my_customer2 (customer_id NUMBER(5), company_name VARCHAR2(32), contact_name VARCHAR2(32) NOT NULL, address VARCHAR2(80) CONSTRAINT address_nn NOT NULL ); CREATE TABLE my_customer2 (customer_id NUMBER(5), company_name VARCHAR2(32), contact_name VARCHAR2(32) NOT NULL, address VARCHAR2(80) CONSTRAINT address_nn NOT NULL );

Check Constraint CREATE TABLE my_sales_person2 (sales_person_id NUMBER(5), first_name VARCHAR2(32), last_name VARCHAR2(32), address VARCHAR2(100), gender CHAR(1) CHECK (gender IN ('M', 'F')), phone VARCHAR2(12)); CREATE TABLE my_sales_person2 (sales_person_id NUMBER(5), first_name VARCHAR2(32), last_name VARCHAR2(32), address VARCHAR2(100), gender CHAR(1) CHECK (gender IN ('M', 'F')), phone VARCHAR2(12));

Modifying Constraints ALTER TABLE table_name ADD [CONSTRAINT constraint_name] Constraint_Type (column_name1, column_name2,…) ALTER TABLE my_orders8 ADD CONSTRAINT ship_ck8 CHECK (shipping_type IN ('AIRBORNE')); ALTER TABLE my_orders DISABLE CONSTRAINT ship_ck8; ALTER TABLE my_orders8 DROP CONSTRAINT ship_ck8; ALTER TABLE table_name ADD [CONSTRAINT constraint_name] Constraint_Type (column_name1, column_name2,…) ALTER TABLE my_orders8 ADD CONSTRAINT ship_ck8 CHECK (shipping_type IN ('AIRBORNE')); ALTER TABLE my_orders DISABLE CONSTRAINT ship_ck8; ALTER TABLE my_orders8 DROP CONSTRAINT ship_ck8;

Inserting Rows INSERT INTO my_customer (customer_id, company_name, contact_name, address, city, state, zip, phone) VALUES (301, 'JK Plumbing', 'Mary','1625, Woodward Lane', 'Almond', 'IL', 60517, ' '); INSERT INTO my_product SELECT * FROM product; INSERT INTO my_customer (customer_id, company_name, contact_name, address, city, state, zip, phone) VALUES (301, 'JK Plumbing', 'Mary','1625, Woodward Lane', 'Almond', 'IL', 60517, ' '); INSERT INTO my_product SELECT * FROM product;

Updating Rows UPDATE customer SET address = '101 Main St' WHERE customer_id = 400; UPDATE my_sales_person SET salary = salary + (salary * 0.1); UPDATE customer SET address = '101 Main St' WHERE customer_id = 400; UPDATE my_sales_person SET salary = salary + (salary * 0.1);

Deleting Rows DELETE FROM my_product WHERE product_id = 12; DELETE FROM my_product; DELETE FROM my_product WHERE product_id = 12; DELETE FROM my_product;

Transaction Control Commit Rollback SAVEPOINT –SAVEPOINT savepoint_name; –ROLLBACK to savepoint_name; Commit Rollback SAVEPOINT –SAVEPOINT savepoint_name; –ROLLBACK to savepoint_name;