Database Lab Lecture 1. Database Languages Data definition language ( DDL ) Data definition language –defines data types and the relationships among them.

Slides:



Advertisements
Similar presentations
9 Creating and Managing Tables. Objectives After completing this lesson, you should be able to do the following: Describe the main database objects Create.
Advertisements

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.
9 Copyright © Oracle Corporation, All rights reserved. Creating and Managing Tables.
10 Copyright © Oracle Corporation, All rights reserved. Including Constraints.
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.
8 Copyright © Oracle Corporation, All rights reserved. Manipulating Data.
Introduction to Structured Query Language (SQL)
Chapter 5 Data Manipulation and Transaction Control Oracle 10g: SQL
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.
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.
Lecture 2: Using DDL Statements to Create and Manage Tables & Indexes
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.
10 Copyright © Oracle Corporation, All rights reserved. Including Constraints.
Objectives After completing this lesson, you should be able to do the following: Describe each data manipulation language (DML) statement Insert rows.
10-1 Copyright  Oracle Corporation, All rights reserved. Database Objects ObjectDescription TableBasic unit of storage; composed of rows and columns.
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.
Session 2: SQL (A): Parts 1 and 2 Original materials supplied by the Oracle Academic Initiative (OAI). Edited for classroom use by Professor Laku Chidambaram.
Database Systems Design, Implementation, and Management Coronel | Morris 11e ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or.
11-1 Copyright  Oracle Corporation, All rights reserved. What Are Constraints? Constraints enforce rules at the table level. Constraints prevent.
Tables and Constraints Oracle PL/SQL. Datatypes The SQL Data Definition Language Commands (or DDL) enable us to create, modify and remove database data.
1 SQL - II Data Constraints –Applying data constraints Types of data constraints –I/O constraints The PRIMARY KEY constraints The FOREIGN KEY constraints.
Chapter 9 Constraints. Chapter Objectives  Explain the purpose of constraints in a table  Distinguish among PRIMARY KEY, FOREIGN KEY, UNIQUE, CHECK,
Oracle 11g: SQL Chapter 4 Constraints.
10 Copyright © Oracle Corporation, All rights reserved. Including Constraints.
Copyright  Oracle Corporation, All rights reserved. 11 Including Constraints.
Copyright  Oracle Corporation, All rights reserved. Introduction.
Copyright  Oracle Corporation, All rights reserved. 4 Introduction.
Chapter 4 Constraints Oracle 10g: SQL. Oracle 10g: SQL 2 Objectives Explain the purpose of constraints in a table Distinguish among PRIMARY KEY, FOREIGN.
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.
INCLUDING CONSTRAINTS lecture5. Outlines  What are Constraints ?  Constraint Guidelines  Defining Constraint  NOT NULL constraint  Unique constraint.
11 Including Constraints Objectives At the end of this lesson, you will be able to: Describe constraints Create and maintain constraints At the.
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.
Copyright © 2004, Oracle. All rights reserved. Lecture 2: Using DDL Statements to Create and Manage Tables & Indexes ORACLE.
Altering Tables and Constraints Database Systems Objectives Add and modify columns. Add, enable, disable, or remove constraints. Drop a table. Remove.
DDL and Views. Database Objects Logically represents subsets of data from one or more tables View Generates numeric valuesSequence Basic unit of storage;
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)
Including Constraints. What Are Constraints? Constraints enforce rules at the table level. You can use constraints to do the following: – Enforce rules.
Installation Oracle 11g Express 2 double click the "setup" button to install the Oracle.
SQL Statements SELECT INSERTUPDATEDELETECREATEALTERDROPRENAMETRUNCATECOMMITROLLBACKSAVEPOINTGRANTREVOKE Data Retrieval Language (DRL) Data Retrieval Language.
Creating and Managing Tables. Database Objects ObjectDescription TableBasic unit of storage; composed of rows and columns ViewLogically represents subsets.
Oracle 11g: SQL Chapter 5 Data Manipulation and Transaction Control.
Insert, update, delete TCL. Data Manipulation Language – A DML statement is executed when you: Add new rows to a table Modify existing rows in a table.
Using DDL Statements to Create and Manage Tables
Including Constraints
SQL: Schema Definition and Constraints Chapter 6 week 6
SQL Creating and Managing Tables
Manipulating Data.
SQL Creating and Managing Tables
SQL Creating and Managing Tables
“Manipulating Data” Lecture 6.
“Manipulating Data” Lecture 6.
Manipulating Data.
Oracle Data Definition Language (DDL)
1 Manipulating Data. 2 Objectives After completing this lesson, you should be able to do the following:  Describe each data manipulation language (DML)
Including Constraints
Presentation transcript:

