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

Three Days Workshop On SQL Server 2005: Presented by

The document summarizes key SQL concepts including views, creating and dropping indexes, altering tables, truncating tables, backing up and restoring databases, and outputting SQL query results to files. It provides syntax examples for creating views, indexes, altering tables, truncating tables, backing up databases, restoring databases, and outputting query results to text files. The workshop covers SQL Server 2005 and is presented by Aptech Training Solutions over three days focusing on SQL views, indexes, altering tables, and other SQL statements.

Uploaded by

kishornaval
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

Three Days Workshop On SQL Server 2005: Presented by

The document summarizes key SQL concepts including views, creating and dropping indexes, altering tables, truncating tables, backing up and restoring databases, and outputting SQL query results to files. It provides syntax examples for creating views, indexes, altering tables, truncating tables, backing up databases, restoring databases, and outputting query results to text files. The workshop covers SQL Server 2005 and is presented by Aptech Training Solutions over three days focusing on SQL views, indexes, altering tables, and other SQL statements.

Uploaded by

kishornaval
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 31

Three Days Workshop

on
SQL Server 2005
Presented By Aptech Training Solutions
Faculty : Naveen Mishra
SQL Views

• SQL CREATE VIEW Statement


• In SQL, a view is a virtual table based on the
result-set of an SQL statement.
• A view contains rows and columns, just like a
real table. The fields in a view are fields from
one or more real tables in the database.
• You can add SQL functions, WHERE, and JOIN
statements to a view and present the data as
if the data were coming from one single table.
SQL CREATE VIEW Syntax
CREATE VIEW view_name AS
SELECT column_name(s)
FROM table_name
WHERE condition
CREATE VIEW [Current Product List] AS
SELECT ProductID,ProductName
FROM Products
WHERE Discontinued=No
• We can query the view above as follows:
SELECT * FROM [Current Product List]
Another view in the Northwind sample database
selects every product in the "Products" table
with a unit price higher than the average unit
price:
CREATE VIEW [Products Above Average Price] AS
SELECT ProductName,UnitPrice
FROM Products
WHERE UnitPrice>(SELECT AVG(UnitPrice)
FROM Products)
SELECT * FROM [Products Above Average Price]
SQL Dropping a View

• You can delete a view with the DROP VIEW


command.
SQL DROP VIEW Syntax
DROP VIEW view_name
SQL ALTER TABLE Statement

The ALTER TABLE Statement


The ALTER TABLE statement is used to add,
delete, or modify columns in an existing table.
SQL ALTER TABLE Syntax
To add a column in a table, use the following
syntax:
ALTER TABLE table_name
ADD column_name datatype
• To delete a column in a table, use the
following syntax (notice that some database
systems don't allow deleting a column):
ALTER TABLE table_name
DROP COLUMN column_name
• To change the data type of a column in a table,
use the following syntax:
ALTER TABLE table_name
ALTER COLUMN column_name datatype
Now we want to add a column named
"DateOfBirth" in the "Persons" table.
We use the following SQL statement:
ALTER TABLE Persons
ADD DateOfBirth datetime
SQL CREATE INDEX Statement

The CREATE INDEX statement is used to create


indexes in tables.
• Indexes allow the database application to find
data fast; without reading the whole table.
• An index can be created in a table to find data
more quickly and efficiently.
• The users cannot see the indexes, they are just
used to speed up searches/queries.
SQL CREATE INDEX Syntax

• Creates an index on a table. Duplicate values


are allowed:
CREATE INDEX index_name
ON table_name (column_name)
SQL CREATE UNIQUE INDEX Syntax
• Creates a unique index on a table. Duplicate
values are not allowed:
CREATING UNIQUE INDEX
CREATE UNIQUE INDEX index_name
ON table_name (column_name)
• If you want to create an index on a
combination of columns, you can list the
column names within the parentheses,
separated by commas:
CREATE INDEX PIndex
ON Persons (LastName, FirstName)
Drop Index /Table
The DROP INDEX Statement
The DROP INDEX statement is used to delete an
index in a table.
DROP INDEX table_name.index_name
• The DROP TABLE Statement
• The DROP TABLE statement is used to delete a
table.
DROP TABLE table_name
DROP DATABASE
• The DROP DATABASE Statement
• The DROP DATABASE statement is used to
delete a database.
DROP DATABASE database_name
TRUNCATE TABLE
• The TRUNCATE TABLE Statement
• What if we only want to delete the data inside
the table, and not the table itself?
• Then, use the TRUNCATE TABLE statement:
TRUNCATE TABLE table_name
Creating Login
• To create a SQL Server
login that uses SQL
Server Authentication
(SQL Server
Management Studio)
• In SQL Server Management Studio, open Object Explorer and
expand the folder of the server instance in which to create the
new login.
• Right-click the Security folder, point to New, and then
click Login.
• On the General page, enter a name for the new login in
the Login name box.
• Select SQL Server Authentication. Windows Authentication is
the more secure option.
• Enter a password for the login.
• Select the password policy options that should be applied to
the new login. In general, enforcing password policy is the
more secure option.
• Click OK.
Generating SQL Scripts
•  There are steps how to GENERATE SQL SCRIPT in
SQL server 2000 Right click on the temporary data
and pointing all tasks
• Select Generate SQL Script, in the dialogue box
click show all.
• under script all objects, check the box of all tables
• Click options, under table scripting options, check
the box of Script indexes, and Script primary keys,
FOREGIN keys, defaults, and check constraints.
Generating SQL Scripts
Back up a database
Back up a database
• After connecting to the appropriate instance of the Microsoft SQL Server
Database Engine, in Object Explorer, click the server name to expand the
server tree.
• Expand Databases, and depending on the database, either select a user
database or expand System Databasesand select a system database.
• Right-click the database, point to Tasks, and then click Back Up. The Back Up
Database dialog box appears.
• In the Database list box, verify the database name. You can optionally select
a different database from the list.
• You can perform a database backup for any recovery model
(FULL, BULK_LOGGED, or SIMPLE).
• In the Backup type list box, select Full.
• Note that after creating a full database backup, you can create a differential
database backup; for more information
Back up a database
Restore Database
Right-click the database, point to Tasks, and
then click Restore.
• Click Database, which opens the Restore
Database dialog box.
• On the General page, the name of the
restoring database appears in the To
database list box. To create a new database,
enter its name in the list box.
Restore Database
Restore Database
Output an SQL Query to a text file

EXEC master.dbo.sp_configure 'show advanced


options', 1
RECONFIGURE
EXEC master.dbo.sp_configure 'xp_cmdshell',
1
RECONFIGURE
EXEC master..xp_cmdshell'bcp "SELECT TOP 5
CUSTOMERID FROM
Northwind.dbo.Customers" queryout
"C:\Users\Naval\Desktop\SQL
Assignments\text.txt" -c -T -x'
 It will generate the output in destination
folder as text.txt
Output an SQL Query to any file
• Ctrl + Shift + f  Output to File
• Ctrl +d -> Output to SQL Grid
Formatted Output

You might also like