Introduction to Structured Query Language (SQL) By Techandmate.comTechandmate.com Learn SQL Server With US.

Slides:



Advertisements
Similar presentations
Module 5: Joining Multiple Tables. Overview Using Aliases for Table Names Combining Data from Multiple Tables Combining Multiple Result Sets.
Advertisements

Virtual training week 4 structured query language (SQL)
Database Systems: Design, Implementation, and Management Tenth Edition
Copyright © by Royal Institute of Information Technology Introduction To Structured Query Language (SQL) 1.
Introduction to Structured Query Language (SQL)
Introduction to Structured Query Language (SQL)
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”
Introduction to Structured Query Language (SQL)
Database Systems: Design, Implementation, and Management Eighth Edition Chapter 7 Introduction to Structured Query Language (SQL)
1 Nassau Community CollegeProf. Vincent Costa Acknowledgements: Introduction to Database Management, All Rights ReservedIntroduction to Database Management.
DAY 21: MICROSOFT ACCESS – CHAPTER 5 MICROSOFT ACCESS – CHAPTER 6 MICROSOFT ACCESS – CHAPTER 7 Akhila Kondai October 30, 2013.
SQL Basics. SQL SQL (Structured Query Language) is a special-purpose programming language designed from managing data in relational database management.
Introduction to SQL J.-S. Chou Assistant Professor.
AL-MAAREFA COLLEGE FOR SCIENCE AND TECHNOLOGY INFO 232: DATABASE SYSTEMS CHAPTER 7 INTRODUCTION TO STRUCTURED QUERY LANGUAGE (SQL) Instructor Ms. Arwa.
 SQL stands for Structured Query Language.  SQL lets you access and manipulate databases.  SQL is an ANSI (American National Standards Institute) standard.
CPS120: Introduction to Computer Science Lecture 19 Introduction to SQL.
Structure Query Language SQL. Database Terminology Employee ID 3 3 Last name Small First name Tony 5 5 Smith James
SQL SQL Server : Overview SQL : Overview Types of SQL Database : Creation Tables : Creation & Manipulation Data : Creation & Manipulation Data : Retrieving.
Chapter 5: Part 1: DDL STRUCTURED QUERY LANGUAGE (SQL)
7 1 Chapter 7 Introduction to Structured Query Language (SQL) Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel.
SQL: DDL. SQL Statements DDL - data definition language –Defining and modifying data structures (metadata): database, tables, views, etc. DML - data manipulation.
6 1 Lecture 8: Introduction to Structured Query Language (SQL) J. S. Chou, P.E., Ph.D.
BIS Database Systems School of Management, Business Information Systems, Assumption University A.Thanop Somprasong Chapter # 7 Introduction to Structured.
Topic 1: Introduction to SQL. SQL stands for Structured Query Language. SQL is a standard computer language for accessing and manipulating databases SQL.
Database Systems Design, Implementation, and Management Coronel | Morris 11e ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or.
SQL Unit – 2 Base Knowledge Presented By Mr. R.Aravindhan.
Database Systems Design, Implementation, and Management Coronel | Morris 11e ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or.
1 DBS201: Introduction to Structure Query Language (SQL) Lecture 1.
SQL Basics. What is SQL? SQL stands for Structured Query Language. SQL lets you access and manipulate databases.
SQL. คำสั่ง SQL SQL stands for Structured Query Language is a standard language for accessing and manipulating 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.
Database Lab Lecture 1. Database Languages Data definition language ( DDL ) Data definition language –defines data types and the relationships among them.
Database Fundamental & Design by A.Surasit Samaisut Copyrights : All Rights Reserved.
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,
AL-MAAREFA COLLEGE FOR SCIENCE AND TECHNOLOGY INFO 232: DATABASE SYSTEMS CHAPTER 7 (Part II) INTRODUCTION TO STRUCTURED QUERY LANGUAGE (SQL) Instructor.
IS 380 Introduction to SQL This lectures covers material from: database textbook chapter 3 Oracle chapter: 3,14,17.
ITEC 3220A Using and Designing Database Systems Instructor: Prof. Z. Yang Course Website: 3220a.htm
ITS232 Introduction To Database Management Systems Siti Nurbaya Ismail Faculty of Computer Science & Mathematics, Universiti Teknologi MARA (UiTM), Kedah.
Relational Database Management System(RDBMS) Structured Query Language(SQL)
Distribution of Marks For Second Semester Internal Sessional Evaluation External Evaluation Assignment /Project QuizzesClass Attendance Mid-Term Test Total.
Ch 5. Introducing More Database Objects. Database Objects Table (ch2) View (ch3) Stored Procedure Trigger Function User-defined types.
Manipulating Data Lesson 3. Objectives Queries The SELECT query to retrieve or extract data from one table, how to retrieve or extract data by using.
Introduction to Database SEM I, AY Department of Information Technology Salalah College of Technology Chapter No.3 SQL.
7 1 Database Systems: Design, Implementation, & Management, 7 th Edition, Rob & Coronel 7.6 Advanced Select Queries SQL provides useful functions that.
Unit-8 Introduction Of MySql. Types of table in PHP MySQL supports various of table types or storage engines to allow you to optimize your database. The.
LM 5 Introduction to SQL MISM 4135 Instructor: Dr. Lei Li.
MICROSOFT ACCESS – CHAPTER 5 MICROSOFT ACCESS – CHAPTER 6 MICROSOFT ACCESS – CHAPTER 7 Sravanthi Lakkimsety Mar 14,2016.
 CONACT UC:  Magnific training   
