Database Management Systems,

Similar documents
CSCC43H: Introduction to Databases. Lecture 4

Database Management Systems,

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

EGCI 321: Database Systems. Dr. Tanasanee Phienthrakul

Database Management Systems,

SQL Functionality SQL. Creating Relation Schemas. Creating Relation Schemas

Basic SQL. Basic SQL. Basic SQL

Database Management Systems,

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

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

Downloaded from

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

1) Introduction to SQL

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

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

CS2300: File Structures and Introduction to Database Systems

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

Week 4 & 5: SQL. SQL as a Query Language. Example Database. SQL Queries

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

Database Management Systems

user specifies what is wanted, not how to find it

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

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

Tables From Existing Tables

The DBMS accepts requests for data from the application program and instructs the operating system to transfer the appropriate data.

COGS 121 HCI Programming Studio. Week 03 - Tech Lecture

SQL Structured Query Language Introduction

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

SQL Data Definition Language

SQL Introduction. CS 377: Database Systems

Chapter 3: Introduction to SQL

Working with Databases and Java

MTAT Introduction to Databases

Unit 1 - Chapter 4,5

Copyright 2016 Ramez Elmasri and Shamkant B. Navathe

Introduction to SQL on GRAHAM ED ARMSTRONG SHARCNET AUGUST 2018

Sample Question Paper

Data Storage and Query Answering. Data Storage and Disk Structure (4)

Oracle 1Z MySQL 5.6 Developer.

2. E/R Design Considerations

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

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

Chapter 3: Introduction to SQL. Chapter 3: Introduction to SQL

SQL: Data Definition Language

Database Programming with SQL

ACS-3902 Fall Ron McFadyen 3D21 Slides are based on chapter 5 (7 th edition) (chapter 3 in 6 th edition)

Chapter 3: Introduction to SQL

Simple Quesries in SQL & Table Creation and Data Manipulation

UFCEKG 20 2 : Data, Schemas and Applications

Selections. Lecture 4 Sections Robb T. Koether. Hampden-Sydney College. Wed, Jan 22, 2014

The SQL data-definition language (DDL) allows defining :

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

After completing this unit, you should be able to: Define the terms

CS143: Relational Model

Relational Model. CSE462 Database Concepts. Demian Lessa. Department of Computer Science and Engineering State University of New York, Buffalo

EE221 Databases Practicals Manual

SQL DATA DEFINITION LANGUAGE

Draft. Students Table. FName LName StudentID College Year. Justin Ennen Science Senior. Dan Bass Management Junior

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

Translating an ER Diagram to a Relational Schema

Relational Database Languages

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

History of SQL. Relational Database Languages. Tuple relational calculus ALPHA (Codd, 1970s) QUEL (based on ALPHA) Datalog (rule-based, like PROLOG)

CSIE30600 Database Systems Basic SQL 2. Outline

Lab # 4. Data Definition Language (DDL)

The Relational Model

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

Exam code: Exam name: Database Fundamentals. Version 16.0

MySQL Introduction. By Prof. B.A.Khivsara

Full file at

Database Management System (15ECSC208) UNIT I: Chapter 2: Relational Data Model and Relational Algebra

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

CS145 Final Examination

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

CS 327E Lecture 2. Shirley Cohen. January 27, 2016

CSC309: Introduction to Web Programming. Lecture 6

From theory to practice. Designing Tables for a postgresql Database System. psql. Reminder. A few useful commands

QQ Group

Database Management System Dr. S. Srinath Department of Computer Science & Engineering Indian Institute of Technology, Madras Lecture No.

Database Systems CSE 303. Lecture 02

Database Systems CSE 303. Lecture 02

UNIT III INTRODUCTION TO STRUCTURED QUERY LANGUAGE (SQL)

Chapter 9: Working with MySQL

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

Private Institute of Aga NETWORK DATABASE LECTURER NIYAZ M. SALIH

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

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

Introduction to Relational Database Management Systems

Applied Databases. Sebastian Maneth. Lecture 7 Simple SQL Queries. University of Edinburgh - February 1 st, 2016

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

