SQL: Concepts. Todd Bacastow IST 210: Organization of Data 2/17/ IST 210

Similar documents
SQL DATA DEFINITION LANGUAGE

SQL DATA DEFINITION LANGUAGE

SQL Functionality SQL. Creating Relation Schemas. Creating Relation Schemas

SQL DATA DEFINITION LANGUAGE

SQL: Data Definition Language

user specifies what is wanted, not how to find it

EGCI 321: Database Systems. Dr. Tanasanee Phienthrakul

Chapter 4. Basic SQL. Copyright 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

SQL language. Jaroslav Porubän, Miroslav Biňas, Milan Nosáľ (c)

Chapter 4. Basic SQL. SQL Data Definition and Data Types. Basic SQL. SQL language SQL. Terminology: CREATE statement

Information Systems Engineering. SQL Structured Query Language DDL Data Definition (sub)language

COSC344 Database Theory and Applications. Lecture 5 SQL - Data Definition Language. COSC344 Lecture 5 1

Full file at

CS2300: File Structures and Introduction to Database Systems

SQL Data Definition and Data Manipulation Languages (DDL and DML)

Chapter # 7 Introduction to Structured Query Language (SQL) Part I

Slides by: Ms. Shree Jaswal

Lab # 4. Data Definition Language (DDL)

COGS 121 HCI Programming Studio. Week 03 - Tech Lecture

Database Management System 9

SQL Data Definition Language: Create and Change the Database Ray Lockwood

Chapter 4: SQL Basics

Data Definition and Data Manipulation. Lecture 5: SQL s Data Definition Language CS1106/CS5021/CS6503 Introduction to Relational Databases

Lecture 5: SQL s Data Definition Language

Copyright 2016 Ramez Elmasri and Shamkant B. Navathe

Full file at

CS143: Relational Model

SQL Simple Queries. Chapter 3.1 V3.01. Napier University

Creating Tables, Defining Constraints. Rose-Hulman Institute of Technology Curt Clifton

In This Lecture. SQL Data Definition SQL SQL. Non-Procedural Programming. Notes. Database Systems Lecture 5 Natasha Alechina

Outline. Textbook Chapter 6. Note 1. CSIE30600/CSIEB0290 Database Systems Basic SQL 2

Basic SQL. Basic SQL. Basic SQL

Databases 1. Defining Tables, Constraints

Programming and Database Fundamentals for Data Scientists

CSCI3030U Database Models

Chapter 1 SQL and Data

The Relational Model of Data (ii)

SQL Fundamentals. Chapter 3. Class 03: SQL Fundamentals 1

BASIC SQL CHAPTER 4 (6/E) CHAPTER 8 (5/E)

CS54100: Database Systems

CSIE30600 Database Systems Basic SQL 2. Outline

Chapter 2 The relational Model of data. Relational model introduction

MTAT Introduction to Databases

Chapter 7. Introduction to Structured Query Language (SQL) Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel

SQL Introduction. CS 377: Database Systems

SQL - Subqueries and. Schema. Chapter 3.4 V4.0. Napier University

CSCC43H: Introduction to Databases. Lecture 4

CPS510 Database System Design Primitive SYSTEM STRUCTURE

SQL Overview. CSCE 315, Fall 2017 Project 1, Part 3. Slides adapted from those used by Jeffrey Ullman, via Jennifer Welch

BASIC SQL CHAPTER 4 (6/E) CHAPTER 8 (5/E)

SQL: DDL. John Ortiz Cs.utsa.edu

CSC 453 Database Technologies. Tanu Malik DePaul University

Using Relational Databases for Digital Research

Basic SQL. Dr Fawaz Alarfaj. ACKNOWLEDGEMENT Slides are adopted from: Elmasri & Navathe, Fundamentals of Database Systems MySQL Documentation

Principles of Data Management

SQL: Data De ni on. B0B36DBS, BD6B36DBS: Database Systems. h p:// Lecture 3

Introduction to Information Systems

Downloaded from

1.8 Database and data Data Definition Language (DDL) and Data Manipulation Language (DML)

SQL: A COMMERCIAL DATABASE LANGUAGE. Data Change Statements,

HOW TO CREATE AND MAINTAIN DATABASES AND TABLES. By S. Sabraz Nawaz Senior Lecturer in MIT FMC, SEUSL

D B M G. SQL language: basics. Managing tables. Creating a table Modifying table structure Deleting a table The data dictionary Data integrity

