DBS201: More on SQL Lecture 2.

Slides:



Advertisements
Similar presentations
Session 2Introduction to Database Technology Data Types and Table Creation.
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.
1 DBS201: More on SQL Lecture 3. 2 Agenda Review Constraints Primary Key Not Null Unique Check Foreign Key Changing a Table Definition Changing and deleting.
Copyright © by Royal Institute of Information Technology Introduction To Structured Query Language (SQL) 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.
1 DBS201: More on SQL Lecture 2. 2 Agenda Review How to create a table How to insert data into a table Terms Lab 2.
Sanjay Goel, School of Business, University at Albany, SUNY 1 SQL- Data Definition Language ITM 692 Sanjay Goel.
SQL components In Oracle. SQL in Oracle SQL is made up of 4 components: –DDL Data Definition Language CREATE, ALTER, DROP, TRUNCATE. Creates / Alters.
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)
Database Lecture # 1 By Ubaid Ullah.
CS 3630 Database Design and Implementation. Your Oracle Account UserName is the same as your UWP username Followed Not case sensitive Initial.
CHAPTER:14 Simple Queries in SQL Prepared By Prepared By : VINAY ALEXANDER ( विनय अलेक्सजेंड़र ) PGT(CS),KV JHAGRAKHAND.
Oracle Data Definition Language (DDL) Dr. Bernard Chen Ph.D. University of Central Arkansas Fall 2008.
SQL SQL Server : Overview SQL : Overview Types of SQL Database : Creation Tables : Creation & Manipulation Data : Creation & Manipulation Data : Retrieving.
1 Structured Query Language (SQL). 2 Contents SQL – I SQL – II SQL – III SQL – IV.
7 1 Chapter 7 Introduction to Structured Query Language (SQL) Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel.
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.
Topic 1: Introduction to SQL. SQL stands for Structured Query Language. SQL is a standard computer language for accessing and manipulating databases SQL.
Database Systems Design, Implementation, and Management Coronel | Morris 11e ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or.
1 DBS201: Introduction to Structure Query Language (SQL) Lecture 1.
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.
Database Lab Lecture 1. Database Languages Data definition language ( DDL ) Data definition language –defines data types and the relationships among them.
CREATE TABLE CREATE TABLE statement is used for creating relations Each column is described with three parts: column name, data type, and optional constraints.
Visual Programing SQL Overview Section 1.
1 DBS201: More on SQL Lecture 3. 2 Agenda How to use SQL to update table definitions How to update data in a table How to join tables together.
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.
Altering Tables and Constraints Database Systems Objectives Add and modify columns. Add, enable, disable, or remove constraints. Drop a table. Remove.
Relational Database Management System(RDBMS) Structured Query Language(SQL)
Chapter 3: Relational Databases
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.
Creating and Managing Tables. Database Objects ObjectDescription TableBasic unit of storage; composed of rows and columns ViewLogically represents subsets.
Database Constraints Ashima Wadhwa. Database Constraints Database constraints are restrictions on the contents of the database or on database operations.
Fundamentals of DBMS Notes-1.
Fundamental of Database Systems
CS 3630 Database Design and Implementation
CS320 Web and Internet Programming SQL and MySQL
Managing Tables, Data Integrity, Constraints by Adrienne Watt
SQL: Schema Definition and Constraints Chapter 6 week 6
Insert, Update and the rest…
SQL Creating and Managing Tables
ORACLE SQL Developer & SQLPLUS Statements
SQL Creating and Managing Tables
STRUCTURED QUERY LANGUAGE
Lecturer: Mukhtar Mohamed Ali “Hakaale”
DBS201: More on SQL Lecture 2.
SQL Creating and Managing Tables
DBS201: More on SQL Lecture 3
Chapter # 7 Introduction to Structured Query Language (SQL) Part II.
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:
Contents Preface I Introduction Lesson Objectives I-2
Chapter 2: Creating And Modifying Database Tables
CS3220 Web and Internet Programming SQL and MySQL
Data Definition Language
MIS2502: Data Analytics SQL 4– Putting Information Into a Database
CS3220 Web and Internet Programming SQL and MySQL
Instructor: Samia arshad
INTRODUCTION A Database system is basically a computer based record keeping system. The collection of data, usually referred to as the database, contains.
SQL (Structured Query Language)
DBS201: More on SQL Lecture 3
Presentation transcript:

DBS201: More on SQL Lecture 2

Agenda Review How to create a table How to insert data into a table Terms Lab 2

Review Data Anomalies What is a database? What is a DBMS? What are three categories of SQL statements Use three letters to describe each category On IBM i platform What command is used to initiate an interactive SQL session? How do you construct a container for Tables and Views A collection is also called two other names - ?

How to Create a Table First write a Database Structure chart. We can also call this a Data Dictionary of your table. Column Type Length PK FK reference Req’d ? Unique ? Validation

How to Create a Table Type: Length: Numeric Decimal Character Varchar Date ‘yyyy-mm-dd’ Do not specify a length Time Length: Specify positions to the left of the decimal point and positions to the right of the decimal point

How to Create a Table Primary Keys For concatenated Primary Keys, pay close attention to the order of the attributes within the Primary Key Indexing starts with the first attribute mentioned, then proceeds to the next attribute and so on PK: Specify ‘Y’ if this column name is a part of the PK, otherwise leave blank

How to Create a Table FK Reference: Req’d?: Specify the table name and the column name where this field is a PK Req’d?: PKs, by default are required If this column must be present, specify ‘Y’ Means that this column can not be left blank or NULL – enforces data integrity

How to Create a Table Unique?: Validation: Means only that the value can only appear once in this column Validation: Specify the range of values or the specific values that are allowed for this column

Definition of a Table Format for defining the table PAINTER: Column Type Len PK FK Req’d Unique Validation PTR_NUM CHAR 4 Y PTR_LNAME 15 PTR_FNAME PTR_CITY 20 PTR_PHONE DEC 10

Creating a ‘Database’ CREATE COLLECTION Syntax Used to create a ‘database’ Syntax CREATE COLLECTION ‘database name’

Deleting a ‘Database’ To remove a ‘database’, use the DROP COLLECTION statement Syntax DROP COLLECTION ‘database name’

Creating Tables in SQL CREATE TABLE Syntax Used to create a table CREATE TABLE tablename (field1 datatype fieldsize, field2 datatype fieldsize, …. CHECK …, PRIMARY KEY (fieldname(s)), FOREIGN KEY (fieldname) REFERENCES tablename (PKfieldname))

Creating Tables in SQL CREATE TABLE painter ( p_num char (4) not null with default, p_lname char (15) not null with default, p_fname char (15) not null with default, p_city char (20), p_phone dec (10), Constraint Painter_p_num_PK Primary Key (p_num) )

Creating Tables in SQL INSERT INTO WS201J40/PAINTER (P_NUM, P_FNAME, P_CITY) VALUES('111', 'BILL', 'TORONTO') 1 rows inserted in PAINTER in WS201J40. SELECT * FROM PAINTER

Creating Tables in SQL

Creating Tables in SQL CREATE TABLE painter ( p_num char (4), p_lname char (15) not null, p_fname char (15) not null, p_city char (20), p_phone dec (10), Constraint Painter_p_num_PK Primary Key (p_num) )

Creating Tables in SQL INSERT INTO WS201J40/PAINTER (P_NUM, P_FNAME, P_CITY) VALUES('111', 'BILL', 'TORONTO') Null values not allowed in column or variable P_LNAME

Creating Tables in SQL Primary Key Constraint initially not done: Table created without a primary key constraint p_phone dec (10) ) ALTER TABLE PAINTER ADD CONSTRAINT PAINTER_p_num_PK PRIMARY KEY (p_num)

Dropping Tables in SQL DROP TABLE Syntax Used to remove a table DROP TABLE tablename