Database Lab Lecture 1

Database Languages Data definition language ( DDL ) Data definition language –defines data types and the relationships among them Data manipulation language ( DML )Data manipulation language –performs tasks such as inserting, updating, or deleting data occurrences Query language –allows searching for information and computing derived information

SQl Statements Data retrievalSelect Data manipulation language (DML) INSERT UPDATE DELETE Data Definition language (DLL) CREATE ALTER DROP RENAME TRUNCATE Transaction ControlCOMMIT ROLLBACK SAVEPOINT

Tables in the Oracle Database 1. User Tables: Are a collection of tables created and maintained by the user Contain user information 2.Data Dictionary: Is a collection of tables created and maintained by the Oracle Server Contain database information

Create Table The simplified syntax for the CREATE TABLE statement is as follows: CREATE TABLE table_name ( column_name type [DEFAULT default_exp CONSTRAINT constraint_def] [, column_name type [DEFAULT default_exp CONSTRAINT constraint_def]...] ) ;

Create Table table_name specifies the name you assign to the table. column_name specifies the name you assign to a column. type specifies the type of a column. constraint_def specifies the definition of a constraint on a column. default_exp specifies the expression used to assign a default value to a column.

Naming Rules Table names and column names: – Must begin with a letter – Must be 1–30 characters long – Must contain only A–Z, a–z, 0–9, _, $, and # – Must not duplicate the name of another object –owned by the same user – Must not be an Oracle server reserved word

Data Types DescriptionData type Variable-length character dataVARCHAR2(size) Fixed-length character dataCHAR(size) Variable-length numeric dataNUMBER(p,s) Date and time valuesDATE Binary data up to 4 gigabytesBLOB

The DEFAULT Option Specify a default value for a column during an insert. Literal values, expressions, or SQL functions are legal values. The default data type must match the column data type. ….. Salary number ( 6,2) default 0 …..

Dropping a Table All data and structure in the table is deleted. You cannot roll back the DROP TABLE statement. Syntax: –Drop Table table_name;

Data Dictionary See the names of tables owned by the user. –SELECT table_name FROM user_tables ; –SELECT * FROM Tab; View tables, views, synonyms, and sequences owned by the user. –Select * from cat;

What are Constraints? Constraints enforce rules at the table level. The following constraint types are valid: – NOT NULL – UNIQUE – PRIMARY KEY – FOREIGN KEY – CHECK

Constraint Guidelines Name a constraint or the Oracle server generates a name by using the SYS_Cn format. Create a constraint either: – At the same time as the table is created, or – After the table has been created Define a constraint at the column or table level. View a constraint in the data dictionary. SELECT * FROM USER_CONSTRAINTS;

Defining Constraints Column constraint level Table constraint level In the syntax: constraint_name is the name of the constraint constraint_type is the type of the constraint column [CONSTRAINT constraint_name] constraint_type, column,... [CONSTRAINT constraint_name] constraint_type (column,...),

The NOT NULL Constraint Ensures that null values are not permitted for the column The NOT NULL constraint can be specified only at the column level, not at the table level. Example : Salary varchar2(12) CONSTRAINT sal_nn not null,

The UNIQUE Constraint A UNIQUE key integrity constraint requires that every value in a column or set of columns (key) be unique. UNIQUE constraints allow the input of nulls unless you also define NOT NULL constraints for the same columns Defined at either the table level or the column level Column constraint level - Example Table constraint level- Example varchar2(12) CONSTRAINT _un unique, ……., CONSTRAINT _un unique ( ),