SQL Basics Review Reviewing what we’ve learned so far…….
LEC-8 SQL. Indexes The CREATE INDEX statement is used to create indexes in tables. Indexes allow the database application to find data fast; without reading.
Advanced SQL Advanced Database Dr. AlaaEddin Almabhouh.
COM621: Advanced Interactive Web Development Lecture 11 MySQL – Data Manipulation Language.
Module 5: Joining Multiple Tables. Overview Using Aliases for Table Names Combining Data from Multiple Tables Combining Multiple Result Sets.
Fundamentals of DBMS Notes-1.
Web Systems & Technologies
CHAPTER 7 DATABASE ACCESS THROUGH WEB
SQL Query Getting to the data ……..
Relational Database Design
The Basics of Data Manipulation
Prepared by : Moshira M. Ali CS490 Coordinator Arab Open University
STRUCTURED QUERY LANGUAGE
The Basics of Data Manipulation
Introduction To Structured Query Language (SQL)
Database systems Lecture 3 – SQL + CRUD
Chapter 7 Introduction to Structured Query Language (SQL)
SQL Fundamentals in Three Hours
Introduction To Structured Query Language (SQL)
Contents Preface I Introduction Lesson Objectives I-2
Chapter # 7 Introduction to Structured Query Language (SQL) Part I.
Manipulating Data Lesson 3.
Presentation transcript:

Introduction to Structured Query Language (SQL) By Techandmate.comTechandmate.com Learn SQL Server With US

Objectives 2 Explore basic commands and functions of SQL How to use SQL for data administration (to create tables, indexes, and views) How to use SQL for data manipulation (to add, modify, delete, and retrieve data) How to use SQL to query a database to extract useful information

Agenda What is SQL ? Data Types Constraints Database Relationships SQL Queries Transact- SQL Commands | DDL, DCL,DML Retrieving Data Customizing Data Grouping Data SQL Operators Joining Data

Agenda continue Inserting, Updating and Deleting Data Working With Tables Working with Views Working With Constraints Generating Scripts Working with Stored Procedures Working with Functions

Introduction to SQL 5 SQL stands for Structured Query Language. SQL is used to communicate with a database. According to ANSI (American National Standards Institute), it is the standard language for relational database management systems SQL functions fit into two broad categories: Data definition language SQL includes commands to: Create database objects, such as tables, indexes, and views Define access rights to those database objects Data manipulation language Includes commands to insert, update, delete, and retrieve data within database tables

SQL Database Objects A SQL Server database has lot of objects like Tables Views Stored Procedures Functions Rules Defaults Cursors Triggers

SQL Server Data types Integer: Stores whole number Float: Stores real numbers Text: Stores characters Decimal: Stores real numbers Money: Stores monetary data. Supports 4 places after decimal Date: Stores date and time Binary: Stores images and other large objects Miscellaneous : Different types special to SQL Server.

A null value is an unknown Null value is not as zero or space.

every value in a column or set of columns must be unique. When there is not the primary key. Example no two employees can have the same phone number

The primary key value must be unique and not null. Multiple UNIQUE constraints and only one Primary key in a Table.

FOREIGN KEY in one table points to a PRIMARY KEY in another table. prevents that invalid data form being inserted into the foreign key column

used to limit the value range that can be placed in a column. Example : A column must only include integers greater than 0

used to insert a default value into a column

SELECT * | column1 [, column2, …….] FROM table

SELECT * | column1 [, column2, …….] FROM table [WHERE conditions]

SELECT * | column1 [, column2, …….] FROM table [WHERE conditions] [ORDER BY column1 [, column2, ……..] ASC, DESC]

1 2

