Download presentation
Presentation is loading. Please wait.
Published byTechandMate . Modified over 8 years ago
1
Introduction to Structured Query Language (SQL) By Techandmate.comTechandmate.com Learn SQL Server With US
2
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
3
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
4
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
5
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
6
SQL Database Objects A SQL Server database has lot of objects like Tables Views Stored Procedures Functions Rules Defaults Cursors Triggers
7
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.
8
A null value is an unknown Null value is not as zero or space.
9
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
10
The primary key value must be unique and not null. Multiple UNIQUE constraints and only one Primary key in a Table.
11
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
12
used to limit the value range that can be placed in a column. Example : A column must only include integers greater than 0
13
used to insert a default value into a column
18
SELECT * | column1 [, column2, …….] FROM table
20
SELECT * | column1 [, column2, …….] FROM table [WHERE conditions]
23
SELECT * | column1 [, column2, …….] FROM table [WHERE conditions] [ORDER BY column1 [, column2, ……..] ASC, DESC]
25
1 2
28
eliminates duplicate row values from the results Example : the Next Slide
34
SELECT * | column1, group_function (column2), ……. FROM table [WHERE conditions] [GROUP BY column1, ……..] [ORDER BY column1 [, column2, ……..] ASC, DESC]
36
SELECT * | column1, group_function (column2), ……. FROM table [WHERE conditions] [GROUP BY column1, ……..] HAVING [conditions] [ORDER BY column1 [, column2, ……..] ASC, DESC]
39
Arithmetic operators Comparison operators Logical operators Set Operators Other operators
40
addition (+) subtraction (-) multiplication (*) division (/).
41
compare two or more values
43
Used to get a logical value (True or False)
46
EmpLacation Egypt Canada Egypt VacLocation Egypt Canada France Union Egypt Canada France Intersect Egypt Canada Except France
47
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
48
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
49
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
52
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
53
Introduction to Joins Using Inner Joins Using Outer Joins Using Cross Joins Joining More Than Two Tables Joining a Table to Itself
54
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
55
SELECT FROM table1 inner join table2 ON (table1.column1 = table2.column2)
56
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 1 1 1 1 4 4 3 3 2 2 3 3 1 1 5 5 15 5 5 37 11 4 4 2 2 1003 buyersbuyer_namebuyer_name Adam Barr Sean Chai Eva Corets Erin O’Melia buyer_idbuyer_id 1 1 2 2 3 3 4 4 Resultbuyer_namebuyer_name Adam Barr Erin O’Melia Eva Corets buyer_idbuyer_idqtyqty 1 1 1 1 4 4 3 3 15 5 5 37 11 Erin O’Melia 4 4 1003 Example 1
57
SELECT FROM table1 left outer join table2 ON (table1.column1 = table2.column2)
58
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 1 1 1 1 4 4 3 3 2 2 3 3 1 1 5 5 15 5 5 37 11 4 4 2 2 1003 buyersbuyer_namebuyer_name Adam Barr Sean Chai Eva Corets Erin O’Melia buyer_idbuyer_id 1 1 2 2 3 3 4 4 Result buyer_namebuyer_name Adam Barr Erin O’Melia Eva Corets buyer_idbuyer_idqtyqty 1 1 1 1 4 4 3 3 15 5 5 37 11 Erin O’Melia 4 4 1003 Sean Chai NULL Example 1
59
SELECT FROM table1 right outer join table2 ON (table1.column1 = table2.column2)
60
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.
61
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
62
SELECT a.column, b.column FROM Table1 as a JOIN Table1 as b ON a.column = b.column
63
I want Every Employee Name with his Manager
66
Name Ali Islam Country Egypt Cairo CountryName EgyptAli CairoAli EgyptIslam CairoIslam
67
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 15 5 5 37 11 Adam Barr 1003 Sean Chai 15 Sean Chai 5 5 37 Sean Chai 11 Sean Chai 1003 Eva Corets 15... sales buyer_idbuyer_idprod_idprod_idqtyqty 1 1 1 1 4 4 3 3 2 2 3 3 1 1 5 5 15 5 5 37 11 4 4 2 2 1003 buyers buyer_idbuyer_id 1 1 2 2 3 3 4 4 buyer_namebuyer_name Adam Barr Sean Chai Eva Corets Erin O’Melia Example 1
70
INSERT [INTO] table [column1, column2, ………..] VALUES (value1, value2, ………….. )
71
Inserting Data without columns name
72
Inserting Data with columns name Note : You can verify the result by write SELECT statement.
73
SELECT …… INTO FROM [WHERE ]
77
UPDATE SET column1 = value1 [, column2 = value2, ……] WHERE [conditions]
79
DELETE WHERE [conditions]
81
TRUNCATE Table
83
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);
89
CREATE TABLE ( column1 datatype, column2 datatype,............... (
91
Altering an existing table Using User Interface
92
ALTER TABLE Add colum_name datatype ALTER TABLE ALTER COLUMN colum_name datatype ALTER TABLE DROP COLUMN colum_name datatype
94
Disallow null, and decrease length
96
Dropping an existing table
98
DROP TABLE
101
CREATE VIEW AS Create View LondonEmployees as SELECT EmloyeeId, LastName, FirstName,City, Address FROM Employees
104
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
107
Column- level constraints Table-level constraints
108
Creating tables with constraints
115
add a new constraint
118
Create a new Database >> New Database Attach Existing Database >> Attach
119
removes the database entry from the instance closes all files associated to the database releases all operating system locks Detaching a database
134
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
140
SAVE TRANSACTION ROLLBACK TRANSACTION
146
You Can : Contains Statement that Perform Operations Accept input Parameters Return Status Value success or failure Return Multiple Output parameters
147
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.
148
User-defined stored procedures System stored procedures
149
CREATE PROC | PROCEDURE [@parameter data_type] AS
152
ALTER PROC | PROCEDURE [@parameter data_type] AS
153
DROP PROC | PROCEDURE
156
User-defined scalar functions Built-in Functions
158
CREATE FUNCTION [(@parameter data_type)] RETURNS return_data_type | table [AS] BEGIN function_code RETURN scalar_expression | select statement END
168
ALTER FUNCTION [(@parameter data_type)] RETURNS return_data_type [AS] BEGIN function_code RETURN scalar_expression END
169
DROP FUNCTION
172
They can query other tables and include complex Transact-SQL statements They can reference columns in other tables with more complex than check
173
CREATE TRIGGER on INSTEAD OF INSERT | UPDATE | DELETE AS BEGIN END
174
INSTEAD OF INSERT triggers INSTEAD OF UPDATE triggers INSTEAD OF DELETE triggers
181
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 : http://msdn.microsoft.com/en- us/library/aa299742(v=SQL.80).aspxhttp://msdn.microsoft.com/en- us/library/aa299742(v=SQL.80).aspx Google
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.