CSC 453 Database Technologies. Tanu Malik DePaul University

ER to Relational Model. Professor Jessica Lin

Data Definition Language (DDL)

Lecture 07. Spring 2018 Borough of Manhattan Community College

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

Creating SQL Tables and using Data Types

SQL. Char (30) can store ram, ramji007 or 80- b

QUIZ 1 REVIEW SESSION DATABASE MANAGEMENT SYSTEMS

Applied Databases. Sebastian Maneth. Lecture 7 Simple SQL Queries. University of Edinburgh - February 6 st, 2017

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

Transcription:

Database Management Systems SQL Query Language (1) 1

Topics Introduction SQL History Domain Definition Elementary Domains User-defined Domains Creating Tables Constraint Definition INSERT Query SELECT Query Attribute Expressions Logical Expressions in Queries 2

SQL SQL stands for Structured Query Language First proposed by IBM Research 1974 First implementation SQL/DS, IBM 1981 Most DBMS systems support base functionality but add some new properties 3

Domains Domains specify the content type of attributes e.g. Attribute Employee name gets its value from the domain of strings (Each name is a string) Two categories of domains are: Elementary (predefined by the standard) User-defined 4

Elementary Domains - Character Character domain is used with single character or string attributes Strings may have variable length Syntax: character or char to define single character attributes character (n) or char (n) defines a fixed length string character varying or varchar defines a variable length string 5

Elementary Domains Exact Values Exact numeric domains are used with exact values, integer, or numbers with a fractional part Four types are: numeric [ ( Precision [, Scale ] ) ] where precision is the total number of digits and scale is the number of digits after decimal point e.g. numeric (5,2) shows numbers like 455.12 decimal [ ( Precision [, Scale ] ) ] (same as numeric) integer smallint 6

Elementary Domains Real Values Real Value Domains are used for non-exact numeric values Real value domains are based on a floating point representation Three types available float [ ( Precision ) ] e.g. float(6) (total number of digits is 6) double precision (two times the precision of float) real 7

Elementary Domains Date, Time, and Intervals Date and Time are used for temporal instant attributes e.g. Date (stores day, month and year values) A date value is given as MM/DD/YY or MM/DD/YYYY in USA YY.MM.DD or YYYY.MM.DD in ANSI (and more..) Time ( stores hour, minute, and second values) Time value is given as hh:mm:ss 8

Intervals Temporal Interval domains are defined by Interval First To Last e.g. Interval 1990 To 1999 (year) Interval 1985.1 To 2009.10 (year and month) Interval 11:20:10 To 12:25:40 (hour, minute and second) 9

User-defined Domains A user-defined domain is given by name elementary domain default value Syntax: create domain DomainName as elementarydomain [ default DefaultValue ] e.g. create domain StudentName as char(30) e.g. create domain money as numeric(19,2) default 0 10

Table Definition An SQL table consists of an ordered set of attributes an optional set of constraints create table statement defines a relation schema creates an empty table 11

Create Table Syntax: create table TableName ( AttributeName 1 Domain [ DefaultValue ] [ Constraints ], AttributeName 2 Domain [ DefaultValue ] [ Constraints ], AttributeName n Domain [ DefaultValue ] [ Constraints ] [ Other Constraints ] ) 12

Example create table Employee ( RegNo character(6) primary key, FirstName character(20) not null, Surname character(20) not null, Dept character (15) ) 13

Primary Key Constraint Primary key constraint is defined by primary key keyword as After defining the attribute in the table (as a constraint) At the end of create table statement. This method is used when primary key has more than one attribute in it. 14

Example 1 CREATE TABLE Persons ( P_Id integer PRIMARY KEY, LastName varchar NOT NULL, FirstName varchar, Address varchar, City varchar ) 15

Example 2 CREATE TABLE Persons ( P_Id integer NOT NULL, LastName varchar(255) NOT NULL, FirstName varchar(255), Address varchar(255), City varchar(255), PRIMARY KEY (P_Id,LastName) ) 16

