Database Design lecture 3_1 1 Database Design Lecture 3_1 Data definition in SQL.

Slides:



Advertisements
Similar presentations
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.
Advertisements

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”
SQL DDL constraints Restrictions on the columns and tables 1SQL DDL Constraints.
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.
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.
Chapter 5: Part 1: DDL STRUCTURED QUERY LANGUAGE (SQL)
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 Basics. 5/27/2016Chapter 32 of 19 Naming SQL commands are NOT case sensitive SQL commands are NOT case sensitive But user identifier names ARE case.
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.
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.
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. Introduction.
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.
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.
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.
Week 8-9 SQL-1. SQL Components: DDL, DCL, & DML SQL is a very large and powerful language, but every type of SQL statement falls within one of three main.
©Silberschatz, Korth and Sudarshan1 Structured Query Language (SQL) Data Definition Language Domains Integrity Constraints.
ITS232 Introduction To Database Management Systems Siti Nurbaya Ismail Faculty of Computer Science & Mathematics, Universiti Teknologi MARA (UiTM), Kedah.
>> Introduction to MySQL. Introduction Structured Query Language (SQL) – Standard Database Language – Manage Data in a DBMS (Database Management System)
1 CS 430 Database Theory Winter 2005 Lecture 10: Introduction to SQL.
Altering Tables and Constraints Database Systems Objectives Add and modify columns. Add, enable, disable, or remove constraints. Drop a table. Remove.
Populating and Querying tables Insert, Update, Delete and View (DML)
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.
CDT/1 Creating data tables and Referential Integrity Objective –To learn about the data constraints supported by SQL2 –To be able to relate tables together.
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.
Managing Tables, Data Integrity, Constraints by Adrienne Watt
SQL: Schema Definition and Constraints Chapter 6 week 6
STRUCTURED QUERY LANGUAGE
SQL data definition using Oracle
Oracle Data Definition Language (DDL)
SQL-1 Week 8-9.
Database Management System
Session - 6 Sequence - 1 SQL: The Structured Query Language:
Chapter 2: Creating And Modifying Database Tables
Presentation transcript:

Database Design lecture 3_1 1 Database Design Lecture 3_1 Data definition in SQL

Database Design lecture 3_1 2 SQL Overview  SQL (Structured Query Language) has 3 major components: –A DDL for defining database structure. –A DML for retrieving and updating data. –A DCL for data control, granting of rights etc.  Official pronunciation is ‘S-Q-L'.  Can be used by range of users including DBAs, management, application developers, and other types of end users.  Easy to learn: –Consists of standard English words, case insensitive

Database Design lecture 3_1 3 SQL  An ISO standard now exists for SQL, making it both the formal and de facto standard language for relational databases (Latest version SQL-99).  Supported by many vendors –There are vendor-specific variations  Data types vary from vendor to vendor  Standard SQL data types: see text 8.1  Different conformity to SQL standards  SQL terminology  table, row, column  SQL uses bag-semantics-- a table may contain duplicate rows  We will use Oracle 10g SQL, but omitting most of the complex Oracle-specific details.

Database Design lecture 3_1 4 Oracle Data Types  Basic built-in data types include –Numbers  Number –Character Strings  Char  varchar2 –Date and time  Date –large strings and BLOBs (binary large objects)

Database Design lecture 3_1 5 Number  Syntax for specifying numbers –Number (precision, scale)  Precision is the maximum digits of numbers  Scale specifies the position of decimal point.  Eg  Number(5) 5 digit integer,  Number(6,2) 6 digit (not including decimal point) decimal number with 2 digits after the decimal point,  Can store 1—38 digits precision

Database Design lecture 3_1 6 String  To store strings, you can choose from –Char  stores fixed-length character strings of up to 2000 characters. Eg. char(10)  should use it to store short strings or strings with ‘neat’ lengths –Varchar2  Stores variable-length strings of up to 4000 characters long. Eg. Varchar2(50)  Preferred for most string types –String values are quoted with single quotes  Eg ‘12234’, ‘abcd’, ‘a12’

Database Design lecture 3_1 7 Date and time  Oracle uses the date data type to store both date and time –Always uses 7 bytes for date-time data. –Oracle date has rich formats, you need to specify it SSsecond0-59 MIMinute0-59 HH Hour1-12 HH24Military hour1-24 DDday of month1-31 (depends on month) DAYday of the week Sunday-Saturday Dday of the week1-7 MM month number 1-12 MONmonth abbreviated Jan—Dec Month Month spelled outJanuary-December YYlast 2 digits of yeareg, 98 YYYYfull year valueeg, 1998

Database Design lecture 3_1 8 Date and time  Example of date format: –‘dd-mon-yyyy’ 01-dec-2001 –‘dd/mm/yyyy’ 01/12/2001 –‘mm-dd-yy hh:mi:ss’ :30:59  Default format: ‘dd-mon-yyyy’  Current date and time: –Sysdate  Oracle has built-in functions that converts between data types: eg, date to string, string to date, string to number and number to string.

