Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Tables and Constraints Oracle PL/SQL. Datatypes The SQL Data Definition Language Commands (or DDL) enable us to create, modify and remove database data."— Presentation transcript:

1 Tables and Constraints Oracle PL/SQL

2 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.

3 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.

4 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

5 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, 1234.567 will be stored as 1234.57. 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, 1234.567 will be stored as 1234.57.

6 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'.

7 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;

8 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

9 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) );

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

11 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));

12 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;

13 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

14 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));

15 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));

16 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));

17 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 );

18 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 );

19 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));

20 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;

21 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, '630- 666-7777'); 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, '630- 666-7777'); INSERT INTO my_product SELECT * FROM product;

22 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);

23 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;

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


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

Similar presentations


Ads by Google