Private Institute of Aga NETWORK DATABASE LECTURER NIYAZ M. SALIH

Similar documents
3.1. Keys: Super Key, Candidate Key, Primary Key, Alternate Key, Foreign Key

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

SQL Commands & Mongo DB New Syllabus

3ISY402 DATABASE SYSTEMS

Constraints. Primary Key Foreign Key General table constraints Domain constraints Assertions Triggers. John Edgar 2

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

Department of Computer Science University of Cyprus. EPL342 Databases. Lab 1. Introduction to SQL Server Panayiotis Andreou

MySQL. Prof.Sushila Aghav

EGCI 321: Database Systems. Dr. Tanasanee Phienthrakul

SQL Server. Lecture3 Cascading referential integrity constraint

How to use SQL to create a database

A <column constraint> is a constraint that applies to a single column.

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

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

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

Structure Query Language (SQL)

SQL Functionality SQL. Creating Relation Schemas. Creating Relation Schemas

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

Lab # 4. Data Definition Language (DDL)

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

The Structured Query Language Get Started

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

SQL DATA DEFINITION LANGUAGE

Creating and Managing Tables Schedule: Timing Topic

SQL DATA DEFINITION LANGUAGE

Physical Design of Relational Databases

Full file at

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

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

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

Chapter 9: Working with MySQL

MTAT Introduction to Databases

Downloaded from

Data Modelling and Databases. Exercise Session 7: Integrity Constraints

SQL Server 2008 Tutorial 3: Database Creation

DATABASE MANAGEMENT SYSTEMS

CSE 530A. ER Model to Relational Schema. Washington University Fall 2013

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

Using DDL Statements to Create and Manage Tables. Copyright 2004, Oracle. All rights reserved.


Chapter 1 SQL and Data

Chapter 17: Table & Integrity Contraints. Informatics Practices Class XII. By- Rajesh Kumar Mishra. KV No.1, AFS, Suratgarh

Unit 1 - Chapter 4,5

SQL DATA DEFINITION LANGUAGE

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

Using DDL Statements to Create and Manage Tables. Copyright 2004, Oracle. All rights reserved.

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

Basic SQL. Basic SQL. Basic SQL

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

MTAT Introduction to Databases

Data Definition Language (DDL)

Lecture 07. Spring 2018 Borough of Manhattan Community College

Comp 5311 Database Management Systems. 4b. Structured Query Language 3

user specifies what is wanted, not how to find it

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

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

CSE 530A. Inheritance and Partitioning. Washington University Fall 2013

The Relational Model of Data (ii)

CS2300: File Structures and Introduction to Database Systems

Data Infrastructure IRAP Training 6/27/2016

The Relational Model

Building a Test Suite

SQL: Data Definition Language

Tables From Existing Tables

Using DDL Statements to Create and Manage Tables. Copyright 2006, Oracle. All rights reserved.

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

Insert Into Customer1 (ID, CustomerName, City, Country) Values(103, 'Marwa','Baghdad','Iraq')

Department of Computer Science University of Cyprus. EPL342 Databases. Lab 2

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

Database Management Systems,

Keys are fields in a table which participate in below activities in RDBMS systems:

CSCI-UA: Database Design & Web Implementation. Professor Evan Sandhaus Lecture #23: SQLite

Where are we? Week -4: Data definition (Creation of the schema) Week -3: Data definition (Triggers) Week -1: Transactions and concurrency in ORACLE.

Handout 6 CS-605 Spring 18 Page 1 of 7. Handout 6. Physical Database Modeling

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

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

CSCC43H: Introduction to Databases. Lecture 4

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

Simple Quesries in SQL & Table Creation and Data Manipulation

Referential Integrity and Other Table Constraints Ray Lockwood

Full file at

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

Institute of Aga. Network Database LECTURER NIYAZ M. SALIH

SQL: Data Definition Language. csc343, Introduction to Databases Diane Horton Fall 2017

Outline. Data and Operations. Data Types. Integral Types

Assignment 6: SQL III Solution

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

An Introduction to Structured Query Language

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

BEGINNING T-SQL. Jen McCown MidnightSQL Consulting, LLC MinionWare, LLC

Lab # 2 Hands-On. DDL Basic SQL Statements Institute of Computer Science, University of Tartu, Estonia

