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

Programming SQL Server 2012: Topics Homework

The document discusses creating and modifying SQL Server databases. It introduces database concepts like data files and transaction log files. It also provides the syntax for creating a database with specified file sizes and locations. Examples are given to create a database, view its properties, change the data file size, and add a new data file.

Uploaded by

nehal
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
67 views

Programming SQL Server 2012: Topics Homework

The document discusses creating and modifying SQL Server databases. It introduces database concepts like data files and transaction log files. It also provides the syntax for creating a database with specified file sizes and locations. Examples are given to create a database, view its properties, change the data file size, and add a new data file.

Uploaded by

nehal
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Programming SQL Server 2012

Database Terms and Concepts and Introducing SQL Server

Topics Homework:
 Introduction to DDL command A. Create a database using T-SQL with
 SQL Server Database Structure  One primary data file of
 Creating database using simple command Size: 10MB
 Creating databases using details information Filegrowth: 10%
 Viewing database information Maxsize: 100MB
 Altering a database  One Log file of
Size: 10MB
Filegrowth: 10%
Maxsize: 100MB
B. Change the size of data file to 20MB
C. Add a data file ‘ESAD_data1’ of size 5MB to
the database with other default options
D. Rename the database to TraineeDB

Important Points:


Summary:


1
→ Data File - It holds actual data
SQL Statements  Transaction log file - it does not hold data. It keeps track
 SQL is a case-insensitive of data modification you make. If you change data SQL
 Every SQL statement end with ; (semi-colon), which server first write what it is going to do in transaction log
optional file and then it makes actual in data in data file.
 We add GO after a single or a group of SQL statements
 GO denotes end of a batch, we will discuss later about Basic CREATE object Syntax
SQL Batches  In simplest form the syntax is
CREATE object-type object-name
SQL Comment  Sometimes you provide extra information during creation
 A single line comment starts with --. For example of the object, the syntax for that
