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

Similar documents
Unit 1 - Chapter 4,5

SQL. A C P K Siriwardhana MSc, BSc in Computer Science FIRST COURSE

CSC Web Programming. Introduction to SQL

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

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

Structured Query Language (SQL) lab syllabus 4 th science. SQL is used to communicate with a database. it is the standard language for relational

Downloaded from

Private Institute of Aga NETWORK DATABASE LECTURER NIYAZ M. SALIH

DATABASE MANAGEMENT SYSTEMS

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

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

Structured Query Language

Lab # 4. Data Definition Language (DDL)

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

Data Manipulation Language (DML)

CST272 SQL Server, SQL and the SqlDataSource Page 1

Relational Database Management Systems for Epidemiologists: SQL Part II

Structure Query Language (SQL)

Reference: W3School -

Department of Computer Science & IT

Group A: Assignment No 2

Simple Quesries in SQL & Table Creation and Data Manipulation

Practical 8. SHANKER SINH VAGHELA BAPU INSTITUTE OF TECHNOLGY Subject: - DBMS Branch: - IT/CE Semester: - 3rd. 1. What does SQL stand for?


SQL DATA MANIPULATION. Prepared By: Dr. Vipul Vekariya.

CBSE Revision Notes Class-11 Computer Science (New Syllabus) Unit 3: Data Management (DM-1) Database

CMP-3440 Database Systems

SQL functions fit into two broad categories: Data definition language Data manipulation language

Unit 27 Web Server Scripting Extended Diploma in ICT

Data Definition Language (DDL)

SQL Retrieving Data from Multiple Tables

Database Systems Laboratory 2 SQL Fundamentals

Activant Solutions Inc. SQL 2005: Basic Data Manipulation

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

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

Networks and Web for Health Informatics (HINF 6220)

CS2300: File Structures and Introduction to Database Systems

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

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

Aggregate Functions. Eng. Mohammed Alokshiya. Islamic University of Gaza. Faculty of Engineering. Computer Engineering Dept. Database Lab (ECOM 4113)

Exact Numeric Data Types

NCSS: Databases and SQL

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

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

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

Lecture 07. Spring 2018 Borough of Manhattan Community College

Relational Database Management Systems for Epidemiologists: SQL Part I

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

Microsoft MOS- Using Microsoft Office Access Download Full Version :

Basic SQL. Basic SQL. Basic SQL

Lecture 8. Database vs. Files SQL (I) Introduction to SQL database management systems (DBMS)

Appendix A. Using DML to Modify Data. Contents: Lesson 1: Adding Data to Tables A-3. Lesson 2: Modifying and Removing Data A-8

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

MTA Database Administrator Fundamentals Course

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

SQL Server. Lecture3 Cascading referential integrity constraint

Lab # 6. Using Subqueries and Set Operators. Eng. Alaa O Shama

SQL Server Administration Class 4 of 4. Activant Prophet 21. Basic Data Manipulation

Basic SQL. Dr Paolo Guagliardo. University of Edinburgh. Fall 2016

Chapter-14 SQL COMMANDS

3ISY402 DATABASE SYSTEMS

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

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

SQL BASICS WITH THE SMALLBANKDB STEFANO GRAZIOLI & MIKE MORRIS

CSE 414 Introduction to Data Management. Section 2: Introduction to SQLite

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

Graphical Joins in More Detail

Lecture 5. Monday, September 15, 2014

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

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

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

ITCertMaster. Safe, simple and fast. 100% Pass guarantee! IT Certification Guaranteed, The Easy Way!

DATA CONSTRAINT. Prepared By: Dr. Vipul Vekariya

Get Table Schema In Sql Server 2005 Modify. Column Datatype >>>CLICK HERE<<<

SQL. Structured Query Language

Chapter 9: MySQL for Server-Side Data Storage

Chapter 4: SQL Basics

SQL DATA MANIPULATION. Prepared By: Vipul Vekariya M.E( Computer) Gardi College Of Eng. & Tech. Rajkot.

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

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

How to use SQL to create a database

SQL Functionality SQL. Creating Relation Schemas. Creating Relation Schemas

Referential Integrity and Other Table Constraints Ray Lockwood

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

Get Table Schema In Sql Server 2008 To Add Column If Not Exists >>>CLICK HERE<<<

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

Database Laboratory Note (MS SQL Server 2000)

Alper VAHAPLAR

Downloaded from

CHAPTER 16. More on SQL- Grouping Records and Table Joins

MySQL. Prof.Sushila Aghav

1) Introduction to SQL

Relational Database Language

