Database Creation and Maintenance Jorge G Martinez Company
Database Creation and Maintenance Jorge G. Martinez Company LOGO 1
Agenda 1. Overview of MS SQL 2005 2. Managing Databases 3. DDL: Create, Alter, Drop 4. DML: Select, Insert, Delete, Update 5. SSMS: Demos 2
Overview of MS SQL 2005 (Structured Query Language) q SQL server is relatively easy to manage q SQL server scales from a mobile laptop to symmetric multiprocessor (SMP) systems. q SQL server provides business intelligence features that until now have only been available in Oracle. q Different editions of SQL 2005: Express, Workgroup, Standard, and Enterprise Edition. q 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. 3
Managing Databases q Transact-SQL or T-SQL q (SSMS) SQL Server Management Studio and its component Object Explorer 4
SQL Database language SQL contains two sublanguages q Data definition language (DDL) q Data manipulation language (DML) 5
Data definition language (DDL) q. DDL contains three generic SQL statements: §CREATE object §ALTER object §DROP object 6
CREATE statement Use master CREATE DATABASE sample CREATE TABLE sample 7
ALTER statement USE northwind ALTER TABLE employee ADD telephone_no CHAR(12) NULL 8
DROP statement USE northwind ALTER TABLE employees DROP COLUMN telephone_no 9
Data manipulation language (DML) DML encompasses four generic operations for manipulating the database §SELECT §INSERT §DELETE §UPDATE 10
SELECT statement Ex 1. USE northwind SELECT * from employees Ex 2. USE northwind SELECT Employee. ID, Last. Name, First. Name, Title FROM Employees Ex 3. Use northwind SELECT Employee. ID, Order. ID FROM orders WHERE Employee. ID =‘ 5’ AND Ship. Via =‘ 3’ 11
INSERT statement USE northwind INSERT INTO region VALUES (5, 'America') 12
DELETE statement § Deletes any employee with last name king in the employees table from the database name northwind. USE northwind DELETE FROM Employees WHERE Last. Name= 'King' 13
UPDATE statement USE northwind UPDATE Employees SET Title= 'District Manager' WHERE Last. Name ='Buchanan' 14
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 My. New. Database 15
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 My. New. Database. mdf My. New. Database. ldf 16
Cont. Sample showing two files 17
Code designating the. mdf and. ldf file locations. § CREATE DATABASE My. New. Database § ON Primary (Name = My. New. Database, Filename = 'C: DBDataMy. New. Database. mdf', Size= 100 MB, Max. Size= 200 MB, File. Growth= 10%) § LOG ON (Name = My. New. Database_Log, Filename = 'D: DBDataMy. New. Database_Log. Ldf', Size= 30 MB, Max. Size= 50 MB, File. Growth= 10%) 18
Removing Database Objects § All Transact-SQL statements to remove a database object have the general form: DROP object_name DROP DATABASE database 1 {, database 2…} § It remove one or more databases. § One or more tables can be removed from a database with the following statement: DROP TABLE table_name 1 {, table_name 2…} 19
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 (House. ID INT PRIMARY KEY, House. Name char (50)) 20
Tables Cont. CREATE TABLE Employees (Employee. ID INT Primary Key, Last. Name nvarchar(20), First. Name nvarchar(25), Middle. Name nvarchar(25), Username nvarchar(25), Password nchar(10), Email nvarchar(30)) 21
Tables Cont. § § § § § Create table Client (Customer# NUMBER(4) PRIMARY KEY, Last. Name VARCHAR 2(10), First. Name VARCHAR 2(10), Address VARCHAR 2(20), City VARCHAR 2(12), State VARCHAR 2(2), Zip VARCHAR 2(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', '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', '12211', NULL); § § § § 22
SQL 2005 Data Types 23
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. 24
(SSMS) SQL Server Management Studio § From Database choose New Database § Choose a name for the new database Optional Change the settings for the data file and the log file Autogrowth, etc Change the general database options Auto create statistics, etc 25
Creating a new database (1) 26
Cont. 27
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 28
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 29
DEMO USING SSMS 30
Database Maintenance 31
§ You can perform backup operations using. SQL Server Management Studio Transact-SQL statment 32
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. 33
SQL Server provides four different backup methods: § Full database backup § Differential database backup § Transaction log backup § Database file (or filegroup) backup 34
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) 35
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. 36
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. 37
Backup Using T-SQL Statement § BACKUP DATABASE § BACKUP LOG BACKUP DATABASE {db_name} TO device_list 38
Restoring Using T-SQL Statement Manual Recovery § RESTORE LABELONLY § RESTORE HEADERONLY § RESTORE FILELISTONLY § RESTORE VERIFYONLY 39
40
41
42
DEMO WITH SSMS q q BACKUP DEMO ATTACH & DETACH 43
QUESTIONS § MSDN Virtual Labs http: //msdn. microsoft. com/enus/virtuallabs /default. aspx 44
- Slides: 44