The PRIMARY KEY Constraint The PRIMARY KEY constraint is a column or set of columns that uniquely identifies each row in a table. Only one primary key can be created for each table. Defined at either the table level or the column level. A composite primary key must be created by using the table-level definition. Column constraint level - Example Table constraint level- Example Empno varchar2(12) CONSTRAINT Emp_no_pk primary key, ……., CONSTRAINT sections_pk primary key (course_no,section_no),

The FOREIGN KEY Constraint FOREIGN KEY constraints can be defined at the column or table constraint level A composite foreign key must be created by using the table-level definition. Column constraint level - Example Table constraint level- Example …. department_id NUMBER(4) CONSTRAINT emp_deptid_fk REFERENCES departments(department_id), …. ……., CONSTRAINT emp_dept_fk FOREIGN KEY (department_id) REFERENCES departments(department_id), ……..

FOREIGN KEY Constraint Keywords FOREIGN KEY: Defines the column in the child table at the table constraint level REFERENCES: Identifies the table and column in the parent table ON DELETE CASCADE: Deletes the dependent rows in the child table when a row in the parent table is deleted. ON DELETE SET NULL: Converts dependent foreign key values to null

Check Constraint The CHECK constraint defines a condition that each row must satisfy. CHECK constraints can be defined at the column level or table level. Column constraint level - Example Table constraint level- Example …. salary NUMBER(8,2) CONSTRAINT emp_salary_min CHECK (salary > 0),…. …. CONSTRAINT emp_salary_min CHECK (salary > 0),….

Comparison Operators MeaningOperator Greater Than > Less Than < Less Than or equal to <= Greater Than or equal to >= Equal to = Not Equal to <>

Logical Operators MeaningOperator Returns TRUE if both component conditions are trueAnd Returns TRUE if either component condition is trueOR Returns TRUE if the following condition is falseNOT

Renaming a Table You rename a table using the RENAME statement RENAME old_table_name TO new_table_name; RENAME EMPLOYESS TO EMP;

Alter Table You can use ALTER TABLE to perform tasks such as: –Add, modify, or drop a column –Add or drop a constraint. Add Column : Alter Table TABLE_NAME Add COLUMN_DEFINATION; Alter Table EMP Add Birth_date Date;

DROP Column : Alter Table TABLE_NAME DROP COLUMN COLUMN_NAME; Alter Table EMP DROP COLUMN Birth_date;

Modify Column –You can change a column’s data type, size, and default value. –Guidelines: You can increase the width or precision of a numeric column. You can increase the width of numeric or character columns. You can decrease the width of a column only if the column contains only null values or if the table has no rows. You can change the data type only if the column contains null values. You can convert a CHAR column to the VARCHAR2 data type or convert a VARCHAR2 column to the CHAR data type only if the column contains null values or if you do not change the size. A change to the default value of a column affects only subsequent insertions to the table.

Modify Column Alter Table TABLE_NAME MODIFY COLUMN_Def;

Adding a Constraint Syntax Use the ALTER TABLE statement to: –Add or drop a constraint, but not modify its structure Enable or disable constraints –Add a NOT NULL constraint by using the MODIFY clause ALTER TABLE table_name ADD [CONSTRAINT constraint] constraint_type (columns); ALTER TABLE table_name DROP CONSTRAINT constraint_name;

Data Manipulation Language (DML) A DML statement is executed when you: – Add new rows to a table – Modify existing rows in a table – Remove existing rows from a table A transaction consists of a collection of DML statements that form a logical unit of work.

The INSERT Statement Syntax Add new rows to a table by using the INSERT statement. Only one row is inserted at a time with this syntax. INSERT INTO table [(column [, column...])] VALUES (value [, value...]);

Inserting New Rows Insert a new row containing values for each column. List values in the default order of the columns in the table. Optionally, list the columns in the INSERT clause. Enclose character and date values within single quotation marks. The DD-MON-YY format is usually used to insert a date value.

Inserting Rows with Null Values Implicit method: – Omit the column from the column list. Explicit method: – Specify the NULL keyword in the VALUES clause.

Database Transactions Begin when the first DML SQL statement is executed End with one of the following events: – A COMMIT or ROLLBACK statement is issued. – A DDL or DCL statement executes (automatic commit). – The user exits iSQL*Plus. – The system crashes.