Language. f SQL. Larry Rockoff COURSE TECHNOLOGY. Kingdom United States. Course Technology PTR. A part ofcenqaqe Learninq

SQL Data Definition Language

DataBase Lab JAVA-DATABASE CONNECTION. Eng. Haneen El-masry

Database design process

CS 250 SQL and SQLite. Benjamin Dicken

Table Joins and Indexes in SQL

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

Transcription:

Data Base Lab Islamic University Gaza Engineering Faculty Computer Department Lab -5- The Microsoft SQL Server Management Studio Part-3- By :Eng.Alaa I.Haniy.

SQL Constraints Constraints are used to limit the type of data that can go into a table. Constraints can be specified when a table is created with the CREATE TABLE statement) or after the table is created with the ALTER TABLE statement). We will focus on the following constraints: NOT NULL UNIQUE PRIMARY KEY FOREIGN KEY CHECK DEFAULT Now we will describe each constraint in details.. SQL NOT NULL Constraint By default, a table column can hold NULL values. The NOT NULL constraint enforces a column to NOT accept NULL values. The NOT NULL constraint enforces a field to always contain a value. This means that you cannot insert a new record, or update a record without adding a value to this field. The following SQL enforces the "PI_Id" column and the "LName" column to not accept NULL values CREATE TABLE Person PI_Id int NOT NULL, LName varchar50) NOT NULL, FName varchar50), Address varchar50), City varchar50) ) 2

SQL UNIQUE Constraint The UNIQUE constraint uniquely identifies each record in a database table. The UNIQUE and PRIMARY KEY constraints both provide a guarantee for uniqueness or a column or set of columns. A PRIMARY KEY constraint automatically has a UNIQUE constraint defined on it. Note that 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 "PI_Id" column when the "Person" table is created: CREATE TABLE Person PI_Id int NOT NULL UNIQUE, LName varchar50) NOT NULL, FName varchar50), Address varchar50), City varchar50) ) To allow naming of a UNIQUE constraint, and for defining a UNIQUE constraint on multiple columns, use the following SQL syntax: CREATE TABLE Person PI_Id int NOT NULL, LName varchar50) NOT NULL, FName varchar50), Address varchar50), City varchar50), CONSTRAINT uc_personid UNIQUE PI_Id,LName)) ) 3

SQL UNIQUE Constraint on ALTER TABLE To create a UNIQUE constraint on the "P_Id" column when the table is already created, use the following SQL: ADD UNIQUE PI_Id) To allow naming of a UNIQUE constraint, and for defining a UNIQUE constraint on multiple columns, use the following SQL syntax: ADD CONSTRAINT uc_personid UNIQUE PI_Id,LName) To DROP a UNIQUE Constraint To drop a UNIQUE constraint, use the following SQL: DROP CONSTRAINT uc_personid SQL PRIMARY KEY Constraint The PRIMARY KEY constraint uniquely identifies each record in a database table. Primary keys must contain unique values. A primary key column cannot contain NULL values. Each table should have a primary key, and each table can have only one primary key. SQL PRIMARY KEY Constraint on CREATE TABLE The following SQL creates a PRIMARY KEY on the "PI_Id" column when the "Person" table is created: CREATE TABLE Person PI_Id int NOT NULL PRIMARY KEY, LName varchar50) NOT NULL, FName varchar50), Address varchar50), City varchar50) ) To allow naming of a PRIMARY KEY constraint, and for defining a PRIMARY KEY constraint on multiple columns, use the following SQL syntax: 4

CREATE TABLE Person PI_Id int NOT NULL, LName varchar50) NOT NULL, FName varchar50), Address varchar50), City varchar50), CONSTRAINT pk_person PRIMARY KEY PI_Id,LName)) SQL PRIMARY KEY Constraint on ALTER TABLE To create a PRIMARY KEY constraint on the "PI_Id" column when the table is already created, use the following SQL: ADD PRIMARY KEY PI_Id) To allow naming of a PRIMARY KEY constraint, and for defining a PRIMARY KEY constraint on multiple columns, use the following SQL syntax: ADD CONSTRAINT pk_person PRIMARY KEY PI_Id,LName) To DROP a PRIMARY KEY Constraint To drop a PRIMARY KEY constraint, use the following SQL: DROP CONSTRAINT pk_person SQL FOREIGN KEY Constraint A FOREIGN KEY in one table points to a PRIMARY KEY in another table. Let's illustrate the foreign key with an example. Look at the following two tables: The "Person" table: 5

