company logo 1 database creation and maintenance jorge g. martinez

44
1 Company LOGO Database Creation and Maintenance Jorge G. Martinez

Upload: preston-welch

Post on 16-Jan-2016

220 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Company LOGO 1 Database Creation and Maintenance Jorge G. Martinez

1

Company

LOGO

Database Creation and Maintenance

Jorge G. Martinez

Page 2: Company LOGO 1 Database Creation and Maintenance Jorge G. Martinez

2

Agenda

1. Overview of MS SQL 2005 1. Overview of MS SQL 2005

2. Managing Databases2. Managing Databases

3. DDL: Create, Alter, Drop3. DDL: Create, Alter, Drop

4. DML: Select, Insert, Delete, Update 4. DML: Select, Insert, Delete, Update

5. SSMS: Demos 5. SSMS: Demos

Page 3: Company LOGO 1 Database Creation and Maintenance Jorge G. Martinez

3

Overview of MS SQL 2005 (Structured Query Language)

SQL server is relatively easy to manage

SQL server scales from a mobile laptop to symmetric multiprocessor (SMP) systems.

SQL server provides business intelligence features that until now have only been available in Oracle.

Different editions of SQL 2005: Express, Workgroup, Standard, and Enterprise Edition.

SQL Server 2005 Express Edition allows you to run database applications on desktop and small servers. It is the updated version of MSDE and it is free.

Page 4: Company LOGO 1 Database Creation and Maintenance Jorge G. Martinez

4

Managing Databases

Transact-SQL or T-SQL

(SSMS) SQL Server Management Studio and its component Object Explorer

Page 5: Company LOGO 1 Database Creation and Maintenance Jorge G. Martinez

5

SQL Database language

SQL contains two sublanguages

Data definition language (DDL)

Data manipulation language (DML)

Page 6: Company LOGO 1 Database Creation and Maintenance Jorge G. Martinez

6

Data definition language (DDL)

DDL contains three generic SQL statements:

CREATE objectALTER objectDROP object

Page 7: Company LOGO 1 Database Creation and Maintenance Jorge G. Martinez

7

CREATE statement

Use master

CREATE DATABASE sampleCREATE TABLE sample

Page 8: Company LOGO 1 Database Creation and Maintenance Jorge G. Martinez

8

ALTER statement

USE northwind

ALTER TABLE employee

ADD telephone_no CHAR(12) NULL

Page 9: Company LOGO 1 Database Creation and Maintenance Jorge G. Martinez

9

DROP statement

USE northwindALTER TABLE employeesDROP COLUMN telephone_no

Page 10: Company LOGO 1 Database Creation and Maintenance Jorge G. Martinez

10

Data manipulation language (DML)

DML encompasses four generic operations for manipulating the database

SELECTINSERTDELETEUPDATE

Page 11: Company LOGO 1 Database Creation and Maintenance Jorge G. Martinez

11

SELECT statement

Ex1. USE northwindSELECT * from employees

Ex2.USE northwindSELECT EmployeeID,LastName,FirstName, TitleFROM Employees

Ex3. Use northwindSELECT EmployeeID,OrderIDFROM ordersWHERE EmployeeID =‘5’AND ShipVia =‘3’

Page 12: Company LOGO 1 Database Creation and Maintenance Jorge G. Martinez

12

INSERT statement

USE northwind

INSERT INTO region

VALUES (5,'America')

Page 13: Company LOGO 1 Database Creation and Maintenance Jorge G. Martinez

13

DELETE statement

Deletes any employee with last name king in the employees table from the database name northwind.

USE northwind

DELETE FROM Employees

WHERE LastName= 'King'

Page 14: Company LOGO 1 Database Creation and Maintenance Jorge G. Martinez

14

UPDATE statement

USE northwind

UPDATE Employees

SET Title= 'District Manager'

WHERE LastName ='Buchanan'

Page 15: Company LOGO 1 Database Creation and Maintenance Jorge G. Martinez

15

Creating Database Objects Using T-SQL DDL (Data Definition Language)

A database is the main container for tables, views, indexes stored procedures, and other database objects.

