Creating Tables Create a new table by defining the column structure.

Slides:



Advertisements
Similar presentations
Data Definition and Integrity Constraints
Advertisements

CC SQL Utilities.
MySQL. To start go to Login details: login: labuser password:macimd15 – There.
2010/11 : [1]Building Web Applications using MySQL and PHP (W1)MySQL Recap.
Database Systems: Design, Implementation, and Management Tenth Edition
Copyright © by Royal Institute of Information Technology Introduction To Structured Query Language (SQL) 1.
Structured Query Language - SQL Carol Wolf Computer Science.
Introduction to Structured Query Language (SQL)
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 SQL stands for Structured Query Language SQL allows you to access a database SQL is an ANSI standard computer language SQL can execute queries against.
5 Chapter 5 Structured Query Language (SQL1) Revision.
Writing Basic SQL statement 2 July July July Create By Pantharee Sawasdimongkol.
Introduction to Structured Query Language (SQL)
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.
Database Systems: Design, Implementation, and Management Eighth Edition Chapter 7 Introduction to Structured Query Language (SQL)
WRITING BASIC SQL SELECT STATEMENTS Lecture 7 1. Outlines  SQL SELECT statement  Capabilities of SELECT statements  Basic SELECT statement  Selecting.
DATABASES AND SQL. Introduction Relation: Relation means table(data is arranged in rows and columns) Domain : A domain is a pool of values appearing in.
Structured Query Language (SQL) A2 Teacher Up skilling LECTURE 2.
A Guide to SQL, Eighth Edition Chapter Three Creating Tables.
Chapter 5 Introduction to SQL. Structured Query Language = the “programming language” for relational databases SQL is a nonprocedural language = the user.
Copyright © 2003 Pearson Education, Inc. Slide 8-1 The Web Wizard’s Guide to PHP by David Lash.
Relational DBs and SQL Designing Your Web Database (Ch. 8) → Creating and Working with a MySQL Database (Ch. 9, 10) 1.
AL-MAAREFA COLLEGE FOR SCIENCE AND TECHNOLOGY INFO 232: DATABASE SYSTEMS CHAPTER 7 INTRODUCTION TO STRUCTURED QUERY LANGUAGE (SQL) Instructor Ms. Arwa.
CHAPTER:14 Simple Queries in SQL Prepared By Prepared By : VINAY ALEXANDER ( विनय अलेक्सजेंड़र ) PGT(CS),KV JHAGRAKHAND.
Chapter 8 Part 1 SQL-99 Schema Definition, Constraints, Queries, and Views.
Chapter 7 SQL HUANG XUEHUA. SQL SQL server2005 introduction Install components  management studio.
11 Chapter 7: Creating Tables and Views 7.1 Creating Views with the SQL Procedure 7.2 Creating Tables with the SQL Procedure (Self-Study) 7.3 Integrity.
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.
CS146 References: ORACLE 9i PROGRAMMING A Primer Rajshekhar Sunderraman
6 1 Lecture 8: Introduction to Structured Query Language (SQL) J. S. Chou, P.E., Ph.D.
BIS Database Systems School of Management, Business Information Systems, Assumption University A.Thanop Somprasong Chapter # 7 Introduction to Structured.
1 DBS201: Introduction to Structure Query Language (SQL) Lecture 1.
SQL Basics. What is SQL? SQL stands for Structured Query Language. SQL lets you access and manipulate databases.
Advanced Database CS-426 Week 1 - Introduction. Database Management System DBMS contains information about a particular enterprise Collection of interrelated.
SQL for SQL Server, C13© 2002, Mike Murach & Associates, Inc. Slide 1.
SQL CREATING AND MANAGING TABLES lecture4 1. Database Objects ObjectDescription TableBasic unit of storage; composed of rows and columns ViewLogically.
CMPT 258 Database Systems The Relationship Model (Chapter 3)
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.
1 CS 430 Database Theory Winter 2005 Lecture 11: SQL DDL.
1 Copyright © Oracle Corporation, All rights reserved. Writing Basic SQL SELECT Statements.
1 CS 430 Database Theory Winter 2005 Lecture 13: SQL DML - Modifying Data.
Writing Basic SQL SELECT Statements Lecture
LM 5 Introduction to SQL MISM 4135 Instructor: Dr. Lei Li.
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.
Copyright س Oracle Corporation, All rights reserved. 1 Writing Basic SQL Statements.
3 A Guide to MySQL.
Chapter 5 Introduction to SQL.
Managing Tables, Data Integrity, Constraints by Adrienne Watt
SQL: Schema Definition and Constraints Chapter 6 week 6
Insert, Update and the rest…
CS 480: Database Systems Lecture 13 February 13,2013.
Basic Queries Specifying Columns
PROC SQL, Overview.
Writing Basic SQL SELECT Statements
ORACLE SQL Developer & SQLPLUS Statements
Correlated Subqueries
Integrity Constraints
Defining a Database Schema
Oracle Data Definition Language (DDL)
Writing Basic SQL SELECT Statements
SQL-1 Week 8-9.
Writing Basic SQL SELECT Statements
Session - 6 Sequence - 1 SQL: The Structured Query Language:
Chapter # 7 Introduction to Structured Query Language (SQL) Part I.
Subqueries.
Session - 6 Sequence - 1 SQL: The Structured Query Language:
JDBC II IS
SQL (Structured Query Language)
Presentation transcript:

Creating Tables Create a new table by defining the column structure. 3 Creating Tables Create a new table by defining the column structure. Create a new table by copying column structure from an existing table. Create a new table and add data using a single query. Load data into a table.

Creating Tables with SQL Multiple techniques are used to create tables and insert data into tables with SQL. Method Syntax Result 1 CREATE TABLE table-name (column-name type(length) <, ...column-name type(length)>); Create an empty table by manually specifying all column attributes. 2 CREATE TABLE table-name LIKE old-table-name; Create an empty table by copying column attributes from an existing table using a LIKE clause. 3 CREATE TABLE table-name AS query-expression; Create a table and add data all in one step, using a query.

Creating Tables Method 1: Define the columns. General form of the CREATE TABLE statement: CREATE TABLE table-name (column-name type(length) <, ...column-name type(length)> );

Method 1: Defining Columns The table definition is enclosed in parentheses. Individual column definitions are separated by commas. proc sql; create table Discounts (Product_ID num format=z12., Start_Date date, End_Date date, Discount num format=percent.); quit; proc contents data=discounts;run; Name the new table. Define the columns.

Method 1: Defining Columns For ANSI compliance, PROC SQL accepts the following data types in table definitions: ANSI Type Resulting SAS Type Default Length Default Format CHAR(n) Character 8 $w. VARCHAR INTEGER Numeric BEST. SMALLINT DECIMAL NUMERIC FLOAT REAL DOUBLE PRECISION DATE DATE.

Method 1: Defining Columns proc sql; create table Testing_Types (Char_Column char(4), Varchar_Column varchar, Int_Column int, SmallInt_Column smallint, Dec_Column dec, Num_Column num, Float_Column float, Real_Column real, Date_Column date, Double_Column double precision); quit; proc contents data=testing_types;run;

Creating Tables Method 2: Copy the table structure. CREATE TABLE table-name-2 LIKE table-name-1;

Method 2: Copying Table Structure proc sql; create table work.New_Sales_Staff like orion.Sales ; quit; proc contents data=new_sales_staff; run;

Creating Tables (Review) Method 3: Create and populate a table with an SQL query. CREATE TABLE table-name AS query-expression;

Method 3: Create and Populate a Table with an SQL Query (Review) proc sql; create table work.Melbourne as select Employee_Name as Name,Salary from orion.Staff as s, orion.Employee_addresses as a where s.Employee_ID=a.Employee_ID and City ="Melbourne"; quit; proc contents data=melbourne;run;

Adding Data to a Table, the INSERT Statement The INSERT statement can be used to add data to an empty table, or to append data to a table that already contains data, using one of three methods. Method Syntax Description A INSERT INTO table-name SET column-name=value, column-name=value,...; One clause per row using column-value pairs B INSERT INTO table-name <(column list)> VALUES (value,value,...); One clause per row using positional values C INSERT INTO table-name <(column list)> SELECT columns FROM table-name; A query returning multiple rows, and based on positional values

Create the example table, discounts proc sql; create table Discounts (Product_ID num format=z12., Start_Date date, End_Date date, Discount num format=percent.); quit; proc contents data=discounts;run;

Method A: Adding Data with a SET Clause The SET clause requires that you add data using column name–value pairs: proc sql; insert into Discounts set Product_ID=230100300006, Start_Date='01MAR2007'd, End_Date='15MAR2007'd,Discount=.33 set Product_ID=230100600018, Start_Date='16MAR2007'd, End_Date='31MAR2007'd, Discount=.15 ; quit; proc print data=discounts;run;

Method B: Adding Data with a VALUES Clause proc sql; create table Discounts (Product_ID num format=z12., Start_Date date, End_Date date, Discount num format=percent.) ; insert into Discounts values (230100300006,'01MAR2007'd, '15MAR2007'd,.33) values (230100600018,'16MAR2007'd, '31MAR2007'd,.15) select * from discounts; quit; The VALUES clause adds data to the columns in a single row of data. The VALUES clause must produce values in the same order as the INSERT INTO statement column list.

Method B: Adding Data with a VALUES Clause proc sql; create table Discounts (Product_ID num format=z12., Start_Date date, End_Date date, Discount num format=percent.) ; insert into Discounts (Start_Date,End_Date, Product_ID, Discount) values ('01MAR2007'd,'15MAR2007'd,230100300006,.33) values ('16MAR2007'd,'31MAR2007'd,230100600018,.15) select * from discounts; quit; Optionally, the INSERT statement can list the columns into which data is to be inserted, in the order in which the VALUES clause will provide the data.

Method C: Adding Data with a Query proc sql; create table Discounts (Product_ID num format=z12., Start_Date date, End_Date date, Discount num format=percent.) ; insert into Discounts (Product_ID,Discount,Start_Date,End_Date) select distinct Product_ID,.35, '01MAR2007'd,'31mar2007'd from orion.Product_Dim where Supplier_Name contains 'Pro Sportswear Inc' select * from discounts; quit; Query results are inserted positionally. The query must produce values in the same order as the INSERT statement column list.