Chapter 9: Working with MySQL

DB Creation with SQL DDL

COSC 304 Introduction to Database Systems SQL DDL. Dr. Ramon Lawrence University of British Columbia Okanagan

Introduction to Databases

Islamic University of Gaza Faculty of Engineering Department of Computer Engineering Fall 2011 ECOM 4113: Database System Lab Eng.

CS145 Introduction. About CS145 Relational Model, Schemas, SQL Semistructured Model, XML

CMPT 354: Database System I. Lecture 2. Relational Model

WHAT IS SQL. Database query language, which can also: Define structure of data Modify data Specify security constraints

2.9 Table Creation. CREATE TABLE TableName ( AttrName AttrType, AttrName AttrType,... )

Referential Integrity and Other Table Constraints Ray Lockwood

Module 3 MySQL Database. Database Management System

DS Introduction to SQL Part 1 Single-Table Queries. By Michael Hahsler based on slides for CS145 Introduction to Databases (Stanford)

Database Management Systems,

Data Base Lab. The Microsoft SQL Server Management Studio Part-3- By :Eng.Alaa I.Haniy.

types, but key declarations and constraints Similar CREATE X commands for other schema ëdrop X name" deletes the created element of beer VARCHARè20è,

Lab # 2. Data Definition Language (DDL) Eng. Alaa O Shama

SQL OVERVIEW. CS121: Relational Databases Fall 2017 Lecture 4

SQL DATA DEFINITION: KEY CONSTRAINTS. CS121: Relational Databases Fall 2017 Lecture 7

Chapter 8 SQL-99: Schema Definition, Basic Constraints, and Queries

Fname A variable character field up to 15 characters in length. Must have a value Lname A variable character field up to 15

Database Systems: Design, Implementation, and Management Tenth Edition. Chapter 7 Introduction to Structured Query Language (SQL)

1 INTRODUCTION TO EASIK 2 TABLE OF CONTENTS

SQL Data Definition: Table Creation

Database Foundations. 6-4 Data Manipulation Language (DML) Copyright 2015, Oracle and/or its affiliates. All rights reserved.

Where Are We? Next Few Lectures. Integrity Constraints Motivation. Constraints in E/R Diagrams. Keys in E/R Diagrams

Solved MCQ on fundamental of DBMS. Set-1

CMP-3440 Database Systems

doc. RNDr. Tomáš Skopal, Ph.D. RNDr. Michal Kopecký, Ph.D.

Database design process

Databases-1 Lecture-01. Introduction, Relational Algebra

Data Definition Language (DDL)

Data about data is database Select correct option: True False Partially True None of the Above

Data Manipulation (DML) and Data Definition (DDL)

ASSIGNMENT NO 2. Objectives: To understand and demonstrate DDL statements on various SQL objects

Data Modelling and Databases. Exercise Session 7: Integrity Constraints

Relational Data Structure and Concepts. Structured Query Language (Part 1) The Entity Integrity Rules. Relational Data Structure and Concepts

Introduction. Sample Database SQL-92. Sample Data. Sample Data. Chapter 6 Introduction to Structured Query Language (SQL)

Data, Databases, and DBMSs

Transcription:

SQL: Concepts Todd Bacastow IST 210: Organization of Data 2/17/2004 1

Design questions How many entities are there? What are the major entities? What are the attributes of each entity? Is there a unique identifier for one or more of these entities? How are the entities related to each other? What queries and/or reports will be produced with the data? 2/17/2004 2

Design Process The entity becomes a table The entity name becomes the table name Each attribute becomes a column The instance identifier becomes the primary key For the one to many relation, an additional column is added to the table containing the foreign key 2/17/2004 3

SQL SQL = Structured Query Language Data Definition Language (DDL) Data Manipulation Language (DML) Standards Current - SQL2 Emerging - SQL3 Most DBMS implementations have a variation Syntax = How the language is used 2/17/2004 4

DDL commands Create the structure for database objects - the database itself, tables and views Create, Alter, Truncate, Drop, and Delete Insert and change data are Insert and Delete 2/17/2004 5

DDL Commands CREATE TABLE - creates a new database table ALTER TABLE - alters (changes) a database table DROP TABLE - deletes a database table CREATE INDEX - creates an index (search key) DROP INDEX - deletes an index 2/17/2004 6

