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

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

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

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

Chapter 1 SQL and Data

SQL DATA DEFINITION LANGUAGE

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

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

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

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

SQL DATA DEFINITION LANGUAGE

Full file at

SQL Functionality SQL. Creating Relation Schemas. Creating Relation Schemas

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

SQL Commands & Mongo DB New Syllabus

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

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

Copyright 2016 Ramez Elmasri and Shamkant B. Navathe

EGCI 321: Database Systems. Dr. Tanasanee Phienthrakul

SQL: Data Definition Language

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

Oracle Create Table Foreign Key On Delete No

The Structured Query Language Get Started

Lab # 4. Data Definition Language (DDL)

MTAT Introduction to Databases

SQL DATA DEFINITION LANGUAGE

Chapter 9: Working with MySQL

CS143: Relational Model

Chapter 13 : Informatics Practices. Class XI ( As per CBSE Board) SQL Commands. New Syllabus Visit : python.mykvs.in for regular updates

CGS 3066: Spring 2017 SQL Reference

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


Data Base Concepts. Course Guide 2

Index. Bitmap Heap Scan, 156 Bitmap Index Scan, 156. Rahul Batra 2018 R. Batra, SQL Primer,

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

How to define a relational schema for a data base?

Oral Questions and Answers (DBMS LAB) Questions & Answers- DBMS

Course Outline and Objectives: Database Programming with SQL

CSIE30600 Database Systems Basic SQL 2. Outline

CSC 453 Database Technologies. Tanu Malik DePaul University

Chapter 4: SQL Basics

Database Management Systems,

Basic SQL. Basic SQL. Basic SQL

user specifies what is wanted, not how to find it

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

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

Private Institute of Aga NETWORK DATABASE LECTURER NIYAZ M. SALIH

Carnegie Mellon Univ. Dept. of Computer Science Database Applications. General Overview - rel. model. Overview - detailed - SQL

1 INTRODUCTION TO EASIK 2 TABLE OF CONTENTS

Module 3 MySQL Database. Database Management System

SQL: DDL. John Ortiz Cs.utsa.edu

Review. Objec,ves. Example Students Table. Database Overview 3/8/17. PostgreSQL DB Elas,csearch. Databases

Simple Quesries in SQL & Table Creation and Data Manipulation

COGS 121 HCI Programming Studio. Week 03 - Tech Lecture

Content: How to transform the relational model into a database schema DDL: create table students(studentnr int, name varchar);

Database Management System 9

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

Oracle User Administration

Database Foundations. 6-3 Data Definition Language (DDL) Copyright 2015, Oracle and/or its affiliates. All rights reserved.

Data Modelling and Databases. Exercise Session 7: Integrity Constraints

CS2300: File Structures and Introduction to Database Systems

SQL Introduction. CS 377: Database Systems

MySQL Introduction. By Prof. B.A.Khivsara

SQL Data Definition Language

Debapriyo Majumdar DBMS Fall 2016 Indian Statistical Institute Kolkata

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

Today Learning outcomes LO2

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

Essential Skills - RDBMS and SQL

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

Introduction to Computer Science and Business

Databases CSCI 201 Principles of Software Development

(ADVANCED) DATABASE SYSTEMS (DATABASE MANAGEMENTS) PROF. DR. HASAN HÜSEYİN BALIK (6 TH WEEK)

! Define terms. ! Interpret history and role of SQL. ! Write single table queries using SQL. ! Establish referential integrity using SQL

GridDB Advanced Edition SQL reference

SQL Interview Questions

MySQL. A practical introduction to database design

DATABASES SQL INFOTEK SOLUTIONS TEAM

Lecture 07. Spring 2018 Borough of Manhattan Community College

Structured Query Language (SQL)

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

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

Solved MCQ on fundamental of DBMS. Set-1

1. Using Bitvise SSH Secure Shell to login to CS Systems Note that if you do not have Bitvise ssh secure shell on your PC, you can download it from

RDBMS-Day3. SQL Basic DDL statements DML statements Aggregate functions

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

Chapter 3: Introduction to SQL

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

INFORMATION TECHNOLOGY NOTES

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

CSCC43H: Introduction to Databases. Lecture 4

SQL Data Definition: Table Creation

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

Structured Query Language (SQL): A Primer on Data Definition Language (DDL)

Carnegie Mellon Univ. Dept. of Computer Science /615 - DB Applications. Today's Party. Example Database. Faloutsos/Pavlo CMU /615

SQL DDL II. CS121: Relational Databases Fall 2017 Lecture 8

Principles of Data Management

Database Systems. Bence Molnár

UNIT-IV (Relational Database Language, PL/SQL)

DB Creation with SQL DDL

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

Transcription:

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

SQL - Structured Query Language SQL is a computer language for communicating with DBSM Nonprocedural (declarative) language What we want, not how we want it (not a sequence of steps) DBMS is responsible for retrieving data Based on relational calculus

