Download SQL Server laboratory no 1 1. Creating database using visual tools

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

DBase wikipedia , lookup

IMDb wikipedia , lookup

Microsoft Access wikipedia , lookup

Oracle Database wikipedia , lookup

SQL wikipedia , lookup

Open Database Connectivity wikipedia , lookup

Entity–attribute–value model wikipedia , lookup

Functional Database Model wikipedia , lookup

Microsoft SQL Server wikipedia , lookup

Concurrency control wikipedia , lookup

Database wikipedia , lookup

Extensible Storage Engine wikipedia , lookup

PL/SQL wikipedia , lookup

Microsoft Jet Database Engine wikipedia , lookup

Ingres (database) wikipedia , lookup

Clusterpoint wikipedia , lookup

ContactPoint wikipedia , lookup

Relational model wikipedia , lookup

Database model wikipedia , lookup

Transcript
SQL Server laboratory – exercise 1
SQL Server laboratory no 1
1. Creating database using visual tools
1.
2.
3.
4.
5.
6.
7.
Start SQL Server Management Studio.
Log as administrator (Windows Authentication)
Examine existing databases.
Open a New database form
Fill all necessary values (db name, file names).
Click Script button.
Examine script which was created.
2. Creating database in SQL
1. Click New Query and open query window.
2. Create database test1.
CREATE DATABASE test1
3. Add a new table to model database
CREATE TABLE prac(nrp int, nazw char(40))
4. Create database test2.
CREATE DATABASE test2
5. What is the difference between databases test1 and test2?
6. Remove databases test1 and test2 and remove table prac from model database.
3. Adding data to database
1. Create database test with files in c:\data directory
CREATE DATABASE test ON (NAME=test, FILENAME='c:\data\test')
2. Execute DBCC CHECKALLOC (‘test’)
3. How much space do system tables take in database?
TASK A
4. Create table prac
create table prac(nrp int, nazw char(40),adres char(500),uwagi char(500))
5. Create table prac2 with the same structure.
6. Add 5000 records to both tables using the script.
declare @j int
set @j = 1
while(@j<5000)
begin
insert into prac(nrp,nazw) values(@j,'abcdefgh')
set @j = @j+1
end
1
SQL Server laboratory – exercise 1
10. How many pages and extends do both tables take?
dbcc checkalloc('test')
11. What is the average page density in both tables?
dbcc showcontig('prac')
dbcc showcontig('prac2')
12. Delete one for two records in prac table.
declare @j int
set @j = 1
while(@j<5000)
begin
delete from prac where nrp=@j
set @j = @j+2
end
13. Delete records with numbers 1500-4000 from prac2 table.
declare @j int
set @j = 1500
while(@j<4000)
begin
delete from prac2 where nrp=@j
set @j = @j+1
end
14. Execute commands.
select count(*) from prac
select count(*) from prac2
15. How many pages does prac table take and how many prac2 table?
dbcc checkalloc('test')
16. What is the average page density in both tables?
dbcc showcontig('prac')
dbcc showcontig('prac2')
17. Execute command
dbcc dbreindex(‘prac’)
18. Are the values checked in 15 and 16 point changed?
19. Execute command
dbcc shrinkdatabase('test')
20. Are the values checked in 15 and 16 point changed?
TASK B
21. Remove tables prac and prac2.
2
SQL Server laboratory – exercise 1
22. Repeat Task A for tables created with primary key.
create table prac(nrp int PRIMARY KEY, nazw char(40),adres char(500),
uwagi char(500))
SUMMARY
21. What are the differences between results of Task A and Task B?
4. Databases with multiple files and filegroups
1. Remove tables prac and prac2.
2. Add a new file ‘test2’ to database test.
alter database test add file (NAME='test2',FILENAME='c:\data\test2')
3. Recreate table prac.
4. Insert 5000 rows.
5. Where were rows inserted ?
dbcc checkalloc('test')
6. Try to remove test2 file
alter database test remove file 'test2'
7. What happened and why?
8. Execute command:
dbcc shrinkfile ('test2',EMPTYFILE)
9. What happened?
10. Insert to prac table next 5000 rows.
11. Where were rows inserted?
12. Repeat command removing test2 file
13. Create filegroup
alter database test add filegroup group1
14. Create file in the filegroup:
alter database test
add file (name=test3,filename='c:\data\test3') to filegroup group1
15. Create table prac2 in filegroup group1 and insert 5000 rows.
create table prac2(nrp int,...) on group1
16. Create table prac3 and insert 5000 rows.
create table prac3(nrp int,...)
17. Where are rows from prac2 and prac3 tables?
18. What should be done to define default filegroup?
19. Set group1 to readonly.
alter database test modify filegroup group1 readonly
3
SQL Server laboratory – exercise 1
20. Try to change data in prac2 table.
21. What happened?
22. Set group1 back to readwrite.
23. Set AUTO_SHRINK option for the database.
24. Remove all records from prac2 table.
25. What happened?
26. Open a new window and execute:
use test
begin transaction
select count(*) from prac
27. In the first window execute:
alter database test set single_user with no_wait
28. What happened?
29. In the first window execute:
alter database test set single_user
30. What happened?
31. In the first window execute:
alter database test set single_user with rollback immediate
32. What happened?
33. Try to execute any command in the second window.
34. What happened?
35. Set multiuser state for database.
5. Creating tables
1. Remove prac and prac2 tables.
2. Create prac table, in the way that nrp field automatically receives unique value starting
from 100.
CREATE TABLE emp(nrp int identity(100,1), name char(40))
3. Create table kids
CREATE TABLE kids(nrk int identity, nrp int, fname char(40))
4.
5.
6.
7.
8.
9.
Modify the script inserting 5000 rows and execute it for table emp.
Insert employee MISIURA with two children JOHN and MARY.
Add two employess with nrp values 10 and 11.
Add to emp table a new column ins_date with default value of insertion date.
Add constraint that doesn’t allow insertion of employees with name starting with ‘A’.
Check if both constraints work.
4