chapter 4 sql. sql server microsoft sql server is a client/server database management system....

28
Chapter 4 Chapter 4 SQL SQL

Upload: myrtle-lang

Post on 26-Dec-2015

315 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Chapter 4 SQL. SQL server Microsoft SQL Server is a client/server database management system. Microsoft SQL Server is a client/server database management

Chapter 4Chapter 4

SQLSQL

Page 2: Chapter 4 SQL. SQL server Microsoft SQL Server is a client/server database management system. Microsoft SQL Server is a client/server database management

SQL serverSQL server

Microsoft SQL Server is a client/server Microsoft SQL Server is a client/server database management system.database management system.

A client/server database management A client/server database management system consists of two components:system consists of two components: AA front-end front-end component (the client), which is component (the client), which is

used to present and manipulate data; used to present and manipulate data; AA backend backend component (the database component (the database

server), which is used to store, retrieve, and server), which is used to store, retrieve, and protectprotect the databases.the databases.

Page 3: Chapter 4 SQL. SQL server Microsoft SQL Server is a client/server database management system. Microsoft SQL Server is a client/server database management

Components of SQL Components of SQL ServerServer

The commands you primarily use to query a database The commands you primarily use to query a database on a database server areon a database server are

part of the part of the Structured Query Language (SQL).Structured Query Language (SQL). The Structured Query Language isThe Structured Query Language is a standardized set a standardized set

of commands used to work with databases.of commands used to work with databases. Microsoft SQLMicrosoft SQL Server 2000 supports an enhanced Server 2000 supports an enhanced

version of SQL referred to as version of SQL referred to as Transact-SQLTransact-SQL Transact-SQL commands Transact-SQL commands used used to create, maintain, to create, maintain,

and query databasesand query databases MicrosoftMicrosoft SQL Server supports the most recently published SQL Server supports the most recently published

standards for ANSI SQL. Sometimesstandards for ANSI SQL. Sometimes,, the version the version of SQL of SQL implemented in SQL Server referred to as SQL-92.implemented in SQL Server referred to as SQL-92.

Page 4: Chapter 4 SQL. SQL server Microsoft SQL Server is a client/server database management system. Microsoft SQL Server is a client/server database management

Transact-SQLTransact-SQL

Data Definition Language (DDL) statements, Data Definition Language (DDL) statements, which enable you to createwhich enable you to create database objects.database objects.

Data Manipulation Language (DML) Data Manipulation Language (DML) statements, which enable you to querystatements, which enable you to query or or modify data.modify data.

Data Control Language (DCL) statements, Data Control Language (DCL) statements, which enable you to determine,set, or revoke which enable you to determine,set, or revoke users’ permissions to SQL databases and users’ permissions to SQL databases and their objectstheir objects

Page 5: Chapter 4 SQL. SQL server Microsoft SQL Server is a client/server database management system. Microsoft SQL Server is a client/server database management

Microsoft SQL Server 2000 supports Microsoft SQL Server 2000 supports two login authenticationtwo login authentication modesmodes : :

Windows Authentication modeWindows Authentication mode Mixed modeMixed mode

Page 6: Chapter 4 SQL. SQL server Microsoft SQL Server is a client/server database management system. Microsoft SQL Server is a client/server database management

DatabaseDatabase MasterMaster – – Information about the operation of SQL Information about the operation of SQL

Server,including user accounts,Server,including user accounts, other SQL other SQL servers,environment variables,error servers,environment variables,error messages,databases,messages,databases, storage space allocated to storage space allocated to databases,and the tapes and disk drives on thedatabases,and the tapes and disk drives on the SQL SQL server.server.

ModelModel – – A template for creating new databases. SQL A template for creating new databases. SQL Server automatically copiesServer automatically copies the objects in this the objects in this database to each new database you create.database to each new database you create.

MsdbMsdb – – Information about all scheduled jobs,defined Information about all scheduled jobs,defined alerts,and operators on youralerts,and operators on your server. This information server. This information is used by the SQL Server Agent service.is used by the SQL Server Agent service.

TempdbTempdb – –Temporary information. This database is Temporary information. This database is used as a scratchpad by SQLused as a scratchpad by SQL Server.Server.

NorthwindNorthwind, , pubspubs – – A sample database for learning A sample database for learning SQL Server.SQL Server.

Page 7: Chapter 4 SQL. SQL server Microsoft SQL Server is a client/server database management system. Microsoft SQL Server is a client/server database management

Components of SQL Components of SQL ServerServer

The SQL Server ServicesThe SQL Server Services

Page 8: Chapter 4 SQL. SQL server Microsoft SQL Server is a client/server database management system. Microsoft SQL Server is a client/server database management

SQL Server Enterprise SQL Server Enterprise ManagerManager

can also create your owncan also create your own server groups server groups to organize your SQL serversto organize your SQL servers

