Introduction to SQL  SQL or sequel  It is a standardised language with thousands of pages in the standard  It can be in database system through GUI,

Slides:



Advertisements
Similar presentations
Copyright © by Royal Institute of Information Technology Introduction To Structured Query Language (SQL) 1.
Advertisements

Sanjay Goel, School of Business, University at Albany, SUNY 1 SQL- Data Definition Language ITM 692 Sanjay Goel.
Fundamentals, Design, and Implementation, 9/e Chapter 6 Introduction to Structured Query Language (SQL)
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 Table Basics. Database Objects Tables Temporary tables (begin with #) Views Keys Indexes.
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”
SQL DDL constraints Restrictions on the columns and tables 1SQL DDL Constraints.
Database Constraints. Database constraints are restrictions on the contents of the database or on database operations Database constraints provide a way.
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.
Your Oracle Account UserName is the same as your UWP username Followed Not case sensitive Initial Password: UWPstudent Password is case sensitive.
Oracle Data Definition Language (DDL)
1 IT420: Database Management and Organization SQL: Structured Query Language 25 January 2006 Adina Crăiniceanu
 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.
Constraints  Constraints are used to enforce rules at table level.  Constraints prevent the deletion of a table if there is dependencies.  The following.
CS 3630 Database Design and Implementation. Your Oracle Account UserName is the same as your UWP username Followed Not case sensitive Initial.
CHAPTER:14 Simple Queries in SQL Prepared By Prepared By : VINAY ALEXANDER ( विनय अलेक्सजेंड़र ) PGT(CS),KV JHAGRAKHAND.
Chapter 7 SQL HUANG XUEHUA. SQL SQL server2005 introduction Install components  management studio.
CSC 2720 Building Web Applications Database and SQL.
Oracle Data Definition Language (DDL) Dr. Bernard Chen Ph.D. University of Central Arkansas Fall 2008.
10 Copyright © 2009, Oracle. All rights reserved. Using DDL Statements to Create and Manage Tables.
1 Copyright © 2006, Oracle. All rights reserved. Using DDL Statements to Create and Manage Tables.
Copyright © 2004, Oracle. All rights reserved. Using DDL Statements to Create and Manage Tables.
SQL Data Definition Language (DDL) Using Microsoft SQL Server 1SDL Data Definition Language (DDL)
SQL SQL Server : Overview SQL : Overview Types of SQL Database : Creation Tables : Creation & Manipulation Data : Creation & Manipulation Data : Retrieving.
1 Structured Query Language (SQL). 2 Contents SQL – I SQL – II SQL – III SQL – IV.
Lecture 2: Using DDL Statements to Create and Manage Tables & Indexes
1 Creating and Modifying Database Objects. 2 An Oracle database consists of multiple user accounts Each user account owns database objects Tables Views.
10 Copyright © Oracle Corporation, All rights reserved. Including Constraints.
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 Basics. What is SQL? SQL stands for Structured Query Language. SQL lets you access and manipulate databases.
Tables and Constraints Oracle PL/SQL. Datatypes The SQL Data Definition Language Commands (or DDL) enable us to create, modify and remove database data.
Introduction to Database System Adisak Intana Lecturer Chapter 7 : Data Integrity.
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.
Session 11 Creating Tables and Using Data Types. RDBMS and Data Management/Session 11/2 of 40 Session Objectives Define the data types and list the categories.
SQL: Part 1 Original materials supplied by the Oracle Academic Initiative (OAI). Edited for classroom use by Professor Laku Chidambaram. Not for commercial.
INCLUDING CONSTRAINTS lecture5. Outlines  What are Constraints ?  Constraint Guidelines  Defining Constraint  NOT NULL constraint  Unique constraint.
# 1# 1 Creating Tables, Setting Constraints, and Datatypes What is a constraint and why do we use it? What is a datatype? What does CHAR mean? CS 105.
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,
Lecture5: SQL Overview, Oracle Data Type, DDL and Constraints Ref. Chapter6 Lecture4 1.
SQL CREATING AND MANAGING TABLES lecture4 1. Database Objects ObjectDescription TableBasic unit of storage; composed of rows and columns ViewLogically.
1 Chapter 2: Creating and Modifying Database Objects.
Sql DDL queries CS 260 Database Systems.
©Silberschatz, Korth and Sudarshan1 Structured Query Language (SQL) Data Definition Language Domains Integrity Constraints.
Altering Tables and Constraints Database Systems Objectives Add and modify columns. Add, enable, disable, or remove constraints. Drop a table. Remove.
DDL and Views. Database Objects Logically represents subsets of data from one or more tables View Generates numeric valuesSequence Basic unit of storage;
IS6146 Databases for Management Information Systems Lecture 3: SQL III – The DDL Rob Gleasure robgleasure.com.
Introduction to Database SEM I, AY Department of Information Technology Salalah College of Technology Chapter No.3 SQL.
CSCI N311: Oracle Database Programming 5-1 Chapter 15: Changing Data: insert, update, delete Insert Rollback Commit Update Delete Insert Statement –Allows.
LECTURE FOUR Introduction to SQL DDL with tables DML with tables.
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.
Creating and Managing Tables. Database Objects ObjectDescription TableBasic unit of storage; composed of rows and columns ViewLogically represents subsets.
Fundamentals of DBMS Notes-1.
CS 3630 Database Design and Implementation
Managing Tables, Data Integrity, Constraints by Adrienne Watt
SQL: Schema Definition and Constraints Chapter 6 week 6
Data Definition and Data Types
ORACLE SQL Developer & SQLPLUS Statements
STRUCTURED QUERY LANGUAGE
Lecturer: Mukhtar Mohamed Ali “Hakaale”
SQL data definition using Oracle
Oracle Data Definition Language (DDL)
Session - 6 Sequence - 1 SQL: The Structured Query Language:
Data Definition Language
SQL (Structured Query Language)
Presentation transcript:

Introduction to SQL

 SQL or sequel  It is a standardised language with thousands of pages in the standard  It can be in database system through GUI, or a prompt or embedded in programs  Based on relational algebra  Data Definition Language (DDL)  e.g. create table  Data Manipulation Language (DML)  e.g. select (queries)

To create table we use the command create table with the following syntax CREATE TABLE [schema.]table_name ( column1 data_type [column_properties] [column_constraints],... columnN data_type [column_properties] [column_constraints] )

CREATE TABLE Employee ( emp_id int IDENTITY CONSTRAINT PK_Employee_Emp_id PRIMARY KEY, first_name varchar(25) NOT NULL, last_name varchar(30) NOT NULL, birth_date date NOT NULL, gender char(1) CONSTRAINT CH_Employee_gen CHECK(gender='M' OR gender='F'), varchar(40) CONSTRAINT U_Employee_ UNIQUE, manager_id int, CONSTRAINT FK_Employee_Mgr_id FOREIGN KEY(manager_id) REFERENCES Employee(emp_id) ) Column properties IDENTITY(start_with, increment_by) – it automatically generates unique sequential numbers starting at start_with and increasing by increment_by. If only IDENTITY is specified then both parameters are set to 1. emp_id int IDENTITY NULL – it allows to null values in the column. NULL is default. manager_id int NOT NULL – it prohibits null values in the column. manager_id int NOT NULL first_name varchar(25) NULL

CREATE TABLE Employee ( emp_id int IDENTITY CONSTRAINT PK_Employee_Emp_id PRIMARY KEY, first_name varchar(25) NOT NULL, last_name varchar(30) NOT NULL, birth_date date NOT NULL, gender char(1) CONSTRAINT CH_Employee_gen CHECK(gender='M' OR gender='F'), varchar(40) CONSTRAINT U_Employee_ UNIQUE, manager_id int, CONSTRAINT FK_Employee_Mgr_id FOREIGN KEY(manager_id) REFERENCES Employee(emp_id) ) Column constraints PRIMARY KEY – it enforces uniqueness for the column and prohibits NULL values in it. emp_id int IDENTITY CONSTRAINT PK_Employee_Emp_id PRIMARY KEY FOREIGN KEY – it requires only the values which occurs in the corresponding referenced column. CONSTRAINT FK_Employee_Mgr_id FOREIGN KEY(manager_id) REFERENCES Employee(emp_id) UNIQUE - it guarantees that values in the column are distinct. varchar(40) CONSTRAINT U_Employee_ UNIQUE CHECK – it restricts the inserted values to the values which comply with the specified condition. gender char(1) CONSTRAINT CH_Employee_gen CHECK(gender='M' OR gender='F') DEFAULT – it allows to define a default value for the column. The clause CONSTRAINT constraint_name is optional and if it is omitted, then SQL Server assigns its own name internally to a given constraint.

char[(size)] Fixed-length, non-Unicode character data type of length size bytes (size ). It always has the storage size equal to size bytes (even if the inserted value is shorter). The default length is 1 byte. varchar[(size) | MAX] Variable-length, non-Unicode character data type of length size bytes ( size ). It has the storage size equal to the actual length of the inserted value + 2 bytes, MAX denotes the maximum storage size equal to 2^31 – 1 bytes. nchar[(size)] Fixed-length, non-Unicode character data type of length size bytes ( size ). It always has the storage size equal to two times size bytes. nvarchar[(size) | MAX] Variable-length, non-Unicode character data type of length size bytes ( size  ). It has the storage size equal to two times the actal length of the inserted value + 2 bytes. int Integer data type with the range: 2^31 to 2^31-1 (storage: 4B). smallint Integer data type with the range 2^15 to 2^15-1 (storage: 2B). tinyint Integer data type with the range 0 to 255 (storage: 1B). decimal(p,[s]) numeric(p,[s]) Decimal data types having fixed precision p (maximum number of decimal digits both before and after the decimal point) and scale (maximum number of decimal digits after the decimal point); ( p , s  ). The storage size ranges from 5 to 17 depending on p. float[(m)] Approximate-number data type with the range: E+308 to -2.23E-308, 0 and 2.23E-308 to 1.79E+308. The optional parameter m denotes mantissa ( m  ). The storage size depends on m and it is equal to 4B or 8B. real Approximate-number data type with the range: E + 38 to -1.18E - 38, 0 and 1.18E - 38 to 3.40E + 38 (storage 4B). m oney Monetary data type with the range: -922,337,203,685, to 922,337,203,685, (storage: 8B). smallmoney Monetary data type with the range:- 214, to 214, (storage: 4B).

date Date data type with the range: to (accuracy: 1 day, storage: 3B). time Time data type with the range: 00:00: through 23:59: (accuracy: 100 nanoseconds, storage: from 3B to 5B). datetime Date data type connected with the time. It ranges from to (time range: 00:00:00 through 23:59:59.997). The storage size is equal to 8B. binary[(n)] Fixed-length binary data type of length n bytes ( n, storage: n bytes). The default length is 1B. varbinary[(n|MAX)] Variable-length binary data type of length n bytes ( n ). It has the storage size equal to the actual length of the inserted value + 2 bytes. MAX denotes the maximum storage size equal to 2^31 – 1 bytes.

The ALTER TABLE statement is mainly used to add, alter, or drop columns/constraints in a database table. The basic syntax of this statement is as follows: ALTER TABLE [schema.]table_name alter_column_clause add_column_clause drop_column_clause add_constraint_clause drop_constraint_clause;

Consider the following SQL statements which create two tables: CREATE TABLE Person( person_id int IDENTITY CONSTRAINT PK_Person_P_id PRIMARY KEY, last_name varchar(40) NOT NULL) CREATE TABLE Phone( phone_id int IDENTITY NOT NULL, number varchar(40) NOT NULL) Adding columns Example Add two new columns to both tables. ALTER TABLE Person ADD first_name varchar(20) ALTER TABLE Person ADD birth_date date NOT NULL ALTER TABLE Phone ADD type varchar(20) ALTER TABLE Phone ADD person_id int

Altering columns Example Alter the column Person.first_name (change the data type of first_name from varchar(20) to varchar(30) and set NOT NULL to it). ALTER TABLE Person ALTER COLUMN first_name varchar(30) NOT NULL Example Set NOT NULL to Phone.person_id. ALTER TABLE Phone ALTER COLUMN person_id int NOT NULL

Adding new constraints Example Create a primary key on the column Phone.phone_id. ALTER TABLE Phone ADD CONSTRAINT PK_Phone_P_id PRIMARY KEY(phone_id) Example Create a foreign key on Phone.person_id which refers to Person.person_id. ALTER TABLE Phone ADD CONSTRAINT FK_Person_P_id FOREIGN KEY(person_id) REFERENCES Person(person_id) Example Create a UNIQUE constraint on Phone.number. ALTER TABLE Phone ADD CONSTRAINT U_Phone_number UNIQUE(number)

Dropping columns and constraints Example Remove the column Person.birth_date. ALTER TABLE Person DROP COLUMN birth_date Example Remove the UNIQUE constraint U_Phone_number ALTER TABLE Phone DROP CONSTRAINT U_Phone_number

The DROP TABLE statement is applied to drop database tables: DROP TABLE [schema.]table_name Note: When a column of a table is referenced as the foreign key of another table droping is not allowed!

A view can be considered as a virtual table because its rows are not stored in the database. The view is simply a named query whose definition is kept in the database. Each time when a user runs a query involving the view its rows are retrieved from the tables and/or views on which it is based.

Creating views The basic CREATE VIEW statement has the following syntax CREATE VIEW view_name [WITH SCHEMABINDING] AS select_statement [WITH CHECK OPTION] WITH SCHEMABINDING - it prevents modifications of the view tables which influence its definition. select_statement – it defines a select statement on which the view is based. WITH CHECK OPTION – it prohibits modifications which may produce records that are not included in the result set of select_statement.

Using views Consider the following Student table: CREATE TABLE Student ( [index] varchar(10) CONSTRAINT PK_Student_Index PRIMARY KEY, first_name varchar(25) NOT NULL, last_name varchar(30) NOT NULL, birth_date date NOT NULL, gender char(1) CONSTRAINT CH_Employee_gen CHECK(gender='M' OR gender='F'), varchar(40) ) insert into Student values insert into Student values ('826591','Agnes','Stevens',' insert into Student values ('952184','Justine','Norris','

Example Create a view named V_Stud which stores the indices, names and birth dates of students whose age exceeds 25. CREATE VIEW V_Stud AS SELECT [index],first_name,last_name,birth_date FROM Student WHERE DATEDIFF(year, birth_date, GETDATE())> 20 WITH CHECK OPTION SELECT * FROM V_Stud --the view contains only two rows Example Create a view with schemabinding DROP VIEW V_Stud CREATE VIEW V_Stud WITH SCHEMABINDING AS SELECT [index],first_name,last_name,birth_date FROM dbo.Student WHERE DATEDIFF(year, birth_date, GETDATE())> 20 WITH CHECK OPTION The following statement is prohibited because it removes the birth_date column which is included in the view. The with schemabinding clause protects from such operations. ALTER TABLE Student DROP COLUMN birth_date The following statement is valid: ALTER TABLE Student DROP COLUMN