Information Technologies and Microsoft SQL Server

37
Information Technologies and Microsoft SQL Server Day 2 by Alper Özpınar [email protected]

description

Information Technologies and Microsoft SQL Server. Day 2 by Alper Özpınar [email protected]. Parts of a database. Attributes (fields) An attribute or field is a component of a record that describes something about an item. Records A record is the representation of an individual item. - PowerPoint PPT Presentation

Transcript of Information Technologies and Microsoft SQL Server

Page 1: Information Technologies and Microsoft SQL Server

Information Technologies and Microsoft SQL Server

Day 2

by Alper Özpı[email protected]

Page 2: Information Technologies and Microsoft SQL Server

Parts of a database

Attributes (fields) An attribute or field is a component of a record that

describes something about an item. Records

A record is the representation of an individual item. Table

A collection of records Database

A collection of tables and rules for accessing the tables

Page 3: Information Technologies and Microsoft SQL Server

What is a relational database?

Originally developed by E.F. Codd in 1970 Organizes data into tables where each item is

a row and the attributes of the item are in columns.

Different from “flat file” databases because you can define “relationships” between items in different tables.

Page 4: Information Technologies and Microsoft SQL Server

Parts of a database

Record

Attribute/Field

Tables

• Records become “rows”• Attributes/fields become “columns”• Rules determine the relationship between the tables and tie the data together to form a database

Page 5: Information Technologies and Microsoft SQL Server

Kinds of Relationships

“One to One” One row of a table matches exactly to another

One person, one id number, one address “One to Many”

One row of a table matches many of another One person, many phone numbers

“Many to Many” One row may match many of another or many

rows match one row of another

Page 6: Information Technologies and Microsoft SQL Server

Creating a database

What information are we trying to store? How do we describe the information? Phone Book/Contact entries

Name Address Company Phone Number URL/Web Page Age Height (in meters) Birthday When we added the entry

Page 7: Information Technologies and Microsoft SQL Server

Data Types

Binary Database specific binary objects Pictures, digital signatures, etc.

Boolean True/False values

Character Fixed width or variable size

Numeric Integer, Real (floating decimal point), Money

Temporal Time, Date, Timestamp

Page 8: Information Technologies and Microsoft SQL Server

Phone Book/Contact Record

Name CharacterAddress

CharacterCompany CharacterPhone Number CharacterURL/Web Page CharacterAge IntegerHeight Real (float)Birthday DateWhen we added the entry Timestamp

Page 9: Information Technologies and Microsoft SQL Server

Basic SQL Commands

Creating tables with CREATE Adding data with INSERT Viewing data with SELECT Removing data with DELETE Modifying data with UPDATE Destroying tables with DROP

Page 10: Information Technologies and Microsoft SQL Server

SQL Select

SELECT "column_name" FROM "table_name“ SELECT * FROM customers SELECT name,surname FROM customers

Page 11: Information Technologies and Microsoft SQL Server

SQL Select & Where

SELECT "column_name"FROM "table_name"WHERE "condition“

SELECT *FROM employeeWHERE salary>1000

Page 12: Information Technologies and Microsoft SQL Server

SQL Select & Where & Conditions

SELECT "column_name"FROM "table_name"WHERE "simple condition"{[AND|OR] "simple condition"}+

SELECT store_nameFROM Store_InformationWHERE Sales > 1000OR (Sales < 500 AND Sales > 275)

Page 13: Information Technologies and Microsoft SQL Server

And & Or

Logical Operations 1 and 1 =1 1 and 0 =0 0 and 1 =0 0 and 0 =0 1 or 1 = 1 0 or 1 = 1 1 or 0 = 1 0 or 0 = 0

Page 14: Information Technologies and Microsoft SQL Server

SQL Select & IN

SELECT "column_name"FROM "table_name"WHERE "column_name" IN ('value1', 'value2', ...)

The number of values in the parenthesis can be one or more, with each values separated by comma. Values can be numerical or characters. If there is only one value inside the parenthesis, this commend is equivalent to

WHERE "column_name" = 'value1'

Page 15: Information Technologies and Microsoft SQL Server

SQL Between

SELECT "column_name"FROM "table_name"WHERE "column_name" BETWEEN 'value1' AND 'value2‘

SELECT *FROM Store_InformationWHERE Date BETWEEN 'Jan-06-1999' AND 'Jan-10-1999'

Page 16: Information Technologies and Microsoft SQL Server

SQL Like

LIKE allows you to do a search based on a pattern rather than specifying exactly what is desired (as in IN) or spell out a range (as in BETWEEN). The syntax for is as follows:

SELECT "column_name"FROM "table_name"WHERE "column_name" LIKE {PATTERN}

SELECT *FROM Store_InformationWHERE store_name LIKE '%AN%'

Page 17: Information Technologies and Microsoft SQL Server

SQL Like

'A_Z': All string that starts with 'A', another character, and end with 'Z'. For example, 'ABZ' and 'A2Z' would both satisfy the condition, while 'AKKZ' would not (because there are two characters between A and Z instead of one).

'ABC%': All strings that start with 'ABC'. For example, 'ABCD' and 'ABCABC' would both satisfy the condition.

'%XYZ': All strings that end with 'XYZ'. For example, 'WXYZ' and 'ZZXYZ' would both satisfy the condition.

'%AN%': All string that contain the pattern 'AN' anywhere. For example, 'LOS ANGELES' and 'SAN FRANCISCO' would both satisfy the condition.

