0% found this document useful (0 votes)
6 views

Managing Databases and Tables

The document provides an overview of managing databases and tables in SQL Server, detailing the types of system databases, their functions, and how to create, rename, and drop databases. It explains the structure of database files, including primary data files, secondary data files, and transaction log files, and outlines the process for creating tables and managing their data. Additionally, it includes examples of SQL commands for creating and managing databases and tables.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Managing Databases and Tables

The document provides an overview of managing databases and tables in SQL Server, detailing the types of system databases, their functions, and how to create, rename, and drop databases. It explains the structure of database files, including primary data files, secondary data files, and transaction log files, and outlines the process for creating tables and managing their data. Additionally, it includes examples of SQL commands for creating and managing databases and tables.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

Managing Databases and Tables

A database is a collection of tables and objects such as views, indexes,


stored procedures, and triggers. The data stored in a database may be
related to a process, such as an inventory or a payroll. As a database
developer, you might need to create databases to store information. At
times, you might also need to delete a database, if it is not required.
Therefore, it is essential to know how to create and delete a database.
SQL Server supports many databases and contains some standard
system databases. Before creating a database, it is important to
identify the system databases supported by SQL Server and their
importance.

Identifying System Databases in SQL Server


System databases are the standard databases that exist in every
instance of SQL Server. These databases contain a specific set of
tables that are used to store server-specific configurations and
templates for other databases. In addition, these databases contain a
temporary storage area required to query the database. SQL Server
contains the following system databases:

master
tempdb
model
msdb
Resource

The master Database


The master database consists of system tables that keep track of the
server installation as a whole and all the other databases. It records all
the server-specific configuration information, including authorized
users, databases, system configuration settings, and remote servers.
In addition, it records the instance-wide metadata, such as logon
accounts, endpoints, and system configuration settings. The master
database contains critical data that controls the SQL Server
operations. It is advisable not to give any permission to users on the
master database. It is also important to update the backups of the
master database to reflect the changes that take place in the
database as the master database records the existence of all other
databases and the location of those database files. The master
database also stores the initialization information of SQL Server.
Therefore, if the master database is unavailable, the SQL Server
database engine will not be started.
The tempdb Database

The tempdb database is a temporary database that holds all temporary


tables and stored procedures. It is automatically used by the server to
resolve large or nested queries or to sort data before displaying
results to the user. All the temporary tables and results generated by
the GROUP BY, ORDER BY, and DISTINCT clauses are stored in the
tempdb database. The tempdb database is re-created every time SQL
Server is started so that the system always starts with a clean copy of
the database. Temporary tables and stored procedures are dropped
automatically on disconnect or when the system is shut down. Backup
and restore operations are not allowed on tempdb database. Therefore,
you should not save any database object in the tempdb database
because this database is recreated every time SQL Server starts. This
results in losing the data you saved earlier.

The model Database


The model database acts as a template or a prototype for new
databases. Whenever a database is created, the contents of the model
database are copied to the new database. If you modify the model
database, all databases created after the modification will inherit
those changes. The changes include setting permissions or database
options, or adding objects such as tables, functions, or stored
procedures. For example, if you want every new database to contain a
particular database object, you can add the object to the model
database. After this, whenever you create a new database, the object
will be added to the database.

The msdb Database

The msdb database supports SQL Server Agent, which is a tool to


schedule periodic activities of SQL Server, such as backup and
database mailing. SQL Server Agent is used to schedule a job to run in
response to a specific event or on demand. For example, if you want to
back up all the company servers every weekday, you can automate this
task. Schedule the backup to run after 12.00 P.M., Monday through
Friday. If the backup encounters a problem, SQL Server Agent can
record the event and notify you. The msdb database contains task
scheduling, exception handling, alert management, and system operator
information needed for the SQL Executive Service. The msdb
database contains a few system-defined tables that are specific to the
database. As a database developer, you can query this database to
retrieve information on alerts, exceptions, and schedules. For example,
you can query this database to know the schedule for the next backup
and to know the history of previously scheduled backups. You can also
query this database to know how many database e-mail messages have
been sent to the administrator.
The Resource Database
The Resource database is a read-only, hidden system database that
contains all the system objects, such as system-defined procedures
and views, included with SQL Server. It does not contain user data or
user metadata. It makes upgrading to a new version of SQL Server
easier and faster. By design, it is not visible in the Object Explorer
window of SQL Server Management Studio. It also does not have an
entry in master.sys.databases.
Identifying the Database Files