Inserting Data into a Table Used to insert data into a table Syntax INSERT INTO tablename (fieldname1, fieldname2,….) VALUES (value1, value2…) or INSERT INTO tablename VALUES (value1, value2…), (value1, value2…) Single row added Multiple rows added

Inserting Data into a Table Rules for Inserting: Include all column names and provide values for each column Or Ignore column names and provide values in the same order as column names If table was created allowing NULLs, then indicate the word NULL in place of that column when specifying the values

Primary Keys in SQL Primary Key Primary Key must always be NOT NULL (and unique) What happens if you create the table without specifying a primary key, insert rows and then apply the primary key constraint?

Inserting a Row INSERT INTO PAINTERA40/PAINTER VALUES('111', 'Smith', 'Bill', 'Oakville', 905477333) INSERT INTO PAINTERA40/PAINTER VALUES('222', 'Brown', 'Nancy', 'Mississauga', 9055666633)

Inserting Data into a Table INSERT INTO PAINTERA40/PAINTER (p_num, p_lname, p_fname, p_city, p_phone) VALUES('111','Wong', 'Ben', 'Newmarket', 9058876644) Can we add the primary key constraint ? ALTER TABLE PAINTER ADD CONSTRAINT Painter_p_Num_PK PRIMARY KEY (P_Num) Unique index cannot be created because of duplicate keys.

SQL Terminology S C H E M A A group of related objects that consists of a library, a journal, a journal receiver, an SQL catalog, and an optional data dictionary. A schema enables the user to find the table, view and index objects by name. Another name for a schema is collection.

SQL Terminology Transaction Control Language The journal and journal receiver in a DB2 schema, support TCL statements With before and after images of transactions stored, multi table operations are supported with a Commit or Rollback TCL statement

SQL Terminology T A B L E A set of columns and rows. R O W The horizontal part of a table containing a serial set of columns. C O L U M N The vertical part of a table of one data type

Equivalent SQL Terminology L I B R A R Y A group of related objects that enables the user to find the objects by name. A schema is treated the same as a library by some native commands. P H Y S I C A L F I L E A set of records. A Table is treated the same as a Physical File by native and SQL commands

Equivalent SQL Terminology R E C O R D A set of fields. F I E L D One or more bytes of related information of one data type. In SQL this is referred to as a column.

PART Table – What can you identify? PART NUMBER PART DESC ON HAND CLASS WAREHOUSE PRICE AX12 Iron 104 HW 3 23.95 AZ52 Dartboard 20 SG 2 12.95 BA74 Basketball 40 1 29.95 BH22 Cornpopper 95 24.95 BT04 Gas Grill 11 AP 149.99 BZ66 Washer 52 399.99 CA14 Griddle 78 39.99 CB03 Bike 44 299.99 CX11 Blender 112 22.95 CZ81 Treadmill 68 349.99

Table Exercise 1: Create a table called AGENT using the following data dictionary: Insert the following data into your table: Column Type Len PK FK Req’d Unique Validation AGT_NUM CHAR 3 Y AGT_LNAME 25 AGT_FNAME 20 AGT_AREACODE AGT_PHONE 7 AGT_NUM AGT_LNAME AFT_FNAME AGT_AREACODE AGT_PHONE 5 Andrews Mark 416 123-3456 10 Chen Terry 250 333-4059 15 DaSilva Lila 234-5837 20 Edwards George 444-5959 25 Eustace Gina 519 948-8494

Table Exercise 2: Create a table using the following data dictionary: Insert the following data into your table: Column Type Len PK FK Req’d Unique Validation CUST_NUM DEC 3 Y 1-100 CUST_LNAME CHAR 25 CUST_FNAME 20 CUST_AREACODE AGENT_NUM AGENT (AGENT_NUM) CUST_NUM CUST_LNAME CUST_FNAME CUST_AREACODE CUST_PHONE 5 Andrews Mark 416 123-3456 10 Chen Terry 250 333-4059 15 DaSilva Lila 234-5837 20 Edwards George 444-5959 25 Eustace Gina 519 948-8494

Agenda Lab 2