Page 9: Chapter 4 SQL. SQL server Microsoft SQL Server is a client/server database management system. Microsoft SQL Server is a client/server database management

SQL Query AnalyzerSQL Query Analyzer

UUse SQL Query Analyzer to run SQL se SQL Query Analyzer to run SQL queries as well as to optimize the queries as well as to optimize the performanceperformance of the queries.of the queries.

A query is simply a command you A query is simply a command you send to your server. This query can send to your server. This query can request data from the server, change request data from the server, change data, or delete information.data, or delete information.

Page 10: Chapter 4 SQL. SQL server Microsoft SQL Server is a client/server database management system. Microsoft SQL Server is a client/server database management

SQL Query AnalyzerSQL Query Analyzer

Page 11: Chapter 4 SQL. SQL server Microsoft SQL Server is a client/server database management system. Microsoft SQL Server is a client/server database management

Using SQL Query Using SQL Query AnalyzerAnalyzer

Page 12: Chapter 4 SQL. SQL server Microsoft SQL Server is a client/server database management system. Microsoft SQL Server is a client/server database management

SQL Server Database SQL Server Database StructureStructure

the term the term database database refers to a collection of refers to a collection of tables and othertables and other database objects such as database objects such as indexesindexes

A A table table consists of rows and columns; theseconsists of rows and columns; these rows and columns contain the data for the rows and columns contain the data for the table.table.

A database can contain a virtuallyA database can contain a virtually unlimited unlimited number of tables; each table can contain a number of tables; each table can contain a maximum of 1,024maximum of 1,024 columns (fields).columns (fields).

Page 13: Chapter 4 SQL. SQL server Microsoft SQL Server is a client/server database management system. Microsoft SQL Server is a client/server database management

Designing andDesigning andImplementing DatabasesImplementing Databases

To design, create, and manage To design, create, and manage databasesdatabases::

Identify the issues for designing Identify the issues for designing databasesdatabases

Create and configure databasesCreate and configure databases Manage databasesManage databases

Page 14: Chapter 4 SQL. SQL server Microsoft SQL Server is a client/server database management system. Microsoft SQL Server is a client/server database management

Identifying Database Identifying Database Design IssuesDesign Issues

Databases andDatabases and FilesFiles:: A database is a collection of database A database is a collection of database

objects; these objects include tables,objects; these objects include tables, indexes, views, and stored procedures.indexes, views, and stored procedures.

At a minimum, each database consists of aAt a minimum, each database consists of a primary data file with an extension of .mdf.primary data file with an extension of .mdf.

In addition to its primary data file,you can In addition to its primary data file,you can optionally configure SQL Server to store a optionally configure SQL Server to store a database in a secondary datadatabase in a secondary data file. These file. These files use the extension of .ndf.files use the extension of .ndf.

Page 15: Chapter 4 SQL. SQL server Microsoft SQL Server is a client/server database management system. Microsoft SQL Server is a client/server database management

Identifying Database Identifying Database Design IssuesDesign Issues

Transaction LogsTransaction Logs::In addition to a In addition to a database’s primary data file, you must also database’s primary data file, you must also create a transactioncreate a transaction log for each database.log for each database.

SQL ServerSQL Server automatically assigns the automatically assigns the extension of .ldf to each transaction log file.extension of .ldf to each transaction log file.

SQL Server uses the transactionSQL Server uses the transaction log to make log to make it possible to either recover (roll forward) or it possible to either recover (roll forward) or undo (roll back) aundo (roll back) a transactiontransaction to protect your to protect your database from corruption in the event of a database from corruption in the event of a serverserver crash..crash..

Page 16: Chapter 4 SQL. SQL server Microsoft SQL Server is a client/server database management system. Microsoft SQL Server is a client/server database management

FileFile groupsgroups

When create a database, SQL Server When create a database, SQL Server automatically creates a default automatically creates a default filegroupfilegroup that contains your that contains your database’s primary data filedatabase’s primary data file

Page 17: Chapter 4 SQL. SQL server Microsoft SQL Server is a client/server database management system. Microsoft SQL Server is a client/server database management

SQL Server uses disk space in 8 KB SQL Server uses disk space in 8 KB pagespages.. SQL Server uses some pages to keepSQL Server uses some pages to keep track of track of

the space allocated within a databasethe space allocated within a database Within a database, SQL Server allocates space Within a database, SQL Server allocates space

for database objectsfor database objects such as tables and indexes such as tables and indexes in extentsin extents..

An An extent extent is a contiguous block of eightis a contiguous block of eight pages pages for a total of 64 KB of disk spacefor a total of 64 KB of disk space..

A database consistsA database consists of 16 extents per of 16 extents per megabyte.megabyte.

Page 18: Chapter 4 SQL. SQL server Microsoft SQL Server is a client/server database management system. Microsoft SQL Server is a client/server database management
Page 19: Chapter 4 SQL. SQL server Microsoft SQL Server is a client/server database management system. Microsoft SQL Server is a client/server database management

