Creating Tables and Inserting Records -- Not easy to edit! -- check constraints! Create table test1 ( C1 char(5) primary key, C2 Varchar2(15) not null.

Slides:



Advertisements
Similar presentations
MySQL. To start go to Login details: login: labuser password:macimd15 – There.
Advertisements

Relational Database. Relational database: a set of relations Relation: made up of 2 parts: − Schema : specifies the name of relations, plus name and type.
SQL Lecture 10 Inst: Haya Sammaneh. Example Instance of Students Relation  Cardinality = 3, degree = 5, all rows distinct.
Overview Begin 6:00 Quiz15 mins6:15 Review Table Terms25 mins6:40 Short Break10 mins6:50 SQL: Creating Tables60 mins7:50 Break10 mins8:00 Lab – Creating.
Assignment6-1 Assignment6-2 Due Wednesday, March 13 1.
Let’s try Oracle. Accessing Oracle The Oracle system, like the SQL Server system, is client / server. For SQL Server, –the client is the Query Analyser.
Cs3431 Constraints Sections 6.1 – 6.5. cs3431 Example CREATE TABLE Student ( sNum int, sName varchar (20), prof int, CONSTRAINT pk PRIMARY KEY (snum),
1 A GUIDE TO ORACLE8 CHAPTER 2: Creating and ModifyingDatabaseTables 2.
Mid-term Exam Answer Keys Week 7 Spring I. Multiple Choices Questions Each may have more than one answer (6 x 4 = 24 points total)
Relational Model Stores data as tables –Each column contains values about the same attribute –Each column has a distinct name –Each row contains values.
A Guide to SQL, Seventh Edition. Objectives Understand the concepts and terminology associated with relational databases Create and run SQL commands in.
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.
SQL Keys and Constraints Justin Maksim. Key Declaration Key constraint defined within the CREATE TABLE command Key can be declared using either the PRIMARY.
Introduction to Structured Query Language (SQL)
SQL DDL constraints Restrictions on the columns and tables 1SQL DDL Constraints.
A Guide to MySQL 3. 2 Objectives Start MySQL and learn how to use the MySQL Reference Manual Create a database Change (activate) a database Create tables.
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 4-1 David M. Kroenke Database Processing Chapter 7 Structured Query Language.
The Relational Model Codd (1970): based on set theory Relational model: represents the database as a collection of relations (a table of values --> file)
MBAC 611.  We have been using MS Access to query and modify our databases.  MS Access provides a GUI (Graphical User Interface) that hides much of the.
Your Oracle Account UserName is the same as your UWP username Followed Not case sensitive Initial Password: UWPstudent Password is case sensitive.
Oracle Data Definition Language (DDL)
A Guide to SQL, Eighth Edition Chapter Three Creating Tables.
Dr. Chen, Oracle Database System (Oracle) 1 Chapter 4 Constraints Jason C. H. Chen, Ph.D. Professor of MIS School of Business Gonzaga University Spokane,
CS450 HW2Page 1 Homework #2  Due the midnight of April 15th.  100 Points  In this homework, you create an SQL script to build the MOVIES database according.
SQL Data Definition (CB Chapter 6) CPSC 356 Database Ellen Walker Hiram College (Includes figures from Database Systems by Connolly & Begg, © Addison Wesley.
CS 3630 Database Design and Implementation. Your Oracle Account UserName is the same as your UWP username Followed Not case sensitive Initial.
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.
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.
7 1 Chapter 7 Introduction to Structured Query Language (SQL) Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel.
CS 3630 Database Design and Implementation. Assignment 3 Style! Agreement between database designer and the client. UserName1_EasyDrive UserName2_EasyDrive.
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.
Intro to SQL| MIS 2502  Spacing not relevant › BUT… no spaces in an attribute name or table name  Oracle commands keywords, table names, and attribute.
A Guide to MySQL 3. 2 Introduction  Structured Query Language (SQL): Popular and widely used language for retrieving and manipulating database data Developed.
SQL Basics. What is SQL? SQL stands for Structured Query Language. SQL lets you access and manipulate databases.
CS 3630 Database Design and Implementation. Database Schema Branch (Bno…) Staff (Sno…Bno) Owner (Ono…) PropertyForRent (Pno…Ono) Renter (Rno…) Viewing.
Dec 8, 2003Murali Mani Constraints B term 2004: lecture 15.
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.
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.
© 2002 by Prentice Hall 1 David M. Kroenke Database Processing Eighth Edition Chapter 9 Structured Query Language.
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.
ITEC 3220A Using and Designing Database Systems Instructor: Prof. Z. Yang Course Website: 3220a.htm
1 SQL Insert Update Delete Create table Alter table.
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.
CS34311 The Relational Model. cs34312 Why Relational Model? Currently the most widely used Vendors: Oracle, Microsoft, IBM Older models still used IBM’s.
CS 3630 Database Design and Implementation. Null Value The value of an attribute could be NULL NOT known at the moment or NOT Applicable Example Cell.
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.
1 Introduction to SQL *Plus Oracle SQL Interface MIS309 Database Systems.
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.
1 DBS201: More on SQL Lecture 2. 2 Agenda Select command review How to create a table How to insert data into a table.
Constraints and Views Chap. 3-5 continued (7 th ed. 5-7)
Murali Mani Constraints. Murali Mani Keys: Primary keys and unique CREATE TABLE Student ( sNum int, sName varchar (20), dept char (2), CONSTRAINT key.
3 A Guide to MySQL.
SQL: Schema Definition and Constraints Chapter 6 week 6
CS 3630 Database Design and Implementation
ORACLE SQL Developer & SQLPLUS Statements
The Relational Model Relational Data Model
Oracle Data Definition Language (DDL)
SQL-1 Week 8-9.
Chapter 2: Creating And Modifying Database Tables
CS3220 Web and Internet Programming SQL and MySQL
Database Design: Relational Model
CS3220 Web and Internet Programming SQL and MySQL
Instructor: Samia arshad
Presentation transcript:

Creating Tables and Inserting Records -- Not easy to edit! -- check constraints! Create table test1 ( C1 char(5) primary key, C2 Varchar2(15) not null Check (c2 in ('database', 'Windows', 'Web Protocols')), C3 integer Not NULL Check (C3 in (10, 20, 30)), C4 Date); Insert into test1 Values('cs363', 'database', 10, null); Insert into test1 Values('cs387', 'Web Protocols', 15, null); Insert into test1 Values('cs334', ‘windows', 20, ‘ ’); Insert into test1 Values('cs334', ‘Windows', null, ‘ ’); 1

Oracle Editor Debugging individual commands One command at a time! No semicolon at the end 2

Setting editfile to Use Oracle Editor Show editfile SQL> edit (return) Not working! No rights in the working folder Set editfile J:\CS363\DebugLab7.sql SQL> edit (return) It’s working! 3

Using Oracle Editor Using Notepad Save file after editing SQL command SQL> run SQL> / Run the last command! 4

SQL Script File Using any text editor outside SQL*Plus File extension.SQL UserName_Lab7.Sql Multiple SQL commands Each command ends with ; 5

Running Script File Using either Start SQL> Start file_name_with_full_path Use Arrow Keys to get previous commands! 6

Oracle Editor and Script File Use Oracle editor to debug individual commands Copy and paste to your script file Add semicolon at the end of each command Run the script file Drop your script file for Assignment7 7

Table with Foreign Keys drop table test2; create table test2 ( A1 char(5) references test1(C1), A2 integer Primary key); insert into test2 values ('cs363', 18); insert into test2 values ('cs363', 15); insert into test2 values (null, 19); 8

Referential Integrity insert into test2 values ('cs363', 18); insert into test2 values ('cs363', 15); -- Multiple records reference one value in test1 insert into test2 values (null, 19); -- can be null insert into test2 values ('cs234', 20); -- Parent key not found -- This begins comment 9

Referential Integrity drop table test1; -- Unique/primary keys in table referenced -- by foreign keys Delete from test2; drop table test1; -- Unique/primary keys in table referenced -- by foreign keys 10

Referential Integrity drop table test2; -- Table dropped drop table test1; -- Table dropped 11

Referential Integrity create table test2 ( A1 char(5) references test1(C1), A2 integer Primary key); -- table or view does not exist Create table test1 ( C1 char(5) primary key, C2 Varchar2(50), C3 integer, C4 Date); 12

Referential Integrity Create table test1 ( C1 char(5) primary key, C2 Varchar2(50), C3 integer, C4 Date); -- Table created create table test2 ( A1 char(5) references test1(C1), A2 integer Primary key); -- Table created 13

Referential Integrity Drop table test2; Drop table test1; Create table test1 ( C1 char(5) primary key, C2 Varchar2(50), C3 integer, C4 Date); create table test2 ( A1 char(5) references test1(C1), A2 integer Primary key); 14

UserName_Lab7.sql Name : Qi Yang -- UserName : YangQ -- Date : Course : CS Description: Sql script example Drop Table test2; Drop Table test1; Create table test1... Desc Test1 Pause Create Table Test2... Desc test2 Pause Insert into test1... Commit; Select * From Test1;... 15

Integrity Rules: Constraints Column Constraints Table Constraints 16

Column Constraints Create table Staff ( SNo char(4) Primary Key, Bno Char(4) Default 'B363' References Branch, FName Varchar2(20) Not Null, LName Varchar2(20) Not Null, -- assuming functions DateDiff and Now DOB Date Not Null Check (DateDiff(Year, Now, DOB) >= 16), Salary Number Check (Salary Between and ), -- between is inclusive SSN Char(9) Unique, Tel_No Char(12)); -- Primary Key, Unique, References should be the last constraint for a column -- Do not use Foreign key for column constraints, only use References 17

Table Constraints Constraints on one or more columns –Composite PK, AK, FK Cannot use Not Null in table constraints 18

Table Constraints Create table Staff ( SNo char(4), FName Varchar2(20) Not Null, LName Varchar2(20) Not Null, DOB Date, Salary Number default, BNo Char(4), Tel_No Char(12), SSN Char(11), Constraint PK_Staff Primary Key (SNo), Constraint Range_of_Salary Check (Salary between and ), Unique (SSN), Foreign Key (BNo) References Branch (BNo)); 19

Attribute Order in Foreign Key -- Primary key (c1, c2) for TableA Foreign Key (c1, c2) References TableA, -- Same order as PK Foreign Key (c2, c1) References TableA(c2, c1), -- Different order from PK Foreign Key (c2, c1) References TableA, -- Incorrect! 20

Schedule Next Monday Go over Quiz2 and Assignment6-2 Review for Test 1 Next Wednesday Test 1 Next Friday Lab 206 to do Assignment 7 Assignment 7 is due by 5 pm 21