SQL Creating and Managing Tables

Slides:



Advertisements
Similar presentations
9 Copyright © 2004, Oracle. All rights reserved. Using DDL Statements to Create and Manage Tables.
Advertisements

9 Creating and Managing Tables. Objectives After completing this lesson, you should be able to do the following: Describe the main database objects Create.
Data Definition Language (DDL)
Creating Tables. 2 home back first prev next last What Will I Learn? List and provide an example of each of the number, character, and date data types.
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.
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.
1 Structured Query Language (SQL). 2 Contents SQL – I SQL – II SQL – III SQL – IV.
Lecture 2: Using DDL Statements to Create and Manage Tables & Indexes
About the Presentations The presentations cover the objectives found in the opening of each chapter. All chapter objectives are listed in the beginning.
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.
10 Copyright © Oracle Corporation, All rights reserved. Including Constraints.
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.
Chapter 2 Views. Objectives ◦ Create simple and complex views ◦ Creating a view with a check constraint ◦ Retrieve data from views ◦ Data manipulation.
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.
10 Copyright © Oracle Corporation, All rights reserved. Including Constraints.
Database Lab Lecture 1. Database Languages Data definition language ( DDL ) Data definition language –defines data types and the relationships among them.
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.
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.
Database Programming Sections 9 & 10 – DDL Data Definition Language,
Chapter 3 Table Creation and Management Oracle 10g: SQL.
Installation Oracle 11g Express 2 double click the "setup" button to install the Oracle.
 CONACT UC:  Magnific training   
Physical Model Lecture 11. Physical Data Model The last step is the physical design phase, In this phase data is – Store – Organized and – Access.
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.
2 Copyright © 2009, Oracle. All rights reserved. Managing Schema Objects.
Managing Tables, Data Integrity, Constraints by Adrienne Watt
Using DDL Statements to Create and Manage Tables
Including Constraints
Insert, Update and the rest…
SQL Creating and Managing Tables
Manipulating Data Schedule: Timing Topic 40 minutes Lecture
Using DDL Statements to Create and Manage Tables
SQL Creating and Managing Tables
Chapter 5 Sequences.
Chapter 4 Indexes.
CH 4 Indexes.
Chapter 2 Views.
SQL Statements SELECT INSERT UPDATE DELETE CREATE ALTER DROP RENAME
CH 4 Indexes.
Manipulating Data.
Oracle Data Definition Language (DDL)
Using DDL Statements to Create and Manage Tables
Chapter 2 Views.
Ch 3 Synonym.
Using DDL Statements to Create and Manage Tables
IST 318 Database Administration
Chapter 3 Synonym.
Creating and Managing Tables
Including Constraints
Presentation transcript:

SQL Creating and Managing Tables lecture4

Database Objects Database Objects Description Table Basic unit of storage; composed of rows and columns View Logically represents subsets of data from one or more tables Sequence Numeric value generator Index Improves the performance of some queries Synonym Gives alternative names to the other objects

Database Objects All the previews database objects can be created using CREATE sql statement

Tables Creating tables The alter statement Dropping table

Creating table: Syntax Database tables created using (CREATE TABLE statement) The Minimal Syntax is: You Specify: tablename : The name you chooses for the table column : Column name data type : column data type which specifies the type of data to be stored in this column and column size DEFAULT expr: specifies a default value if a value is omitted in the INSERT statement CREATE TABLE tablename (column data type [DEFAULT expr], …..);

Creating table: Naming Rules Table names and column names: Must begin with a letter Must be 1 to 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 Note: Names are not case sensitive. (For example, EMPLOYEES is treated as the same name as eMPloyees or eMpLOYEES.)

Creating table: Naming Guideline Table names and column names: Use a descriptive names for table as well the other database objects Names are not case sensitive. (For example, EMPLOYEES is treated as the same name as eMPloyees or eMpLOYEES.)

Creating table: Data Types

Creating table: Data Types (Cont.)

Creating table: DEFAULT Specify default value for the column during the INSERT operation  Prevents NULL values from entering the columns if the row entered inserted without a value for the column For the DEFAULT : Literal values, expressions, or SQL functions are legal values Another column’s name or pseudocolumn are illegal values The DEFAULT value data type must match the column data type

Creating table: Example Crete a Table CREATE TABLE dept (dnum NUMBER(2), dname VARCHAR2(14),dlocation VARCHAR2(13)); Table created  Appears when you run the query To Confirm the creation DESCRIBE dept ;

The ALTER TABLE Statement Use the ALTER TABLE statement to: • Add a new column • Modify an existing column • Define a default value for the new column • Drop a column

The ALTER TABLE Statement: Syntax Use the ALTER TABLE statement to add, modify or drop columns. ALTER TABLE table ADD (column datatype [DEFAULT expr] [, column datatype]...); MODIFY (column datatype [DEFAULT expr] DROP COLUMN column;

The ALTER TABLE Statement: Syntax

The ALTER TABLE Statement: Example (Add) Use the ADD clause to add columns. ALTER TABLE dept80 ADD (job_id VARCHAR2(9)); •Note That You cannot specify where the new column appear The new column becomes the last column directly. If the table already contains rows when the new column added, then the new column is initialized to NULL (as in this example) or default value (if specified) for all the rows

The ALTER TABLE Statement: Example (Add)

The ALTER TABLE Statement: Example (Modify) You can change a column’s data type, size, and default value. ALTER TABLE dept80 MODIFY (last_name VARCHAR2(30)); Note That: A change to the default value affects only subsequent insertions to the table 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.

The ALTER TABLE Statement: Example (DROP) Use the DROP COLUMN clause to drop columns you no longer need from the table. ALTER TABLE dept80 DROP COLUMN job_id; • Note that: The column may or may not contain data. Using the ALTER TABLE statement, only one column can be dropped at a time. The table must have at least one column remaining in it after it is altered. Once a column is dropped, it cannot be recovered

Dropping a Table • All data and structure in the table is deleted. • You cannot roll back the DROP TABLE statement. DROP TABLE dept80;

Truncating a Table • The TRUNCATE TABLE statement: Removes all rows from a table Releases the storage space used by that table TRUNCATE TABLE detail_dept; •Note That You cannot roll back row removal when using TRUNCATE. Alternatively, you can remove rows by using the DELETE statement. (Later) If the table is the parent of a referential integrity constraint, you cannot truncate the table. Disable the constraint before issuing the TRUNCATE statement.