SQL Server maps a database over a set of operating-system files.


Each database is stored as a set of files on the hard disk of the
computer.

These files include:

Primary data file:

The primary data file contains database objects. It can be used for
the system tables and objects. It is the starting point of the database
and points to other files in the database. Every database has one
primary data file. It has a .mdf extension.

Secondary data file:

The secondary data file is used to store user-defined database


objects. Very large databases may need multiple secondary data files
spread across multiple disks. Databases need not have secondary data
files if the primary data file is large enough to hold all the data in the
database. The secondary data file has a .ndf extension.

Transaction log file:

The transaction log file records all modifications that have occurred in
the database and the transactions that caused those modifications.
The transaction log files hold all the transaction information and can
be used to recover a database. At least one transaction log file must
exist for a database. However, there can be more than one transaction
log files. The minimum size of a transaction log file is 512KB. The size
of the transaction log file should be 25 -40 percent of the size of the
database. The log files have a .ldf extension.

Creating Databases

CREATE DATABASE Personnel

Renaming a User-Defined Database

sp_renamedb old_database_name, new_database_name

where, old_database_name database. new_database_nameis the new


name of the database.
For example, the following statement renames the Personnel database
to the Employee database:

sp_renamedb Personnel, Employee

Dropping a User-Defined Database

You can delete a database when it is no longer required. This causes all
the database files and data to be deleted. Only the users with
sysadmin role and the database owner have the permissions to delete a
database. The DROP DATABASE statement is used to delete a
database.

The syntax of the DROP DATABASE statement is:

DROP DATABASE database_name

where, database_nameis the name of the database. The following


statement deletes the Employee database:

DROP DATABASE Employee

Managing Tables
A table is a database object used to store data. Data in a table is
organized in rows and columns. Each row in a table represents a unique
record and each column represents an attribute of the record. The
column names within a table must be unique, but the same column name
can be used in different tables within a database. As a database
developer, you need to create tables to store data. While creating
tables in a relational database, you must ensure that no one enters
invalid data in it. For this, you need to apply certain rules and
constraints on columns that specify the kind of data to be stored. In
addition, you need to specify the relationships between various tables.
If you want to store a large volume of data in a table, you can create a
partitioned table. This helps in improving query performance. In
addition to creating tables, you are responsible for managing tables.
Management of tables involves modifying tables to add columns or to
change the rules imposed on the table. It also involves deleting tables,
when they are no longer required.

Creating a Table
Creating a Table
In SQL Server, you can create a table by using the CREATE TABLE
statement. The syntax of the CREATE TABLE statement is:

Consider the following example. The management of AdventureWorks,


Inc. needs to maintain the leave details of the employees. For this, you
need to create a table named EmployeeLeave in the HumanResources
schema. The following table shows the structure of the EmployeeLeave
table.
CREATE TABLE HumanResources.EmployeeLeave

EmployeeID int NOT NULL,

LeaveStartDate datetime NOT NULL,

LeaveEndDate datetime NOT NULL,

LeaveReason varchar(100),

LeaveType char(2)NOT NULL

)
Example (Creating database and Tables)

IF Not EXISTS (SELECT * FROM sys.databases WHERE name =


'Library')

BEGIN

Create Database Library

END

GO

IF EXISTS (SELECT * FROM sys.databases WHERE name = 'Library')

BEGIN

Drop DATABASE Library

Create Database Library

END

GO

Creating Authors,Books,Publishers table

CREATE TABLE Authors(


au_id varchar(5) primary key NOT NULL,
au_name varchar(50) NULL,
phone varchar(15) NULL,
address varchar(50) NULL,
city varchar(50) NULL,
state varchar(50) NULL,
zip varchar(50) NULL
)
CREATE TABLE books(
book_id varchar(50) NOT NULL,
book_name varchar(50) NULL,
price varchar(50) NULL,
au_id varchar(5) NULL,
pub_id varchar(5) NULL,
dt_of_publish datetime NULL
)

CREATE TABLE Publishers


(
pub_id varchar(5) NOT NULL,
pub_name varchar(50) NULL,
phone varchar(15) NULL,
address varchar(50) NULL,
city varchar(50) NULL,
state varchar(50) NULL,
zip varchar(50) NULL
)

You might also like