Using the CREATE DATABASE statement, you can create a new database along with the files used to store the database.

You can create 32,767 databases on an instance of SQL server.

CREATE DATABASE MyNewDatabase

Page 16: Company LOGO 1 Database Creation and Maintenance Jorge G. Martinez

16

Cont.

When the database is created, two files are also created: a primary file (an .mdf file) and a transaction log file (an .ldf file).

Its is recommended to keep these files in different drives to simplify recovering the database in case of corruption.

Two Files MyNewDatabase.mdf MyNewDatabase.ldf

Page 17: Company LOGO 1 Database Creation and Maintenance Jorge G. Martinez

17

Cont. Sample showing two files

Page 18: Company LOGO 1 Database Creation and Maintenance Jorge G. Martinez

18

Code designating the .mdf and .ldf file locations.

CREATE DATABASE MyNewDatabase ON Primary

(Name = MyNewDatabase,Filename = 'C:\DBData\MyNewDatabase.mdf',Size= 100MB,MaxSize= 200MB,FileGrowth= 10%)

LOG ON(Name = MyNewDatabase_Log,Filename = 'D:\DBData\MyNewDatabase_Log.Ldf',Size= 30MB,MaxSize= 50MB,FileGrowth= 10%)

Page 19: Company LOGO 1 Database Creation and Maintenance Jorge G. Martinez

19

Removing Database Objects

All Transact-SQL statements to remove a database object have the general form:

DROP object object_name DROP DATABASE database1 {,database2…}

It remove one or more databases.

One or more tables can be removed from a database with the following statement:

DROP TABLE table_name1 {,table_name2…}

Page 20: Company LOGO 1 Database Creation and Maintenance Jorge G. Martinez

20

Tables

Tables are objects that contain data. In SQL server 2005, you can create up to

two billion tables per database CREATE TABLE statement creates a

database table

CREATE TABLE WAREHOUSE(HouseID INT PRIMARY KEY,HouseName char (50))

Page 21: Company LOGO 1 Database Creation and Maintenance Jorge G. Martinez

21

Tables Cont.

CREATE TABLE Employees(EmployeeID INT Primary Key,

LastName nvarchar(20),

FirstName nvarchar(25),

MiddleName nvarchar(25),

Username nvarchar(25),

Password nchar(10),

Emailnvarchar(30))

Page 22: Company LOGO 1 Database Creation and Maintenance Jorge G. Martinez

22

Tables Cont.