Data Types The most familiar data types are Numeric Text Other types of data are time and date logical graphic representations of non- Roman character sets binary large object (or BLOB) 2/17/2004 7

Typical Syntax INT or INTEGER REAL or FLOAT CHAR(n) Fixed-length character string VARCHAR(n) Variable-length strings up to n characters DATE In yyyy-mm-dd format TIME In hh:mm:ss format 2/17/2004 8

Defining a DB Schema CREATE TABLE NameOfTable (Column Name data type null value, Column Name data type null value, Column Name data type null value); Semicolon after SQL Statements? Semicolon is the standard way to separate each SQL statement in database systems that allow more than one SQL statement to be executed in the same call to the server. 2/17/2004 9

Example CREATE TABLE Customer (fname varchar (20) Not Null, lname varchar (25) Not Null, phonenumber varchar (10), address varchar (40) Not Null, state varchar (20) Not Null, zipcode integer Primary Key); 2/17/2004 10

Another Example CREATE TABLE tablename (list of elements) Principal elements are attributes and their types, but key declarations and constraints also appear CREATE TABLE Sells (bar CHAR(20), beer VARCHAR(20), price REAL); DROP TABLE Sells; DROP TABLE tablename Delete the created table 2/17/2004 11

NULL In place of a value for an attribute Interpretation is not exactly missing value There could be many reasons why no value is present, e.g., value inappropriate 2/17/2004 12

Keys A primary key is a column or set of columns that uniquely identifies the rest of the data in any given row. A foreign key is a column in a table where that column is a primary key of another table Means that any data in a foreign key column must have corresponding data in the other table where that column is the primary key In DBMS-speak, this correspondence is known as referential integrity 2/17/2004 13

Declaring Keys PRIMARY KEY SQL2 does not allow nulls (empty values) in primary key Two places to declare: After an attribute s type, if the attribute is a key by itself As a separate element, when key is > 1 attribute CREATE TABLE Sells (bar CHAR(20), beer VARCHAR(20), price REAL, PRIMARY KEY (bar,beer)); 2/17/2004 14

Other Properties of Attributes NOT NULL every row (tuple) must have a real value for this attribute DEFAULT value A value to use whenever no other value of this attribute is known 2/17/2004 15

Populating a Database INSERT statement can be used to add just a single row of data or multiple rows of data into the database. The INSERT query is constructed as follows: INSERT INTO nameoftable (column names) VALUES (data to be added to each of the above column names) 2/17/2004 16

Example CREATE TABLE Customers (name CHAR(30) PRIMARY KEY, addr CHAR(50) DEFAULT 120 Oxford St, phone CHAR(16)); INSERT INTO Customers(name) VALUES( Clinton ); name Clinton addr 120 Oxford St phone NULL 2/17/2004 17

Notes Primary key is by default NOT NULL In INSERT, it is OK to list a subset of the attributes and values for only this subset But if we had declared phone CHAR(16) NOT NULL then the insertion could not be made 2/17/2004 18

Altering a Table The Alter Table command allows the database designer to make changes to a table, to add columns as well as add or drop constraints such as primary and foreign keys. ALTER TABLE ADDRESS ADD (Zip_plus4 VARCHAR2(4)) 2/17/2004 19

Changing Columns ALTER TABLE R ADD column declaration; Add an attribute of relation R Example: ALTER TABLE Customers ADD education CHAR(16) DEFAULT high school ; ALTER TABLE R DROP column; Drop columns of a table 2/17/2004 20

Views VIEW = an expression that describes a table without creating it = a virtual table TABLE = a physical table on the hard drive View definition form is: CREATE VIEW name AS query; Views can be queried as if they existed physically Views may be modified, in some cases 2/17/2004 21

Example The view VIPCustomers is the set of customers that have at least $10,000 Customers (name, balance); CREATE VIEW VIPCustomers AS SELECT * FROM Customers WHERE balance >= 10000; SELECT * FROM VIPCustomer WHERE name LIKE % Clinton ; Note: A "%" sign can be used to define wildcards (missing letters in the pattern) both before and after the pattern 2/17/2004 22

Indexing Indexing improves the storage and retrieval of database attributes and should be considered for any other fields that are likely to experience heavy use Primary keys form the basis for indexes on the databases and the DBMS will automatically create indexes for fields identified as primary keys Indexes also may be built for fields that are not key values or for fields whose values are not expected to occur uniquely on the database These are called secondary keys and are explicitly built 2/17/2004 23