eliminates duplicate row values from the results Example : the Next Slide

SELECT * | column1, group_function (column2), ……. FROM table [WHERE conditions] [GROUP BY column1, ……..] [ORDER BY column1 [, column2, ……..] ASC, DESC]

SELECT * | column1, group_function (column2), ……. FROM table [WHERE conditions] [GROUP BY column1, ……..] HAVING [conditions] [ORDER BY column1 [, column2, ……..] ASC, DESC]

Arithmetic operators Comparison operators Logical operators Set Operators Other operators

addition (+) subtraction (-) multiplication (*) division (/).

compare two or more values

Used to get a logical value (True or False)

EmpLacation Egypt Canada Egypt VacLocation Egypt Canada France Union Egypt Canada France Intersect Egypt Canada Except France

USE northwind SELECT (firstname + ' ' + lastname) AS name,city, postalcode FROM employees UNION SELECT companyname, city, postalcode FROM customers GO USE northwind SELECT (firstname + ' ' + lastname) AS name,city, postalcode FROM employees UNION SELECT companyname, city, postalcode FROM customers GO

USE northwind SELECT (firstname + ' ' + lastname) AS name,city, postalcode FROM employees INTERSECT SELECT companyname, city, postalcode FROM customers GO USE northwind SELECT (firstname + ' ' + lastname) AS name,city, postalcode FROM employees INTERSECT SELECT companyname, city, postalcode FROM customers GO

USE northwind SELECT (firstname + ' ' + lastname) AS name,city, postalcode FROM employees EXCEPT SELECT companyname, city, postalcode FROM customers GO USE northwind SELECT (firstname + ' ' + lastname) AS name,city, postalcode FROM employees EXCEPT SELECT companyname, city, postalcode FROM customers GO

Example 1 (without an alias name) Example 2 (with an alias name) USE joindb SELECT buyer_name, s.buyer_id, qty FROM buyers AS b INNER JOIN sales AS s ON b.buyer_id = s.buyer_id GO USE joindb SELECT buyer_name, s.buyer_id, qty FROM buyers AS b INNER JOIN sales AS s ON b.buyer_id = s.buyer_id GO USE joindb SELECT buyer_name, sales.buyer_id, qty FROM buyers INNER JOIN sales ON buyers.buyer_id = sales.buyer_id GO USE joindb SELECT buyer_name, sales.buyer_id, qty FROM buyers INNER JOIN sales ON buyers.buyer_id = sales.buyer_id GO This slide from 2071b from Microsoft

Introduction to Joins Using Inner Joins Using Outer Joins Using Cross Joins Joining More Than Two Tables Joining a Table to Itself

Selects Specific Columns from Multiple Tables JOIN keyword specifies that tables are joined and how to join them ON keyword specifies join condition Queries Two or More Tables to Produce a Result Set Use primary and foreign keys as join conditions Use columns common to specified tables to join tables

SELECT FROM table1 inner join table2 ON (table1.column1 = table2.column2)

USE joindb SELECT buyer_name, sales.buyer_id, qty FROM buyers INNER JOIN sales ON buyers.buyer_id = sales.buyer_id GO USE joindb SELECT buyer_name, sales.buyer_id, qty FROM buyers INNER JOIN sales ON buyers.buyer_id = sales.buyer_id GO salesbuyer_idbuyer_idprod_idprod_idqtyqty buyersbuyer_namebuyer_name Adam Barr Sean Chai Eva Corets Erin O’Melia buyer_idbuyer_id Resultbuyer_namebuyer_name Adam Barr Erin O’Melia Eva Corets buyer_idbuyer_idqtyqty Erin O’Melia Example 1

SELECT FROM table1 left outer join table2 ON (table1.column1 = table2.column2)

USE joindb SELECT buyer_name, sales.buyer_id, qty FROM buyers LEFT OUTER JOIN sales ON buyers.buyer_id = sales.buyer_id GO USE joindb SELECT buyer_name, sales.buyer_id, qty FROM buyers LEFT OUTER JOIN sales ON buyers.buyer_id = sales.buyer_id GO salesbuyer_idbuyer_idprod_idprod_idqtyqty buyersbuyer_namebuyer_name Adam Barr Sean Chai Eva Corets Erin O’Melia buyer_idbuyer_id Result buyer_namebuyer_name Adam Barr Erin O’Melia Eva Corets buyer_idbuyer_idqtyqty Erin O’Melia Sean Chai NULL Example 1

SELECT FROM table1 right outer join table2 ON (table1.column1 = table2.column2)

