Вы находитесь на странице: 1из 5

Designing Databases

After completing this chapter, you will be able to:


-Understand the requirements and functions of each system database
-Understand the SQL Server database structure
-Create a database
-Add and alter filegroups.
-Add files to filegroups.
-Detach and attach database
-Understand database recovery models
A database is a container of all objects
-tabless
-views
-triggers
-stored procedures etc.
After installing each and every Microsoft SQL Server database, the following sys
tem databases
are created by default and each serves a specific purpose
-master
-tempdb
-model
-msdb
-resource
-distribution
Master
======
-Is the primary system database
-It contains information about objects within an instance
-without the master database, sql server can not run
Tempdb
======
-It is the global play ground for temporary objects created by internal processe
s
-It is recreated each time the the sql server is recreated
Model
=====
-This is the model for all databases that are created within an instance
-It is used as a template each a new database is created
Msdb
====
-This is the backend for for MS SQL Agent
SQL Server Database Structure
==============================
-Every sql database contains two files
1. data file
2. log file
-The data file contains data and database objects
-The log file contains information that helps in the recoverability of transacti
ons in the database
Creating A Database

====================
-You can create a database using either SSMS or T-SQL
eg
USE master;
CREATE DATABASE SBSChp4TSQL
ON PRIMARY
(NAME='SBSChp4TSQL1', FILENAME = 'C:\MSSQL\SQLData\SBSTSQL1.mdf', SIZE=10MB, MAX
SIZE=20,
FILEGROWTH=10%)
LOG ON
(NAME='SBSChp4TSQL_log', FILENAME = 'C:\MSSQL\SQLData\SBSTSQL_log.ldf',
SIZE=10MB, MAXSIZE=200, FILEGROWTH=20%);
================================================================================
================================
USE Training
CREATE TABLE [dbo].[Customer]
([CustomerID] [int] IDENTITY (1, 1) NOT NULL,
[CustomerName] [varchar] (50) NULL,
[YTDOrders] [int] NULL,
[YTDSales] [int] null,
CONSTRAINT [PK_Customer]
PRIMARY KEY CLUSTERED
([CustomerID] ASC)
WITH (PAD_INDEX = OFF,
STATISTICS_NORECOMPUTE = OFF,
IGNORE_DUP_KEY = OFF,
ALLOW_ROW_LOCKS = ON,
ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
)ON [PRIMARY]
GO
================================================================================
===============================
Creating Stored Procedure
=========================
CREATE PROCEDURE Customer_Upsert
@CustomerID int=null,
@CustomerName nvarchar (40)=null,
@YTDOrders int=null,
@YTDSales int=null
AS
BEGIN
SET NOCOUNT ON;
if(@CustomerID is null)
INSERT INTO [dbo].[Customer]
(CustomerName, YTDOrders, YTDSales)
VALUES (@CustomerName, @YTDOrders, @YTDSales)
else
UPDATE [dbo].[Customer]
SET CustomerName=coalesce
(@CustomerName, CustomerName),
YTDOrders=coalesce
(@YTDOrders, YTDOrders),
YTDSales=coalesce
(@YTDSales, YTDSales)
WHERE CustomerID=@CustomerID

END
GO
========================================================
CREATE DATABASE Players
ON PRIMARY
(NAME='Players', FILENAME = 'C:\Program Files\Microsoft SQL Server\MSSQL12.MSSQL
SERVER\MSSQL\DATA\Players.mdf', SIZE=10MB, MAXSIZE=20,
FILEGROWTH=10%)
LOG ON
(NAME='Players_log', FILENAME = 'C:\Program Files\Microsoft SQL Server\MSSQL12.M
SSQLSERVER\MSSQL\DATA\players_log.ldf',
SIZE=10MB, MAXSIZE=200, FILEGROWTH=20%);
USE Players
CREATE TABLE [dbo].[Player](
[Player_ID] [int] IDENTITY (1, 1) NOT NULL,
[Player-Name] [varchar] (50) NOT NULL,
[Date_Of_Birth] [date],
[Club] [varchar] (50) NOT NULL,
CONSTRAINT [PK_Player]
PRIMARY KEY CLUSTERED
([Player_ID] ASC)
WITH (PAD_INDEX = OFF,
STATISTICS_NORECOMPUTE = OFF,
IGNORE_DUP_KEY = OFF,
ALLOW_ROW_LOCKS = ON,
ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
)ON [PRIMARY]
USE Players
CREATE TABLE [dbo].[Player_Log](
[Player_ID] [int] NOT NULL,
[Player-Name] [varchar] (50) NOT NULL,
[Date_Of_Birth] [date],
[Club] [varchar] (50) NOT NULL,
[Start_Date] [date] NOT NULL,
[Stop_Date] [date],
CONSTRAINT [PK_Players]
PRIMARY KEY CLUSTERED
([Player_ID] ASC)
WITH (PAD_INDEX = OFF,
STATISTICS_NORECOMPUTE = OFF,
IGNORE_DUP_KEY = OFF,
ALLOW_ROW_LOCKS = ON,
ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
)ON [PRIMARY]
======================================
TO TEST THIS CODE
======================================
CREATE FUNCTION f_Player_Ins () RETURNS TRIGGER AS $$
BEGIN
INSERT INTO [dbo].[Player_Log] VALUES (New.Player_ID, New.Player
_Name, New.Date_Of_Birth, New.Club, Current_Date, NULL)
RETURN New
End
$$ language 'plpgsql'
CREATE TRIGGER Player_Ins

AFTER INSERT ON Player


FOR EACH ROW EXECUTE Procedure f_Player_Ins ();
USE AdventureWorks
GO
----Create testable
CREATE TABLE TestTable (FirstName VARCHAR(100), LastName VARCHAR(100))
----INSERT INTO TestTable using SELECT
INSERT INTO TestTable (FirstName, LastName)
SELECT FirstName, LastName
FROM Person.Contact
WHERE EmailPromotion = 2
----Verify that Data in TestTable
SELECT FirstName, LastName
FROM TestTable
----Clean Up Database
DROP TABLE TestTable
GO
================================================================================
RUNNING SOLUTION TRIGGERS
================================================================================
CREATE DATABASE Employees
ON PRIMARY
(NAME='Employees', FILENAME = 'C:\Program Files\Microsoft SQL Server\MSSQL12.MSS
QLSERVER\MSSQL\DATA\Employees.mdf', SIZE=10MB, MAXSIZE=20,
FILEGROWTH=10%)
LOG ON
(NAME='Employees_log', FILENAME = 'C:\Program Files\Microsoft SQL Server\MSSQL12
.MSSQLSERVER\MSSQL\DATA\Employees_log.ldf',
SIZE=10MB, MAXSIZE=200, FILEGROWTH=20%);
USE Employees
CREATE TABLE Employee_Test
(
Emp_ID INT Identity,
Emp_name Varchar(100),
Emp_Sal Decimal (10,2)
)
USE Employees
CREATE TABLE Employee_Test_Audit
(
Emp_ID int,
Emp_name varchar(100),
Emp_Sal decimal (10,2),
Audit_Action varchar(100),
Audit_Timestamp datetime
)
CREATE TRIGGER trgAfterInsert ON [dbo].[Employee_Test]
FOR INSERT
AS
declare @empid int;
declare @empname varchar(100);
declare @empsal decimal(10,2);
declare @audit_action varchar(100);

select @empid=i.Emp_ID from inserted i;


select @empname=i.Emp_Name from inserted i;
select @empsal=i.Emp_Sal from inserted i;
set @audit_action='Inserted Record -- After Insert Trigger.';
insert into Employee_Test_Audit
(Emp_ID,Emp_Name,Emp_Sal,Audit_Action,Audit_Timestamp)
values(@empid,@empname,@empsal,@audit_action,getdate());
PRINT 'AFTER INSERT trigger fired.'
GO
USE Employees
insert into Employee_Test values('Chris',1500);

Вам также может понравиться