Introduction to SQL on GRAHAM ED ARMSTRONG SHARCNET AUGUST 2018

The Relational Model. Relational Data Model Relational Query Language (DDL + DML) Integrity Constraints (IC)

Institute of Aga. Microsoft SQL Server LECTURER NIYAZ M. SALIH

Chapter-14 SQL COMMANDS

Introduction to SQL. IT 5101 Introduction to Database Systems. J.G. Zheng Fall 2011

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

Information Systems for Engineers Fall Data Definition with SQL

Oracle Database 10g Express

Relational databases and SQL

Transcription:

Private Institute of Aga 2018 NETWORK DATABASE LECTURER NIYAZ M. SALIH

Data Definition Language (DDL): String data Types: Data Types CHAR(size) NCHAR(size) VARCHAR2(size) Description A fixed-length character string data with a length of size, where the size between 1 and 2000. A fixed-length Unicode character string data with a length of size, where the size between 1 and 1000. A variable-length character string data with a length of size, where the size between 1 and 4000. NVARCHAR2(size) A variable-length Unicode character string data with a length of size, where the size between 1 and 2000. LONG Variable-length character string up to 2 gigabytes Numeric Data Types Data Types NUMBER (p, s) INTEGER DECIMAL (p, s) FLOAT (p) Description Can stores zero, positive, and negative fixed and floating-point numbers Will only store whole (positive, and negative) numbers, such as 5, 93. A DOUBLE stored as a string, allowing for a fixed decimal point. A small number with a floating decimal point. 2

NUMBER (precision, scale), DECIMAL (precision, scale). Precision: is the digits before the decimal number. Scale: is the digits after the decimal number. For example, NUMBER (7, 2) is a number that has 5 digits before the decimal and 2 digits after the decimal. For example, DECIMAL (3, 1) is a number that has 2 digits before the decimal and 1 digit after the decimal. Date Data Types Data Types Date Description Date and Time values to the nearest second between January 1, 4712 B.C., and December 31, 9999 A.D. Difference between CHAR and NCHAR CHAR 1 Can store only non-unicode string data. 2 It just needs 1 byte to store a character. 3 The storage size is equal to the size defined while declaring the field. NCHAR Can store Unicode string data. It needs 2 bytes to store a character. The storage size is equal to double the size defined while declaring the field. 4 Can store 2000 characters. Can store only 1000 characters. 3

The SQL CREATE TABLE Statement The CREATE TABLE statement is used to create a new table in a database. Syntax CREATE TABLE Aga ( ID NUMBER(2), St_Name VARCHAR2(14), ST_Dept VARCHAR2(13) ); DESC Aga The SQL DROP TABLE Statement The DROP TABLE statement is used to drop an existing table in a database. DROP TABLE Aga; 4