Uniqueness Constraint We may define an attribute or a group of attributes as unique. This constraint will not allow repeated values for that attribute(s) Syntax Use unique after defining the attribute Use unique( attribute_name ) at the end of table definition. 17

Example create table Employee ( RegNo character(6) primary key, FirstName character(20) not null, Surname character(20) not null, Dept character (15), Salary numeric(9) default 0, City character(15), unique(surname, FirstName) ) 18

Foreign Key Constraint Foreign keys are defined using references keyword Syntax: references External_Table( Attribute) 19

Example create table Employee ( RegNo character(6) primary key, FirstName character(20) not null, Surname character(20) not null, Dept character (15) references Department(DeptName), Salary numeric(9) default 0, City character(15), unique(surname,firstname) ) 20

NULL Constraint An attribute can be defined to always have a value using not null constraint e.g. FirstName character(20) not null, Surname character(20) not null unique, 21

INSERT Query INSERT is used to add a record ( tuple ) to the table. Syntax Insert Into table_name [( Attributes) ] values (list of values ) e.g. insert into Department(DeptName, City) values( Production, Ankara ) 22

Example Car: Insert into Car (CarRegNo, Make, Model, DriverID) Values ( DEF 456, BMW, Z3, VR 2030020Y ) Car: 23

INSERT Query Properties The ordering of the attributes (if present) and of values is important (first value matches with the first attribute, and so on) If AttributeList is omitted, all the relation attributes are considered, in the order in which they appear in the table definition If AttributeList does not contain all the relation attributes, to the remaining attributes it is assigned the default value (if defined) or the null value 24

SELECT Query Select is used for retrieving records from tables The simplest form of Select query is: SELECT attribute_list FROM Table_name WHERE Condition Condition eliminates some of the records from the list. 25

Example SELECT CarRegNo, DriverID FROM Car WHERE Make = BMW 26

Selecting All Attributes To select all attributes we can use * in place of the attribute list SELECT * FROM table_name WHERE condition SELECT * is the same as select operation defined in relational algebra 27

Example 28

Example (Cont.) 29

Attribute Expressions Attributes can be written as expressions. In this case a name can be assigned to the resulted table attribute using as new_name. 30

Logical Expressions in SELECT Query The condition part of a select query can be written using logical expressions with AND, OR and NOT 31

Example 32

Example Find the names and surnames of all employees working in Administration department in London and earning more than 45, or working in Production department in Oxford and earning less than 30. 33

Solution SELECT Name, Surname FROM Employee WHERE (Dept= Administration AND City = London AND Salary > 45 ) OR (Dept= Production AND City = Oxford AND Salary < 30 ) 34

Summary SQL query language is designed to write queries in relational databases Each attribute is defined by using a domain CREATE TABLE is used to create a new table INSERT is used to add records to a table SELECT is used to retrieve data from a table 35

Questions? 36

Term Project Steps Choose your teammate (groups of two at most) Choose a title, e.g. Library Database Send your team members, project title by email to me. (Deadline April 1) (put CENG356 Project at title) Create a list of data items that will be stored in your project. For example, in library project, you may ask the library. Items need not be put in any order. 37

Term Project Steps Find and list all entities. Find the attributes of entities (the attributes are data items that you already found) Define restrictions for each attribute (e.g. Number of digits in student ID, range of values for Age attribute, ) Define relationships between entities and their types (Explain how you found the type of the relationships) 38

Create Entity-Relation (ER) model of your database. Create necessary tables for your database. Show in which normal forms your tables are. Design necessary queries based on the requirements of the project Write necessary SQL commands to create tables and queries Write a report for your project (put all steps explained above) Implement your project using a DBMS (Oracle, MS-SQL, MySQL, MS-Access, ) 39

Evaluation Gathering data items (10) Defining entities, the attributes of the entities, the restrictions on the attributes (15) Relationships (5) ER model (5) Normalization (10) Queries (15) Implementation (15) Report (10) Presentation (15) (is necessary) 40