returns all the rows from the left table, and all the rows from the right table Also it returns rows in the left table that do not have matches in the right table, or if there are rows in right table that do not have matches in the left table.

NameID Some11 Some22 Some33 CountryID Loc11 Loc22 Loc44 SELECT a.id,b.id, a.name, b.country FROM LeftTable as a Full Join RightTable as b On a.id = b.id Full Join

SELECT a.column, b.column FROM Table1 as a JOIN Table1 as b ON a.column = b.column

I want Every Employee Name with his Manager

Name Ali Islam Country Egypt Cairo CountryName EgyptAli CairoAli EgyptIslam CairoIslam

USE joindb SELECT buyer_name, qty FROM buyers CROSS JOIN sales GO USE joindb SELECT buyer_name, qty FROM buyers CROSS JOIN sales GO Result buyer_namebuyer_name Adam Barr qtyqty Adam Barr 1003 Sean Chai 15 Sean Chai Sean Chai 11 Sean Chai 1003 Eva Corets sales buyer_idbuyer_idprod_idprod_idqtyqty buyers buyer_idbuyer_id buyer_namebuyer_name Adam Barr Sean Chai Eva Corets Erin O’Melia Example 1

INSERT [INTO] table [column1, column2, ………..] VALUES (value1, value2, ………….. )

Inserting Data without columns name

Inserting Data with columns name Note : You can verify the result by write SELECT statement.

SELECT …… INTO FROM [WHERE ]

UPDATE SET column1 = value1 [, column2 = value2, ……] WHERE [conditions]

DELETE WHERE [conditions]

TRUNCATE Table

MERGE INTO table_name table_alias USING (table|view|sub_query) alias ON (join condition) WHEN MATCHED THEN UPDATE SET col1 = col_val1, col2 = col2_val WHEN NOT MATCHED THEN INSERT (column_list) VALUES (column_values);

CREATE TABLE ( column1 datatype, column2 datatype, (

Altering an existing table Using User Interface

ALTER TABLE Add colum_name datatype ALTER TABLE ALTER COLUMN colum_name datatype ALTER TABLE DROP COLUMN colum_name datatype

Disallow null, and decrease length

Dropping an existing table

DROP TABLE

CREATE VIEW AS Create View LondonEmployees as SELECT EmloyeeId, LastName, FirstName,City, Address FROM Employees

Any changes to View that may cause a record disappear from the view, will raises runtime error. Example : Create view SomeOrders as SELECT * FROM Orders where Orders.ShipCity = 'London‘ With Check Option

Column- level constraints Table-level constraints

Creating tables with constraints

add a new constraint

Create a new Database >> New Database Attach Existing Database >> Attach

removes the database entry from the instance closes all files associated to the database releases all operating system locks Detaching a database

Autocommit transactions  The default for SQL Server  Each individual DML or DDL statement is committed when it Completes  You do not have to specify any statements to control transactions Implicit transactions  SET IMPLICIT_TRANSACTIONS ON  Each Transact-SQL statement (INSERT,UPDATE,DELETE) execute as a Transaction Explicit transactions  Starting with BEGIN TRANSACTION Statement

SAVE TRANSACTION ROLLBACK TRANSACTION

You Can : Contains Statement that Perform Operations Accept input Parameters Return Status Value success or failure Return Multiple Output parameters

They are registered (permanently stored) at the server. They can have security attributes (such as permissions). They can enhance the security of your application. They allow modular programming. They are named code and so they allow repeating execution. They can reduce network traffic.

User-defined stored procedures System stored procedures

CREATE PROC | PROCEDURE data_type] AS

ALTER PROC | PROCEDURE data_type] AS

DROP PROC | PROCEDURE

User-defined scalar functions Built-in Functions

CREATE FUNCTION data_type)] RETURNS return_data_type | table [AS] BEGIN function_code RETURN scalar_expression | select statement END

ALTER FUNCTION data_type)] RETURNS return_data_type [AS] BEGIN function_code RETURN scalar_expression END

DROP FUNCTION

They can query other tables and include complex Transact-SQL statements They can reference columns in other tables with more complex than check

CREATE TRIGGER on INSTEAD OF INSERT | UPDATE | DELETE AS BEGIN END

INSTEAD OF INSERT triggers INSTEAD OF UPDATE triggers INSTEAD OF DELETE triggers

This is not every thing about Transact-SQL This is just an introduction May you read : 2071B Book ( Querying MS SQL Server with Transact-SQL ) 2073A Book( Programming MS SQL Server Database ) MSDN : us/library/aa299742(v=SQL.80).aspxhttp://msdn.microsoft.com/en- us/library/aa299742(v=SQL.80).aspx Google