Module - 1 Intro To SQL Database
Module - 1 Intro To SQL Database
Module - 1 Intro To SQL Database
Overview
Types of Databases
Database Objects
System Tables
System Databases
master
model tempdb msdb
User defined database
Overview
Defining Databases
Using Filegroups
Managing Databases
How Are Databases Created?
sp_helpdb
sp_helpdb database_name
sp_spaceused [objname]
Northwind Database
sys... ...
sys... Orders
sysusers OrdHistYear2
Customers
sysobjects Products OrdHistYear1
Clustered Index
Anderson Stored
PK Anderson FK Procedure
Barr UpdatePhone Trigger
...
EmpNum LastName FirstName CtryCode Extension LastMod
integer varchar(35) varchar(20) char(2) char(6) char(15)
EmployeePhoneView
SELECT lastname, firstname, extension Check
FROM employee x#####
How Are SQL Server Objects Referenced?
Command-line utility
Standards Compliance
Data Control Language Statements
Data Definition Language Statements
Data Manipulation Language Statements
SQL Server Object Names
Guidelines for Naming Database Objects
Data Control Language Statements
Standard Identifiers
First character must be alphabetic
Other characters can include letters, numerals,
or symbols
Identifiers starting with symbols have special uses
Delimited Identifiers
Use when names contain embedded spaces
Use when reserved words are portions of names
Enclose in brackets ([ ]) or quotation marks (" ")
Guidelines for Naming Database Objects
Local Variables
Operators
Types of Functions
Function Examples
Control-of-Flow Language Elements
Comments
Local Variables
Types of Operators
Arithmetic
Comparison
String concatenation
Logical
Types of Functions
Aggregate Functions
SELECT AVG(UnitPrice) FROM Products
Scalar Functions
SELECT DB_NAME() AS 'database'
Function Examples
Result
Region Style
ANSI: 2000
European: 22 Mar 2000 14:20:00:010
Japanese: 2000/03/22
Control-of-Flow Language Elements
Statement Level
BEGIN…END blocks IF USER_NAME() <> 'dbo'
BEGIN
IF…ELSE blocks print('Must be sysadmin
WHILE constructs to Perform Operation',)
RETURN
Row Level END
ELSE
CASE expression DBCC CHECKDB(Northwind)
Comments
In-Line Comments
SELECT ProductName,
(UnitsInStock + UnitsOnOrder) AS Max -- Calculates inventory
, SupplierID
FROM Products
Block Comments
/*
This code retrieves all rows of the products table
and displays the unit price, the unit price increased
by 10 percent, and the name of the product.
*/
SELECT UnitPrice, (UnitPrice * 1.1), ProductName
FROM Products
Lesson: Executing Transact-SQL Statements
EXECUTE
('USE ' + @dbname + ' SELECT * FROM '+ @tablename)
Batches
BEGIN TRANSACTION
UPDATE savings SET balance = (amount - 100)
WHERE custid = 78910
… <Rollback transaction if error>
UPDATE checking SET balance = (amount + 100)
WHERE custid = 78910
… <Rollback transaction if error>
COMMIT TRANSACTION
XML Support