--Insert data into trainees table CREATE object-type object-name
 A multiline comment is enclosed with /* and */ (
/* Extra information
Comment )
*/
 A multi-line comment must be within a single a batch. Let's create a database
The following is invalid  Open SSMS
/*  Click on "new query" tool
--  Write the following
GO CREATE database sqlStore
-- GO
*/  Select the lines
 PRESS F5 or Execute button on toolbar
Running SQL in Query Editor  You will see the following message in messages window
 When Press F5 or Click on Execute button, All the
statements in the selected editor are executed Command(s) completed successfully.
 When you press F5 with a set of statement selected, only
selected statement will be executed View Database information
 Each time a query is executed, messages are shown on  To extract information of an existing database, use
messages window. If any error occurred, you can track it sp_helpdb built-in procedure
seeing the error messages  The syntax is
execute sp_helpdb 'database-name'
Parsing Query  sp_helpdb is a built-in stored procedure. SQL provides a
 Press Ctrl + F5 or click on the parse button (√ marked lot of such procedures. We will learn stored procedures
later
tool)
 SSMS will parse the query without executing it and show Try this
any error is there EXECUTE sp_help_db 'sqlStore'
 You should always track syntax errors before executing it GO
You will see result l showing a lot of information including
What is SQL? 
data and log file
 SQL (Structured Query Language) is a database
language designed for managing data in relational Deleting a database
database management systems  To delete an object, we use drop statement
The syntax is
What is T-SQL? 
DROP object-type object-name
 Known as Transact SQL
 T-SQL (Transact-SQL) is a version of SQL from Sybase and Try this
Microsoft that add several features to the Structured DROP database sqlStore
Query Language (SQL) including transaction control, GO
exception and error handling, row processing, and  You will see
declared variables.
 Microsoft's SQL Server and Sybase's SQL server support T- Command(s) completed successfully.
SQL statements Creating database with file details
DDL & DML  When you create a database using simple create
 SQL statements primarily fall into two group: statement, two files, data and log files, are created at
 DDL (Data Definition Language): DDL allows creating, predefined location (in data folder under SQL server's
altering or removing database objects - like database installation directory with default values of other
itself, tables, stored procedures etc. attributes.
 There are three basic DDL  For default values, SQL Server depends model system
→ CREATE - used to create database objects database.
→ ALTER - used to modify database objects  But you can create database with files at location of
→ DROP - used to delete database object your own choice and explicit values of various file
 DML (Data Manipulation Language): DML allows attributes
retrieving, storing, modifying or deleting data  The syntax is:
 There are four basic DML statements CREATE database database-name
→ SELECT – to retrieve data from table ON
→ INSERT – to store data into a table (
→ UPDATE – to modify data in a table NAME = logical_file_name ,
→ DELETE – to delete data from a table FILENAME = 'os_file_name',
SIZE = size,
SQL Database structure MAXSIZE = max_size,
 A SQL server is made of at least two files FILEGROWTH = growth_increment
 One data file (.mdf) )
→ One transaction log file (.ldf)

2
LOG ON  Let's change the size of the data file of the sqlStore
( database
NAME = logical_file_name , Try this
FILENAME = 'os_file_name', ALTER database sqlStore
SIZE = size, MODIFY file
MAXSIZE = max_size, (
FILEGROWTH = growth_increment name = 'sqlStoredata_1',
) filename = 'F:\sqlStore_data_1.mdf',
 ON(…) - encloses data file specification size = 4mb
 LOG ON (..) - encloses log file specification )
 NAME - Is the logical name used in SQL Server when GO
referencing the file? EXEC sp_helpdb 'sqlStore'
 FILENAME - Specifies the operating system (physical) file GO
name. The path and file name used by the operating
system when you create the file. Adding file to an existing database
 SIZE-Is the initial size of the file. The kilobyte (KB),  The syntax is:
megabyte (MB), gigabyte (GB), or terabyte (TB) units ALTER database
can be used. The default is MB. Specify a whole number. MODIFY FILE
 MAXSIZE - Specifies the maximum size to which the file (
can grow. The kilobyte (KB), megabyte (MB), gigabyte NAME = logical_file_name ,
(GB), or terabyte (TB) suffixes can be used. The default is FILENAME = 'os_file_name',
MB. Specify a whole number. Can be UNLIMITED - the file SIZE = size
grows until the disk is full. MAXSIZE = max_size,
 FILEGROWTH - Is the amount of space added to the file FILEGROWTH = growth_increment
every time new space is required. The value can be )
specified in MB, KB, GB, TB, or percent (%). If a number is  You must specify NAME and FILENAME
specified without an MB, KB, or % suffix, the default is MB. Try this
 NAME and FILENAME are mandatory. ALTER database sqlStore
 Other attributes are optional. ADD file
 You can create database with multiple data and log (
files name = 'sqlStoredata_2',
 The syntax is: filename = 'F:\sqlStore_data_2.ndf',
CREATE database size = 2mb,
ON (…), filegrowth = 10%
(..) )
LOG ON (..), GO
(..) EXEC sp_helpdb 'sqlStore'
Create a database with details GO

Try this Deleting a file from database


CREATE database sqlStore  The syntax is
ON ALTER database database-name
( REMOVE file logical-name
name = 'sqlStoredata_1',  A file must be empty before it can be deleted
filename = 'F:\sqlStore_data_1.mdf', Try this
size = 2mb, ALTER DATABASE sqlStore
maxsize = 50mb, REMOVE FILE sqlStoredata_2
filegrowth = 10% GO
) EXEC sp_helpdb 'sqlStore'
LOG ON GO
(
name = 'sqlStorelog_1', Renaming a database
filename = 'F:\sqlStore_log_1.ldf',  To rename a database, you have to make sure that no
size = 2mb, one is using the database.
maxsize = 50mb,  You can rename in two ways
filegrowth = 1mb  Using ALTER statement
)  The syntax is
GO ALTER database database-name
EXEC sp_helpdb 'sqlStore' Modify Name = new-name
GO  Using sp_renamedb stored procedure
 The Syntax is
Modify file of an existing database EXECUTE sp_renamedb 'old-name', 'new-name’
 The syntax is
 ALTER database
Try this
ALTER DATABASE sqlStore
MODIFY FILE
MODIFY name = demo
(
GO
NAME = logical_file_name ,
 Refresh databases node in object explorer
FILENAME = 'os_file_name',
 You will see the new name
SIZE = size,
MAXSIZE = max_size, Again try this
FILEGROWTH = growth_increment EXEC sp_renamedb 'demo', 'sqlStore'
) GO
 You must specify NAME and FILENAME  Refresh databases node in object explorer
 You will see the database is changed to sqlStore

You might also like