Using SQL to create tables Ways of using Databases.

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

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.
Introduction to Structured Query Language (SQL)
SQL components In Oracle. SQL in Oracle SQL is made up of 4 components: –DDL Data Definition Language CREATE, ALTER, DROP, TRUNCATE. Creates / Alters.
Populating and Querying tables Insert and mostly View (DML)
Overview Relational Databases and SQL Pertemuan 1 Matakuliah: T0413/Current Popular IT II Tahun: 2007.
ASP.NET Database Connectivity I. 2 © UW Business School, University of Washington 2004 Outline Database Concepts SQL ASP.NET Database Connectivity.
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.
DT211 Stage 2 Databases Lab 1. Get to know SQL Server SQL server has 2 parts: –A client, running on your machine, in the lab. You access the database.
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”
Introduction to Structured Query Language (SQL)
SQL DDL constraints Restrictions on the columns and tables 1SQL DDL Constraints.
DAT702.  Standard Query Language  Ability to access and manipulate databases ◦ Retrieve data ◦ Insert, delete, update records ◦ Create and set permissions.
Oracle Data Definition Language (DDL)
SQL Basics. SQL SQL (Structured Query Language) is a special-purpose programming language designed from managing data in relational database management.
Chapter 9 SQL and RDBMS Part C. SQL Copyright 2005 Radian Publishing Co.
LOGO 1 Lab_02: Basic SQL. 2 Outline  Database Tables  SQL Statements  Semicolon after SQL Statements?  SQL DML and DDL  SQL SELECT Statement  SQL.
 SQL stands for Structured Query Language.  SQL lets you access and manipulate databases.  SQL is an ANSI (American National Standards Institute) standard.
Database Design lecture 3_1 1 Database Design Lecture 3_1 Data definition in SQL.
SQL data definition using Oracle1 SQL Data Definition using Oracle.
1 Chapter 2 Database Environment Transparencies © Pearson Education Limited 1995, 2005.
Chapter 10 – Database Creation1 IT238: Data Modeling and Database Design Unit 6: Database Creation Instructor: Qing Yan, M.D., Ph.D.
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.
SQL SQL Server : Overview SQL : Overview Types of SQL Database : Creation Tables : Creation & Manipulation Data : Creation & Manipulation Data : Retrieving.
FEN  Data Definition: CREATE TABLE, ALTER TABLE  Data Manipulation: INSERT, UPDATE, DELETE  Queries: SELECT SQL: Structured Query Language.
7 1 Chapter 7 Introduction to Structured Query Language (SQL) Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel.
SQL: DDL John Ortiz Cs.utsa.edu.
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 - DML. Data Manipulation Language(DML) Are used for managing data: –SELECT retrieve data from the a database –INSERT insert data into a table –UPDATE.
Database Systems Design, Implementation, and Management Coronel | Morris 11e ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or.
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.
Prince Sultan University Dept. of Computer & Information Sciences CS 340 Introduction to Database Systems.
7 1 Constraints & Triggers Chapter Constraints and triggers? Constraints: Certain properties that the DBMS is required to enforce –E.g. primary.
Chapter 7 SQL: Data Definition Pearson Education © 2009.
DBSQL 5-1 Copyright © Genetic Computer School 2009 Chapter 5 Structured Query Language.
Database Fundamental & Design by A.Surasit Samaisut Copyrights : All Rights Reserved.
SQL introduction 2013.
Visual Programing SQL Overview Section 1.
SQL Jan 20,2014. DBMS Stores data as records, tables etc. Accepts data and stores that data for later use Uses query languages for searching, sorting,
IS 380 Introduction to SQL This lectures covers material from: database textbook chapter 3 Oracle chapter: 3,14,17.
>> Introduction to MySQL. Introduction Structured Query Language (SQL) – Standard Database Language – Manage Data in a DBMS (Database Management System)
Populating and Querying tables Insert, Update, Delete and View (DML)
There are two types of MySQL instructions (Data Definition Language) DDL: Create database, create table, alter table,,,. (Data Manipulation Language) DML.
ECMM6018 Enterprise Networking For Electronic Commerce Tutorial 6 CGI/Perl and databases.
Relational Database Management System(RDBMS) Structured Query Language(SQL)
IS6146 Databases for Management Information Systems Lecture 3: SQL III – The DDL Rob Gleasure robgleasure.com.
Distribution of Marks For Second Semester Internal Sessional Evaluation External Evaluation Assignment /Project QuizzesClass Attendance Mid-Term Test Total.
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.
Chapter 3: Relational Databases
7 1 Database Systems: Design, Implementation, & Management, 7 th Edition, Rob & Coronel 7.6 Advanced Select Queries SQL provides useful functions that.
LM 5 Introduction to SQL MISM 4135 Instructor: Dr. Lei Li.
Physical Model Lecture 11. Physical Data Model The last step is the physical design phase, In this phase data is – Store – Organized and – Access.
Understand Data Definition Language (DDL) Database Administration Fundamentals LESSON 1.4.
Fundamentals of DBMS Notes-1.
Rob Gleasure robgleasure.com
Fundamental of Database Systems
CS 3630 Database Design and Implementation
Rob Gleasure robgleasure.com
Managing Tables, Data Integrity, Constraints by Adrienne Watt
SQL: Schema Definition and Constraints Chapter 6 week 6
Quiz Questions Q.1 An entity set that does not have sufficient attributes to form a primary key is a (A) strong entity set. (B) weak entity set. (C) simple.
Minggu 5, Pertemuan 9 SQL: Data Definition
CS 3630 Database Design and Implementation
لغة قواعد البيانات STRUCTURED QUERY LANGUAGE SQL))
SQL data definition using Oracle
DATABASE SQL= Structure Query Language مبادئ قواعد بيانات
Oracle Data Definition Language (DDL)
Presentation transcript:

Using SQL to create tables Ways of using Databases

Revisiting database structure External Schema Conceptual Schema Internal Schema Physical Schema

SQL Standard Query Language is used widely to access databases. Implemented SQL (Oracle, MS SQL Server, Access) varies a little from the standard, but is reasonably standard It consists of a data definition language - DDL (Create, Alter, Drop) And a Data Manipulation Language - DML (Select, Union, Update, Delete, Insert Into, Select Into, Transform, Parameter)

DLL - Add a table CREATE TABLE table_name {(column_name data_type [NOT NULL] [UNIQUE][DEFAULT default_option] [CHECK (search_condition)][,…]} [PRIMARY KEY (list_of_columns),] {[UNIQUE (list_of_columns),][,…]} {[FOREIGN KEY {(list_of_foreign_key_columns) REFERENCES parent_table_name[(list_of_candidate_key_columns)], [MATCH {PARTIAL | FULL} {[CHECK (search_condition)][,…]}) [] optional, {} one or more, lower case – user supplied names, upper case reserved words, [|] either or, () are part of the syntax.

Basic create DDL - Create a table Create table Publisher (PubID VARCHAR(10) NOT NULL, PubName VARCHAR(100), PubPhone VARCHAR(20)) Create table Books (ISBN VARCHAR(13) NOT NULL, Title VARCHAR(100), Price Decimal(7,2), PubID VARCHAR(10))

With keys Create table Publisher (PubID VARCHAR(10) UNIQUE NOT NULL, PubName VARCHAR(100), PubPhone VARCHAR(20), primary key (PubID)) Create table Books (ISBN VARCHAR(13), Title VARCHAR(100), Price Decimal(7,2), PubID VARCHAR(10), PRIMARY KEY (ISBN), FOREIGN KEY (PubID) REFERENCES PUBLISHER)

DDL - Add a table Syntax is as follows:- CREATE TABLE TableName (ColumnDefinition,… [,Multi-ColumnConstraint,…]); Column definition is ColumnDefinition ::= ColumnName DataType[(size)][Single-ColumnConstraint]

DDL - Single column constraint Single-ColumnConstraint:== CONSTRAINT IndexName [PRIMARY KEY | UNIQUE | REFERENCES ReferencedTable [(ReferencedColumn,…)] ] The ReferencedColumn is only necessary if the field being referenced is not the primary key of the referenced table

DDL - Multi-column constraints This constraint clause appears after all column definitions: CONSTRAINT IndexName [PRIMARY KEY (ColumnName,…) | UNIQUE (ColumnName,…) | FOREIGN KEY (ReferencingColumn,…) REFERENCES ReferencedTable[(ReferencedColumn,…)] ]

DDL - Create a table Create table Publisher (PubID TEXT(10) CONSTRAINT PrimaryKeyName PRIMARY KEY, PubName TEXT(100), PubPhone TEXT(20)); Create table Books (ISBN TEXT(13) CONSTRAINT PrimaryKeyName PRIMARY KEY, Title TEXT(100), Price MONEY, PubID TEXT(10) CONSTRAINT FOREIGN KEY PubID REFERENCES PUBLISHER(PubID));

DDL - ALTER or DROP TABLE Alter is used to –Add a new column to the table –Delete a column from the table Drop is used to delete a table

DDL - Syntax for Alter and Drop ALTER TABLE TableName ADD ColName ColType[(size)] [unique][not null] | DROP COLUMN ColName DROP TABLE TableName

Populating and Querying tables Insert, Update, Delete and View (DML)

Inserting data into a table To add a row of data to a table INSERT INTO VALUES (value1, value2, …valueN) If the value list matches the column list exactly, there is no need to list the column names. If the value list does not match the column list exactly, there is a need to list the column names.

Example insert into Expert values ( ,'Dr. O''Meara','Orthopaedic'); Where the expert table was set up as:- CREATE TABLE Expert ( Expert_Id numeric(7,0), Expert_Name varchar(50), Expertise_area varchar(15), PRIMARY KEY (Expert_Id)) Note also, if you want to put an apostrophe in a string, put in two apostrophes.

Delete Delete from expert where expert_id = ; This deletes all rows from the expert table that have an expert_id of Please note that you can only delete a row if no other row depends on it.

Referential integrity The above tables have no foreign keys and a straightforward insert does not compromise referential integrity. If a table is constrained by foreign key values, then there is a need to see what is in the other table. Retrieving data from a database –Data can be retrieved from a table or set of tables using the SELECT statement

Exercise The following tables form part of a database held in a relational DBMS: –Hotel (Hotel_No, Name, Address) –Room (Room_No, Hotel_No, Type, Price) –Booking (Hotel_No,Guest_No, Date_from, Date_To, Room_No) –Guest (Guest_No, Name, Address) where –Hotel contains hotel details and Hotel_No is the primary key, –Room contains room details for each hotel and (Hotel_No, Room_No) forms the primary key, –Booking contains details of the bookings and the primary key comprises (Hotel_No, Guest_No and Date_From) –Guest contains guest details and Guest_No is the primary key. Create the database.