The "Orders" table: Note that the "PI_Id" column in the "Orders" table points to the "PI_Id" column in the "Person" table. The "PI_Id" column in the "Person" table is the PRIMARY KEY in the "Person" table. The "PI_Id" 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 link between tables. The FOREIGN KEY constraint also prevents that invalid data is inserted into the foreign key column, because it has to be one of the values contained in the table it points to. 6 SQL FOREIGN KEY Constraint on CREATE TABLE The following SQL creates a FOREIGN KEY on the "PI_Id" column when the "Orders" table is created: CREATE TABLE Orders O_Id int NOT NULL PRIMARY KEY, OrderNo int NOT NULL, PI_Id int FOREIGN KEY REFERENCES PersonPI_Id)) To allow naming of a FOREIGN KEY constraint, and for defining a FOREIGN KEY constraint on multiple columns, use the following SQL syntax: CREATE TABLE Orders O_Id int NOT NULL, OrderNo int NOT NULL, PI_Id int, PRIMARY KEY O_Id), CONSTRAINT fk_perorders FOREIGN KEY PI_Id) REFERENCES PersonPI_Id) ) SQL FOREIGN KEY Constraint on ALTER TABLE To create a FOREIGN KEY constraint on the "P_Id" column when the "Orders" table is already created, use the following SQL: ALTER TABLE Orders ADD FOREIGN KEY PI_Id) REFERENCES PersonPI_Id)

To allow naming of a FOREIGN KEY constraint, and for defining a FOREIGN KEY constraint on multiple columns, use the following SQL syntax: ALTER TABLE Orders ADD CONSTRAINT fk_perorders FOREIGN KEY PI_Id) REFERENCES PersonPI_Id) To DROP a FOREIGN KEY Constraint To drop a FOREIGN KEY constraint, use the following SQL: ALTER TABLE Orders DROP CONSTRAINT fk_perorders 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. 7 SQL CHECK Constraint on CREATE TABLE The following SQL creates a CHECK constraint on the "PI_Id" column when the "Person" table is created. The CHECK constraint specifies that the column "PI_Id" must only include integers greater than 0. CREATE TABLE Persons PI_Id int NOT NULL CHECK PI_Id>0), LName varchar50) NOT NULL, FName varchar50), Address varchar50), City varchar50) ) To allow naming of a CHECK constraint, and for defining a CHECK constraint on multiple columns, use the following SQL syntax: CREATE TABLE Person PI_Id int NOT NULL, LName varchar50) NOT NULL, FName varchar50), Address varchar50),

City varchar50), CONSTRAINT chk_person CHECK PI_Id>0 AND City='Gaza') ) SQL CHECK Constraint on ALTER TABLE To create a CHECK constraint on the "P_Id" column when the table is already created, use the following SQL: ADD CHECK PI_Id>0) To allow naming of a CHECK constraint, and for defining a CHECK constraint on multiple columns, use the following SQL syntax: ADD CONSTRAINT chk_person CHECK PI_Id>0 AND City='Gaza') To DROP a CHECK Constraint To drop a CHECK constraint, use the following SQL: DROP CONSTRAINT chk_person SQL DEFAULT Constraint The DEFAULT constraint is used to insert a default value into a column. The default value will be added to all new records, if no other value is specified. 8 SQL DEFAULT Constraint on CREATE TABLE The following SQL creates a DEFAULT constraint on the "City" column when the "Person" table is created: CREATE TABLE Person PI_Id int NOT NULL, LName varchar50) NOT NULL, FName varchar50), Address varchar50), City varchar50) DEFAULT 'Gaza') The DEFAULT constraint can also be used to insert system values, by using functions like GETDATE): CREATE TABLE Orders O_Id int NOT NULL, OrderNo int NOT NULL, PI_Id int,

OrderDate datetime DEFAULT GETDATE) ) SQL DEFAULT Constraint on ALTER TABLE To create a DEFAULT constraint on the "City" column when the table is already created, use the following SQL: ALTER COLUMN City SET DEFAULT 'Gaza' To DROP a DEFAULT Constraint To drop a DEFAULT constraint, use the following SQL: ALTER COLUMN City DROP DEFAULT DROP TABLE, and DROP DATABASE The DROP TABLE Statement The DROP TABLE statement is used to delete a table. DROP TABLE table_name Example: DROP TABLE Orders select * from Orders The result The DROP DATABASE Statement The DROP DATABASE statement is used to delete a database. DROP DATABASE database_name 9