Creating DatabasesCreating Databases

CCreate a database by usingreate a database by using:: the CREATE DATABASEthe CREATE DATABASE Transact-SQL Transact-SQL

statementstatement.. the Create Database Wizardthe Create Database Wizard.. SQL Server EnterpriseSQL Server Enterprise ManagerManager

Page 20: Chapter 4 SQL. SQL server Microsoft SQL Server is a client/server database management system. Microsoft SQL Server is a client/server database management

Using Using the Create Database the Create Database WizardWizard

Page 21: Chapter 4 SQL. SQL server Microsoft SQL Server is a client/server database management system. Microsoft SQL Server is a client/server database management

Creating a Database Using Creating a Database Using Transact-SQLTransact-SQL

CREATE DATABASE logical_database_nameCREATE DATABASE logical_database_nameONON

PRIMARY (NAME = logical_file_name,PRIMARY (NAME = logical_file_name,FILENAME = 'path\file_name',FILENAME = 'path\file_name',SIZE = size,SIZE = size,MAXSIZE = maxsize,MAXSIZE = maxsize,FILEGROWTH = filegrowth_increment)FILEGROWTH = filegrowth_increment)

LOG ONLOG ON(NAME = logical_file_name,(NAME = logical_file_name,FILENAME = 'path\file_name',FILENAME = 'path\file_name',SIZE = size,SIZE = size,MAXSIZE = maxsize,MAXSIZE = maxsize,FILEGROWTH = filegrowth_increment)FILEGROWTH = filegrowth_increment)

Page 22: Chapter 4 SQL. SQL server Microsoft SQL Server is a client/server database management system. Microsoft SQL Server is a client/server database management

Creating a Database Using Creating a Database Using Transact-SQLTransact-SQL

CREATE DATABASE SalesDb ON (NAME = SalesDb_dat, FILENAME = 'c:\data\salesDB_dat.mdf', SIZE = 10, MAXSIZE = 50, FILEGROWTH = 5) LOG ON (NAME = 'SalesDb_log', FILENAME = 'c:\data\salesDB_log.ldf', SIZE = 5MB, MAXSIZE = 25MB, FILEGROWTH = 10%) GO

Page 23: Chapter 4 SQL. SQL server Microsoft SQL Server is a client/server database management system. Microsoft SQL Server is a client/server database management

Creating a Database Using Creating a Database Using Transact-SQLTransact-SQL

Creating Filegroup CREATE DATABASE logical_database_name ON PRIMARY (NAME = logical_file_name, FILENAME = 'path\file_name.mdf', SIZE = size, MAXSIZE = maxsize, FILEGROWTH = filegrowth_increment) FILEGROUP filegroup_name (NAME = logical_file_name, FILENAME = 'path\file_name.ndf', SIZE = size, MAXSIZE = maxsize, FILEGROWTH = filegrowth_increment) LOG ON (NAME = logical_file_name, FILENAME = 'path\file_name', SIZE = size, MAXSIZE = maxsize, FILEGROWTH = filegrowth_increment)

Page 24: Chapter 4 SQL. SQL server Microsoft SQL Server is a client/server database management system. Microsoft SQL Server is a client/server database management

ALTER DATABASE

Adding Filegroups to an Existing DatabaseALTER DATABASE logical_database_nameADD FILEGROUP new_filegroup_name

Adding FilesALTER DATABASE logical_database_nameADD FILE(NAME = logical_file_name,FILENAME = 'path\file_name.ndf',SIZE = size,MAXSIZE = maxsize,FILEGROWTH = filegrowth_increment)TO FILEGROUP filegroup_name

Page 25: Chapter 4 SQL. SQL server Microsoft SQL Server is a client/server database management system. Microsoft SQL Server is a client/server database management

ALTER DATABASE

Configuring Database OptionsALTER DATABASE database_nameSET option[, status]

ALTER DATABASE moviesSET READ_ONLY

Page 26: Chapter 4 SQL. SQL server Microsoft SQL Server is a client/server database management system. Microsoft SQL Server is a client/server database management

ALTER DATABASE

Expanding a Database and its Transaction LogALTER DATABASE database_nameMODIFY FILE(NAME = 'logical_name',SIZE = size,MAXSIZE = maxsize,FILEGROWTH = filegrowth_increment) support unrestricted file growth by using the

clause MAXSIZE = UNLIMITED

Page 27: Chapter 4 SQL. SQL server Microsoft SQL Server is a client/server database management system. Microsoft SQL Server is a client/server database management

Deleting a Database

DROP DATABASE database_name.

Page 28: Chapter 4 SQL. SQL server Microsoft SQL Server is a client/server database management system. Microsoft SQL Server is a client/server database management

UsingUsing SQL Server Enterprise SQL Server Enterprise ManagerManager