Database Design lecture 3_1 9 Data Definition  Creating a table create table table-name ( column-name1 datatype [not null], …….. column-nameN datatype [not null]); Table name –can not exceed 30 characters long, –Must begin with an alphabetic character –May contain letters, numbers, $, # and _ –Should not be an oracle reserved word –Should be descriptive

Database Design lecture 3_1 10 Identifying Primary Key create table table-name ( column-name1 datatype [not null], …….. column-nameN datatype [not null], [constraint constraint-name] Primary key (clolumn-nameA,…,column-nameX) ); or create table table-name ( column-name1 datatype [not null] [constraint constraint-name] primary key, …….. column-nameN datatype [not null])

Database Design lecture 3_1 11 Example create table Department ( Dept_nochar(4)PRIMARY KEY, Dept_name varchar2(25)); or create table Department ( dept_nochar(4), Dept_name varchar2(25), CONSTRAINT dept_PK PRIMARY KEY(dept_no));

Database Design lecture 3_1 12 Constraints  Primary key is an example of constraints. Other constraints exist, eg. foreign key, check constraints, null- value constraints.  If you don’t give the constraint name, the system will generate a name automatically, but the name is hard for human understanding.  As seen, primary key constraints (in fact other constraints too) can be specified as row constraints and table constraints. Tables constraints can, for example, identify several columns as the primary key.  Viewing constraints –User constraints are stored in the table user_constraints; –Can use select command to view the constraints.

Database Design lecture 3_1 13 Identifying Foreign Keys create table table-name ( column-name1 datatype [not null] [unique], …….. column-nameN datatype [not null] [unique], [constraint constraint-name1] Primary key (clolumnA,…,columnX), [constraint constraint-name2] Foreign key (Column_list) references referenced_table (column_list_in_referenced_table) ); Create table Staff( staff_no char(3), staff_name varchar2(20), dept_no char(4), Constraint staff_fk foreign key (dept_no) references department (dept_no));

Database Design lecture 3_1 14 More about Foreign Keys  When you delete or update a value of the columns referenced by other tables, the referential integrity constraints may be violated.  You can specify what action to take in such cases –No action or restrict: reject the delete or update (default) –Cascade: cascading the deletion –Set null –Set default

Database Design lecture 3_1 15 Check Constraints  You can use check constraints to limit the values of a column or some columns  Example: Create table staff( Staff_no char(3), Staff_name varchar2(20) not null, Staff_gender char(1) check (staff_gender in (‘M’, ‘F’)), Staff_salary number(8,2) not null, Dept_no char(4), Constraint staff_pk Primary key (staff_no), Constraint staff_fk Foreign key (dept_no) references department (dept_no), Constraint staff_sal check (staff_salary > ));

Database Design lecture 3_1 16 Unique Constraints and Default Values Create table staff( Staff_no char(3), Staff_name varchar2(20) not null, DateofBirth date, Staff_nationality char(10) default ‘Australia’, Staff_salary number(8,2) not null, Dept_no char(4), Constraint staff_pk Primary key (staff_no), Constraint staff_fk Foreign key (dept_no) references department (dept_no) on delete set null, Constraint staff_sal check (staff_salary > ), UNIQUE(staff_name, DateofBirth)); The table can not have two rows having the same values for staff_name and DateofBirth Unlike primary key columns, unique columns are not automatically NOT NULL

Database Design lecture 3_1 17 Modifying Table Definitions Alter table table_name add (column_specification | constraint,..., column_specification| constraint); Alter table table_name modify (column_specification | constraint,..., Column_specification | constraint); Alter table table_name drop column column_name | drop (column_list); Alter table table_name drop primary key; Alter table table_name drop constraint constraint_name;  Compare with standard SQL in text

Database Design lecture 3_1 18 Examples alter table orders add (quantity number (3) not null); alter table orders modify (quantity number(5)); alter table orders drop (quantity);  Be careful if the table contains data.

Database Design lecture 3_1 19 Dropping Tables  Drop table table_name [Cascade Constraints];  Pay attention to referential integrity constraints when dropping tables.  If Cascade Constraints is used, the constraints will be dropped first.  example Drop table Staff; Drop table Department; Drop table Department cascade constraints;

Database Design lecture 3_1 20 Viewing/enabling/disabling/dropping Constraints  To view the constraints defined for table Department, type SELECT constraint_name, constraint_type, search_condition FROM user_constraints WHERE table_name ='DEPARTMENT';  To disable/enable a constraint, use ALTER TABLE Table_name DISABLE CONSTRAINT constraint_name; ALTER TABLE Table_name ENABLE CONSTRAINT constraint_name;  To drop a constraint, use ALTER TABLE Table_name DROP PRIMARY KEY| UNIQUE (column_name) | CONSTRAINT constraint_name;

Database Design lecture 3_1 21 Viewing Tables and Table Structures  To see what tables are there in SQL*plus, type select * from cat;  To see the structure of a table, type describe table_name; or desc table _name;