The TRUNCATE TABLE Statement What if we only want to delete the data inside the table, and not the table itself? Then, use the TRUNCATE TABLE statement: TRUNCATE TABLE table_name Work as same as DELETE from table_name Example: TRUNCATE TABLE Orders select * from Orders The result: SQL ALTER TABLE Statement The ALTER TABLE statement is used to add, delete, or modify columns in an existing table. 01 SQL ALTER TABLE Syntax To add a column in a table, use the following syntax: ALTER TABLE table_name ADD column_name datatype To delete a column in a table, use the following syntax notice that some database systems don't allow deleting a column): ALTER TABLE table_name DROP COLUMN column_name To change the data type of a column in a table, use the following syntax: ALTER TABLE table_name ALTER COLUMN column_name datatype

SQL ALTER TABLE Example Add Column Example: Look at the "Person" table: Now we want to add a column named "Phone" in the "Person" table. We use the following SQL statement: ADD phone varchar7) The "Person" table will now like this: Change Data Type Example Now we want to change the data type of the column named "phone" in the "Person" table. We use the following SQL statement: ALTER COLUMN phone int DROP COLUMN Example Next, we want to delete the column named "Phone" in the "Person" table. We use the following SQL statement: DROP COLUMN phone 00

The "Person" table will now like this: SQL LIKE Operator The LIKE operator is used to search for a specified pattern in a column. SQL LIKE Syntax SELECT column_names) FROM table_name WHERE column_name LIKE pattern SQL Wildcards SQL wildcards can be used when searching for data in a database. SQL wildcards can substitute for one or more characters when searching for data in a database. SQL wildcards must be used with the SQL LIKE operator. With SQL, the following wildcards can be used: Wildcard Description % _ [charlist] A substitute for zero or more characters A substitute for exactly one character [Any single character in charlist SQL Wildcard Examples Using the % Wildcard The "Person" table: 02

Now we want to select the persons living in a city that starts with "G" from the table above. We use the following SELECT statement: Next, we want to select the persons living in a city that ends with an "s" from the "Person" table. We use the following SELECT statement Next, we want to select the persons living in a city that contains the pattern "af" from the "Person" table. We use the following SELECT statement: 03

It is also possible to select the persons living in a city that NOT contains the pattern "af" from the "Person" table, by using the NOT keyword. We use the following SELECT statement: Using the _ Wildcard: Now we want to select the persons with a first name that starts with any character, followed by "oor" from the "Person" table. We use the following SELECT statement: 04

Next, we want to select the persons with a last name that starts with "H", followed by any character, followed by "s", followed by any character, followed by "e" followed by any character from the "Person" table. We use the following SELECT statement: Using the [charlist] Wildcard Now we want to select the persons with a last name that starts with "H" or "W" from the "Person" table. We use the following SELECT statement: Next, we want to select the persons with a last name that do not start with "H" or "W" from the "Person" table.we use the following SELECT statement: 05

SQL IN Operator The IN operator allows you to specify multiple values in a WHERE clause. SQL IN Syntax SELECT column_names) FROM table_name WHERE column_name IN value1,value2,...) IN Operator Example The "Person" table: Now we want to select the persons with a last name equal to "Hashem" or "Anan" from the table above. We use the following SELECT statement: 06

SQL BETWEEN Operator The BETWEEN operator selects a range of data between two values. The values can be numbers, text, or dates. SQL BETWEEN Syntax SELECT column_names) FROM table_name WHERE column_name BETWEEN value1 AND value2 BETWEEN Operator Example 1 The "Person" table: Now we want to select the persons with a last name alphabetically between "Hashem" and "Wady" from the table above. We use the following SELECT statement 07

Example 2 To display the persons outside the range in the previous example, use NOT BETWEEN SQL Alias With SQL, an alias name can be given to a table or to a column. You can give a table or a column another name by using an alias. This can be a Good thing to do if you have very long or complex table names or column names. An alias name could be anything, but usually it is short. SQL Alias Syntax for Tables SELECT column_names) FROM table_name AS alias_name SQL Alias Syntax for Columns SELECT column_name AS alias_name FROM table_name 08

Alias Example Assume we have a table called "Person" and another table called "Product_Orders". We will give the table aliases of "p" an "po" respectively. Now we want to list all the orders that "Ola Hansen" is responsible for. We use the following SELECT po.orderid, p.lastname, p.firstname FROM Person AS p, Product_Orders AS po WHERE p.lastname= Haniya AND p.firstname='alaa The same SELECT statement without aliases: SELECT Product_Orders.OrderID, Person.LastName, Person.FirstName FROM Person, Product_Orders WHERE Persons.LastName='Haniya' AND Person.FirstName='Alaa' As you'll see from the two SELECT statements above; aliases can make queries 09