SQL ALTER TABLE Statement The ALTER TABLE statement is used to add, delete, or modify columns in an existing table. The ALTER TABLE statement is also used to add and drop various constraints on an existing table. ALTER TABLE - ADD Column To add a column in a table, use the following syntax: Alter Table Aga Add DateOfBirth Date; ALTER TABLE - DROP COLUMN To delete a column in a table, use the following syntax (notice that some database systems don't allow deleting a column): Alter Table Aga Drop Column DateOfBirth Date; 5

ALTER TABLE - MODIFY COLUMN To change the data type of a column in a table, use the following syntax: Alter Table Aga Modify ST_Name NVARCHAR2(22) Data Manipulation Language: The SQL INSERT INTO Statement The INSERT INTO statement is used to insert new records in a table. INSERT INTO Syntax It is possible to write the INSERT INTO statement in two ways. The first way specifies both the column names and the values to be inserted: INSERT INTO Aga (ID,ST_Name,ST_Dept) VALUES(1,'Adam','Network'); SELECT * FROM Aga 6

If you are adding values for all the columns of the table, you do not need to specify the column names in the SQL query. However, make sure the order of the values is in the same order as the columns in the table. The INSERT INTO syntax would be as follows: INSERT INTO Aga VALUES (2,'Aram','Network'); SELECT * FROM Aga The SQL INSERT INTO SELECT Statement The INSERT INTO SELECT statement copies data from one table and inserts it into another table. INSERT INTO SELECT Syntax Copy all columns from one table to another table: CREATE TABLE Aga2 (ID NUMBER(2),St_Name VARCHAR2(14),ST_Dept VARCHAR2(13)); INSERT INTO Aga2 SELECT * FROM Aga; SELECT * FROM Aga2; Copy only some columns from one table into another table: 7

CREATE TABLE Aga3 (ID NUMBER(2),St_Name VARCHAR2(14)); INSERT INTO Aga3 (ID,ST_Name) SELECT ID,ST_Name FROM Aga2; The SQL UPDATE Statement The UPDATE statement is used to modify the existing records in a table. UPDATE Syntax UPDATE Aga SET ST_Dept='Programming' WHERE ID=1; UPDATE Aga SET ST_Dept='Programming' WHERE ST_Name= Aram ; SELECT * FROM Aga The SQL DELETE Statement The DELETE statement is used to delete existing records in a table. 8

DELETE Syntax DELETE FROM Aga WHERE ID=1; DELETE FROM Aga WHERE ST_Name='Aram'; SELECT * FROM Aga Delete All Records It is possible to delete all rows in a table without deleting the table. This means that the table structure, attributes, and indexes will be intact: DELETE FROM Aga; SELECT * FROM Aga; 9

SQL Constraints SQL constraints are used to specify rules for the data in a table. Constraints are used to limit the type of data that can go into a table. This ensures the accuracy and reliability of the data in the table. If there is any violation between the constraint and the data action, the action is aborted. Constraints can be column level or table level. Column level constraints apply to a column, and table level constraints apply to the whole table. The following constraints are commonly used in SQL: NOT NULL - Ensures that a column cannot have a NULL value UNIQUE - Ensures that all values in a column are different PRIMARY KEY - A combination of a NOT NULL and UNIQUE. Uniquely identifies each row in a table FOREIGN KEY - Uniquely identifies a row/record in another table CHECK - Ensures that all values in a column satisfies a specific condition DEFAULT - Sets a default value for a column when no value is specified SQL NOT NULL Constraint By default, a column can hold NULL values. The NOT NULL constraint enforces a column to NOT accept NULL values. This enforces a field to always contain a value, which means that you cannot insert a new record, or update a record without adding a value to this field. The following SQL ensures that the "ID", "LastName", and "FirstName" columns will NOT accept NULL values: CREATE TABLE Persons1 ( ID INT NOT NULL,LastName VARCHAR(255) NOT NULL,FirstName VARCHAR(255) ); DESC Persons1 10

SQL UNIQUE Constraint The UNIQUE constraint ensures that all values in a column are different. Both the UNIQUE and PRIMARY KEY constraints provide a guarantee for uniqueness for a column or set of columns. A PRIMARY KEY constraint automatically has a UNIQUE constraint. However, you can have many UNIQUE constraints per table, but only one PRIMARY KEY constraint per table. SQL UNIQUE Constraint on CREATE TABLE The following SQL creates a UNIQUE constraint on the "ID" column when the "Persons1" table is created: CREATE TABLE Persons2 ( ); ID INT NOT NULL UNIQUE, LastName VARCHAR(255) NOT NULL, FirstName VARCHAR(255) DESC Persons2 SQL UNIQUE Constraint on ALTER TABLE To name a UNIQUE constraint, and to define a UNIQUE constraint on multiple columns, use the following SQL syntax: ALTER TABLE Persons2 ADD CONSTRAINT UC_Person UNIQUE (ID,LastName); 11

DROP a UNIQUE Constraint To drop a UNIQUE constraint, use the following SQL: ALTER TABLE Persons2 DROP CONSTRAINT UC_Person; SQL PRIMARY KEY Constraint The PRIMARY KEY constraint uniquely identifies each record in a database table. Primary keys must contain UNIQUE values, and cannot contain NULL values. A table can have only one primary key, which may consist of single or multiple fields. SQL PRIMARY KEY on CREATE TABLE The following SQL creates a PRIMARY KEY on the "ID" column when the "Persons3" table is created: CREATE TABLE Persons3 ( ID INT NOT NULL PRIMARY KEY, LastName VARCHAR(255) NOT NULL, FirstName VARCHAR(255)); DESC Persons3 12

SQL PRIMARY KEY on ALTER TABLE To allow naming of a PRIMARY KEY constraint, and for defining a PRIMARY KEY constraint on multiple columns, use the following SQL syntax: ALTER TABLE Persons3 ADD CONSTRAINT PK_Person PRIMARY KEY (ID,LastName); DROP a PRIMARY KEY Constraint To drop a PRIMARY KEY constraint, use the following SQL: ALTER TABLE Persons3 DROP CONSTRAINT PK_Person; SQL FOREIGN KEY Constraint A FOREIGN KEY is a key used to link two tables together. A FOREIGN KEY is a field (or collection of fields) in one table that refers to the PRIMARY KEY in another table. The table containing the foreign key is called the child table, and the table containing the candidate key is called the referenced or parent table. Look at the following two tables: "Persons4" table: 13

"Orders" table: Notice that the "PersonID" column in the "Orders" table points to the "PersonID" column in the "Persons4" table. The "PersonID" column in the "Persons" table is the PRIMARY KEY in the "Persons4" table. The "PersonID" column in the "Orders" table is a FOREIGN KEY in the "Orders" table. The FOREIGN KEY constraint is used to prevent actions that would destroy links between tables. The FOREIGN KEY constraint also prevents invalid data from being inserted into the foreign key column, because it has to be one of the values contained in the table it points to. CREATE TABLE Persons4 ( PersonID INT NOT NULL PRIMARY KEY, LastName VARCHAR(255) NOT NULL, FirstName VARCHAR(255)); CREATE TABLE Orders ( 14

OrderID INT NOT NULL PRIMARY KEY, OrderNumber INT NOT NULL, PersonID INT, CONSTRAINT FK_PersonOrder FOREIGN KEY (PersonID) REFERENCES Persons4(PersonID)); SQL FOREIGN KEY on ALTER TABLE To create a FOREIGN KEY constraint on the "PersonID" column when the "Orders" table is already created, use the following SQL: ALTER TABLE Orders ADD CONSTRAINT FK_PersonOrder FOREIGN KEY (PersonID) REFERENCES Persons4(PersonID); DROP a FOREIGN KEY Constraint To drop a FOREIGN KEY constraint, use the following SQL: ALTER TABLE Orders DROP CONSTRAINT FK_PersonOrder; 15

SQL CHECK Constraint The CHECK constraint is used to limit the value range that can be placed in a column. If you define a CHECK constraint on a single column it allows only certain values for this column. If you define a CHECK constraint on a table it can limit the values in certain columns based on values in other columns in the row. SQL CHECK on CREATE TABLE The following SQL creates a CHECK constraint on the "Age" column when the "Persons5" table is created. The CHECK constraint ensures that you cannot have any person below 18 years: CREATE TABLE Persons5 ( ID INT NOT NULL, LastName VARCHAR(255) NOT NULL, FirstName VARCHAR(255), Age INT CHECK (Age>=18)); SQL CHECK on ALTER TABLE To allow naming of a CHECK constraint, and for defining a CHECK constraint on multiple columns, use the following SQL syntax: ALTER TABLE Persons5 ADD CONSTRAINT CHK_PersonAge CHECK (Age>=18 AND Age<=36); 16

DROP a CHECK Constraint To drop a CHECK constraint, use the following SQL: ALTER TABLE Persons5 DROP CHECK CHK_PersonAge; Disable Constraint ALTER TABLE Persons5 DISABLE CONSTRAINT CHK_PersonAge Enable Constraint ALTER TABLE Persons5 ENABLE CONSTRAINT CHK_PersonAge SQL DEFAULT Constraint The DEFAULT constraint is used to provide a default value for a column. The default value will be added to all new records IF no other value is specified. 17

SQL DEFAULT on CREATE TABLE The following SQL sets a DEFAULT value for the "City" column when the "Persons" table is created: CREATE TABLE Persons6 (ID INT NOT NULL, LastName VARCHAR(255) NOT NULL, FirstName VARCHAR(255),City VARCHAR(255) DEFAULT 'Hawler'); DESC Persons6 SQL DEFAULT on ALTER TABLE To create a DEFAULT constraint on the "City" column when the table is already created, use the following SQL: ALTER TABLE Persons6 MODIFY City DEFAULT 'Hawler'; DROP a DEFAULT Constraint To drop a DEFAULT constraint, use the following SQL: ALTER TABLE Persons6 ALTER COLUMN City DROP DEFAULT; 18