Create table Client (Customer# NUMBER(4) PRIMARY KEY, LastName VARCHAR2(10), FirstName VARCHAR2(10), Address VARCHAR2(20), City VARCHAR2(12), State VARCHAR2(2), Zip VARCHAR2(5), Referred NUMBER(4));

INSERT INTO CUSTOMERS VALUES (1001, 'MORALES', 'BONITA', 'P.O. BOX 651', 'EASTPOINT', 'FL', '32328', NULL); INSERT INTO CUSTOMERS VALUES (1002, 'THOMPSON', 'RYAN', 'P.O. BOX 9835', 'SANTA MONICA', 'CA', '90404',

NULL); INSERT INTO CUSTOMERS VALUES (1003, 'SMITH', 'LEILA', 'P.O. BOX 66', 'TALLAHASSEE', 'FL', '32306', NULL); INSERT INTO CUSTOMERS VALUES (1004, 'PIERSON', 'THOMAS', '69821 SOUTH AVENUE', 'BOISE', 'ID', '83707',

NULL); INSERT INTO CUSTOMERS VALUES (1005, 'GIRARD', 'CINDY', 'P.O. BOX 851', 'SEATTLE', 'WA', '98115', NULL); INSERT INTO CUSTOMERS VALUES (1006, 'CRUZ', 'MESHIA', '82 DIRT ROAD', 'ALBANY', 'NY', '12211', NULL);

Page 23: Company LOGO 1 Database Creation and Maintenance Jorge G. Martinez

23

SQL 2005 Data Types

Page 24: Company LOGO 1 Database Creation and Maintenance Jorge G. Martinez

24

ISO –International Organization for Standardization.

The ISO synonyms for nchar are national char and national character.

The ISO synonyms for nvarchar are national char varying and national character varying.

Page 25: Company LOGO 1 Database Creation and Maintenance Jorge G. Martinez

25

(SSMS) SQL Server Management Studio

From Database choose New Database

Choose a name for the new databaseOptional Change the settings for the data file and

the log fileAutogrowth, etc

Change the general database optionsAuto create statistics, etc

Page 26: Company LOGO 1 Database Creation and Maintenance Jorge G. Martinez

26

Creating a new database (1)

Page 27: Company LOGO 1 Database Creation and Maintenance Jorge G. Martinez

27

Cont.

Page 28: Company LOGO 1 Database Creation and Maintenance Jorge G. Martinez

28

Creating a new database (2)

After creating the new database you can Set the authorizations for the system users Create Tables Create Views Create Trigger

Page 29: Company LOGO 1 Database Creation and Maintenance Jorge G. Martinez

29

SSMS Creating tables

You can create new tables using The GUI wizard (similar to Access) Choose the name of the fields Set the type of the fields Set possible constraints (“allow nulls”) Define the primary key Define possible foreign keys

A SQL script Allows batch processing

Page 30: Company LOGO 1 Database Creation and Maintenance Jorge G. Martinez

30

DEMO USING SSMS

Page 31: Company LOGO 1 Database Creation and Maintenance Jorge G. Martinez

31

Database Maintenance

Page 32: Company LOGO 1 Database Creation and Maintenance Jorge G. Martinez

32

You can perform backup operations using.

SQL Server Management Studio Transact-SQL statment

Page 33: Company LOGO 1 Database Creation and Maintenance Jorge G. Martinez

33

Backup and Recovery

Backup determines how a copy of the databases or transaction logs is made and which media are used for this process.

SQL server provides static and dynamic backups. Dynamic backup means that a database backup can be performed while users are working on data.

Page 34: Company LOGO 1 Database Creation and Maintenance Jorge G. Martinez

34

SQL Server provides four different backup methods:

Full database backup Differential database backup Transaction log backup Database file (or filegroup) backup

Page 35: Company LOGO 1 Database Creation and Maintenance Jorge G. Martinez

35

Full Database Backup

Captures the state of the database at the time the backup started.

During the full database backup, the system copies the data as well as the schema of all tables of the database and the corresponding file structures.

(all uncommitted transactions in the transaction log are written to the backup media)

Page 36: Company LOGO 1 Database Creation and Maintenance Jorge G. Martinez

36

Differential Backup

As the name implies, only the parts of the database that have changed since the last full database backup are read and then written to the copy.

Page 37: Company LOGO 1 Database Creation and Maintenance Jorge G. Martinez

37

Transaction Log Backup

This backup considers only the changes recorded in the log. For example logical operations that is, change executed using the DML statements INSERT, UPDATE, and DELETE.

Page 38: Company LOGO 1 Database Creation and Maintenance Jorge G. Martinez

38

Backup Using T-SQL Statement

BACKUP DATABASE

BACKUP LOG

BACKUP DATABASE {db_name}

TO device_list

Page 39: Company LOGO 1 Database Creation and Maintenance Jorge G. Martinez

39

Restoring Using T-SQL StatementManual Recovery

RESTORE LABELONLY

RESTORE HEADERONLY

RESTORE FILELISTONLY

RESTORE VERIFYONLY

Page 40: Company LOGO 1 Database Creation and Maintenance Jorge G. Martinez

40

Page 41: Company LOGO 1 Database Creation and Maintenance Jorge G. Martinez

41

Page 42: Company LOGO 1 Database Creation and Maintenance Jorge G. Martinez

42

Page 43: Company LOGO 1 Database Creation and Maintenance Jorge G. Martinez

43

DEMO WITH SSMS

BACKUP DEMO ATTACH & DETACH

Page 44: Company LOGO 1 Database Creation and Maintenance Jorge G. Martinez

44

QUESTIONS

MSDN Virtual Labs

http://msdn.microsoft.com/enus/virtuallabs

/default.aspx