-
Notifications
You must be signed in to change notification settings - Fork 0
/
01, Creating Database.sql
124 lines (109 loc) · 2.74 KB
/
01, Creating Database.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
-------------------------------------
-- 1: Creating Database
-------------------------------------
-- Simplest way to create a database in SQL Server
CREATE DATABASE TestDB;
GO
-- Creating a database with 1 datafile and 1 logfile
CREATE DATABASE TestDB
ON PRIMARY
(
NAME = TestDB_Data,
FILENAME = N'c:\db\TestDB_Data.mdf',
SIZE = 10MB,
MAXSIZE = 10GB,
FILEGROWTH = 10%
)
LOG ON
(
NAME = TestDB_Log,
FILENAME = N'c:\db\TestDB_Log.ldf',
SIZE = 10MB,
MAXSIZE = 10GB,
FILEGROWTH = 10MB
);
GO
-- Creating a database with multiple files and filegroups
CREATE DATABASE TestDB
ON PRIMARY
(
NAME = TestDB_Data,
FILENAME = N'c:\db\TestDB_Data.mdf',
SIZE = 10MB,
MAXSIZE = 10GB,
FILEGROWTH = 10%
),
(
NAME = TestDB_Data2,
FILENAME = N'c:\db\TestDB_Data2.ndf',
SIZE = 10MB,
MAXSIZE = 10GB,
FILEGROWTH = 10%
),
FILEGROUP FG2
(
NAME = TestDB_Data3,
FILENAME = N'c:\db\TestDB_Data3.ndf',
SIZE = 10MB,
MAXSIZE = 10GB,
FILEGROWTH = 10%
),
(
NAME = TestDB_Data4,
FILENAME = N'c:\db\TestDB_Data4.ndf',
SIZE = 10MB,
MAXSIZE = 10GB,
FILEGROWTH = 10%
)
LOG ON
(
NAME = TestDB_Log,
FILENAME = N'c:\db\TestDB_Log.ldf',
SIZE = 10MB,
MAXSIZE = 10GB,
FILEGROWTH = 10MB
),
(
NAME = TestDB_Log2,
FILENAME = N'c:\db\TestDB_Log2.ldf',
SIZE = 10MB,
MAXSIZE = 10GB,
FILEGROWTH = 10%
);
GO
-- Altering database recovery model
ALTER DATABASE TestDB SET RECOVERY SIMPLE; -- {Full | BULK_LOGGED}
GO
-- Add new filegroup to existing database
ALTER DATABASE TestDB ADD FILEGROUP FG3;
GO
-- Add new file to database
ALTER DATABASE TestDB ADD FILE (
NAME = TestDB_Data5,
FILENAME = N'c:\db\TestDB_Data5.ndf',
SIZE = 10MB,
MAXSIZE = 10GB,
FILEGROWTH = 10%
) TO FILEGROUP FG3;
GO
-- Add new log file to existing database
ALTER DATABASE TestDB ADD LOG FILE (
NAME = TestDB_Log3,
FILENAME = N'c:\db\TestDB_Log3.ldf',
SIZE = 10MB,
MAXSIZE = 10GB,
FILEGROWTH = 10%
);
GO
-- Change default filegroup
ALTER DATABASE TestDB MODIFY FILEGROUP FG3 DEFAULT;
GO
-- Modify a filegroup to readonly
ALTER DATABASE TestDB MODIFY FILEGROUP FG3 READ_ONLY;
GO
-- Change database collation
ALTER DATABASE TestDB COLLATE Persian_100_CI_AS;
GO
-- Add extended property
EXEC sp_addextendedproperty @name=N'ver', @value=N'1.0.0';
GO