SQL History 1970s Structured English Query Language (SEQUEL or SEQL) 1980 renamed to SQL 1986 SQL-86 ANSI standard 1989 SQL-89 1992 SQL-92, bigger change Data types for date and time Set operations Changes to ALTER, DROP SQL:1999, SQL:2003, SQL:2008, SQL:2011

Database servers Oracle MySQL Microsoft SQL Server Mongo DB (NoSQL) PostgreSQL IBM DB2 Microsoft Access Apache Cassandra (NoSQL) SQLite Redis (NoSQL) [1] http://db-engines.com/en/ranking (2016)

SQL parts DDL (Data Definition Language) database structure definition language, including changes to structure CREATE, DROP, ALTER, TRUNCATE DML (Data Manipulation Language) data manipulation including retrieving data (queries) SELECT, INSERT, UPDATE, DELETE DCL (Data Control Language) language part for controlling access to data GRANT, REVOKE

Data types Data type defines possible values in the column (attribute domain) Dedicated to describe domain integrity Several built-in types for working with Strings Dumbers Date and time Multimedia contents (BLOB)

Basic data types INTEGER FLOAT - floating point number NUMBER(n,s) - number with precision CHAR(n) - fixed size string VARCHAR(n) - variable size string CLOB - large (max 8TB) strings (texts) DATE - year, month, day, time, minutes, seconds TIMESTAMP - extends DATE time with fractional seconds

CREATE TABLE Creates table Requires to analyze the domain first! syntax: CREATE TABLE table_name( name1 type1 [constraints], name2 type2 [constraints],... namen typen [constraints] );

CREATE TABLE - example CREATE TABLE FBUser ( username VARCHAR2(30), name VARCHAR2(30), surname VARCHAR2(40), birthday DATE, sex CHAR );

DROP TABLE Drops (deletes) table syntax: DROP TABLE table_name; example: DROP TABLE FBUser;

Preventive table removing Removing previous version of table (view) before creating the new one Enforces name availability Used during development process (testing) syntax DROP VIEW name; CREATE VIEW name AS...; or CREATE OR REPLACE VIEW name AS...;

Recommended steps for table creation 1. Identify columns data types 2. Identify nullable columns (and those that cannot be null) 3. Identify unique columns 4. Identify primary and foreign keys (and their pairs that express relationships) 5. Identify default values 6. Identify constraints for columns (with respect to the domain) 7. Create table

Showing information about table Table description represents: List of table columns (attributes), Their data types, and constraints (if any). syntax: DESC table; DESCRIBE table;

Table constraints Enforcing constraints during data inserting, updating, and removal Mandatory value NOT NULL Unique values UNIQUE Primary key PRIMARY KEY Foreign key (link to primary key) FOREIGN KEY Custom checking CHECK Default value - DEFAULT

Example CREATE TABLE osoba ( username VARCHAR(30) PRIMARY KEY, name VARCHAR(30) NOT NULL, surname VARCHAR(40) NOT NULL, birthday DATE, sex CHAR(1) DEFAULT 'M' );

Comparison of NOT NULL, UNIQUE a PRIMARY KEY NOT NULL column It is not possible to insert NULL value Values can be repeated UNIQUE column NULL value is allowed Values cannot repeat PRIMARY KEY column NULL value is not allowed Values cannot repeat

Primary key - Simple key (single column) CREATE TABLE course ( id INT PRIMARY KEY, title VARCHAR(32) NOT NULL ); CREATE TABLE course ( id INT, Title VARCHAR(32) NOT NULL, CONSTRAINT course_pk PRIMARY KEY (id) );

Primary keys - Composed key (multiple keys) CREATE TABLE course_enlist ( id_course INT, id_student INT, PRIMARY KEY ( id_course, id_student) );

Foreign keys and referential integrity I. Foreign key represents relationship (link) between rows (tuples) Table with foreign key: child table Table with primary key: referenced (parent) table Keyword: REFERENCES syntax: REFERENCES table(column)

Foreign keys and referential integrity II. CREATE TABLE course_enlist ( id_course INT REFERENCES course(id), id_student INT REFERENCES student(id), PRIMARY KEY (id_course, id_student) );

Foreign keys and referential integrity III. CREATE TABLE course_enlist ( id_course INT, id_student INT, PRIMARY KEY (id_course, id_student), FOREIGN KEY(id_course) REFERENCES course(id), FOREIGN KEY(id_student) REFERENCES student(id) );

CHECK constraint Checking contraints during insertion and update of table rows; there can be multiple constraints example: CREATE TABLE student (... class INT NOT NULL CHECK(class>=1 AND class<=5) -- CONSTRAINT class_constraint -- CHECK(class>=1 -- AND class<=5) );

ALTER TABLE Changes structure of existing table ADD COLUMN, DROP COLUMN, or MODIFY COLUMN syntax: ALTER TABLE table operation column [type] [constraints];

ALTER TABLE - example Add a column: ALTER TABLE student ADD sex CHAR; Remove column: ALTER TABLE student DROP COLUMN sex; Change existing column: ALTER TABLE student MODIFY name VARCHAR(20);

Questions?