Page 18: Information Technologies and Microsoft SQL Server

SQL Order By

SELECT "column_name"FROM "table_name"[WHERE "condition"]ORDER BY "column_name" [ASC, DESC]

For more columns ORDER BY "column_name1" [ASC, DESC],

"column_name2" [ASC, DESC] SELECT store_name, Sales, Date

FROM Store_InformationORDER BY Sales DESC

Page 19: Information Technologies and Microsoft SQL Server

SQL Aggregate Functions

AVG COUNT MAX MIN SUM The syntax for using functions is, SELECT "function type"("column_name")

FROM "table_name" SELECT SUM(Sales) FROM Store_Information

Page 20: Information Technologies and Microsoft SQL Server

SQL Count

SELECT COUNT("column_name")FROM "table_name"

Select Count(*) from Customers Counts the number of rows in table

Page 21: Information Technologies and Microsoft SQL Server

Count Distinct

COUNT and DISTINCT can be used together in a statement to fetch the number of distinct entries in a table.

SELECT COUNT(DISTINCT store_name)FROM Store_Information

Page 22: Information Technologies and Microsoft SQL Server

SQL Group By

SELECT "column_name1", SUM("column_name2")FROM "table_name"GROUP BY "column_name1“

SELECT store_name, SUM(Sales)FROM Store_InformationGROUP BY store_name

Page 23: Information Technologies and Microsoft SQL Server

SQL Having

Instead of using the WHERE clause in the SQL statement, though, we need to use the HAVING clause, which is reserved for aggregate functions. The HAVING clause is typically placed near the end of the SQL statement, and a SQL statement with the HAVING clause may or may not include the GROUP BY clause. The syntax for HAVING is,

SELECT "column_name1", SUM("column_name2")FROM "table_name"GROUP BY "column_name1"HAVING (arithmetic function condition)

Page 24: Information Technologies and Microsoft SQL Server

SQL Having

SELECT store_name, SUM(sales)FROM Store_InformationGROUP BY store_nameHAVING SUM(sales) > 1500

Page 25: Information Technologies and Microsoft SQL Server

SQL Alias

SELECT "table_alias"."column_name1" "column_alias"FROM "table_name" "table_alias"

Two reason to use Change column name Take data from two table or table alias

SELECT A1.store_name Store, SUM(A1.Sales) "Total Sales"FROM Store_Information A1GROUP BY A1.store_name

Page 26: Information Technologies and Microsoft SQL Server

Joining Tables

Used for combining data from different tables

Page 27: Information Technologies and Microsoft SQL Server

SQL Join

With tables named A1 and A2 SELECT A1.region_name REGION,

SUM(A2.Sales) SALESFROM Geography A1, Store_Information A2WHERE A1.store_name = A2.store_nameGROUP BY A1.region_name

Page 28: Information Technologies and Microsoft SQL Server

Different types of JOINs

“Inner Join” Unmatched rows in either table aren’t printed

“Left Outer Join” All records from the “left” side are printed

“Right Outer Join” All records from the “right” side are printed

“Full Outer Join” All records are printed

Multiple Table Join Join records from multiple tables

Page 29: Information Technologies and Microsoft SQL Server

SQL Union

The purpose of the SQL UNION command is to combine the results of two queries together. In this respect, UNION is somewhat similar to JOIN in that they are both used to related information from multiple tables. One restriction of UNION is that all corresponding columns need to be of the same data type. Also, when using UNION, only distinct values are selected (similar to SELECT DISTINCT).

Page 30: Information Technologies and Microsoft SQL Server

SQL Union

[SQL Statement 1]UNION[SQL Statement 2]

we want to find out all

the dates where there

is a sales transaction.

Page 31: Information Technologies and Microsoft SQL Server

SQL Union

SELECT Date FROM Store_InformationUNIONSELECT Date FROM Internet_Sales

Result:

Page 32: Information Technologies and Microsoft SQL Server

SQL Union ALL

The purpose of the SQL UNION ALL command is also to combine the results of two queries together. The difference between UNION ALL and UNION is that, while UNION only selects distinct values, UNION ALL selects all values.

The syntax for UNION ALL is as follows: [SQL Statement 1]

UNION ALL[SQL Statement 2]

Page 33: Information Technologies and Microsoft SQL Server

SQL Union All

SELECT Date FROM Store_InformationUNION ALLSELECT Date FROM Internet_Sales

Page 34: Information Technologies and Microsoft SQL Server

Record Operations

Page 35: Information Technologies and Microsoft SQL Server

SQL Insert

INSERT INTO table_name (col_name1, … col_namen)

VALUES (value1, …, valuen) INSERT INTO Corvettes(Vette_id,

Body_style, Miles, Year, State)

VALUES (37, 'convertible', 25.5, 1986, 17)

Page 36: Information Technologies and Microsoft SQL Server

SQL Update

To change one or more values of a row in a table UPDATE table_name SET col_name1 = value1, … col_namen = valuen WHERE col_name = value The WHERE clause is the primary key of the rowto be updated UPDATE Corvettes SET Year = 1996 WHERE Vette_id = 17

Page 37: Information Technologies and Microsoft SQL Server

SQL Delete

Delete has the following syntax:

delete rel-name where qualification Example: Fire all those sailors whose rating

is less than 2.

delete sailors

where rating < 2