-
Notifications
You must be signed in to change notification settings - Fork 0
/
4) DDL-CREATE-ALTER-DROP.sql
104 lines (89 loc) · 2.1 KB
/
4) DDL-CREATE-ALTER-DROP.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
-- DDL (CREATE - ALTER - DROP)
CREATE DATABASE TestDatabase
GO
-- > Nasıl bir database olacağını kendimiz belirlemek istediğimizde bu şekilde kodlarız.
CREATE DATABASE TestDatabase
ON PRIMARY
(
Name = 'TestDatabase_meta',
FileName = 'C:\Program Files\Microsoft SQL Server\MSSQL15.MSSQLSERVER\MSSQL\DATA\TestDatabase.mdf',
Size = 10 MB,
MaxSize = UNLIMITED,
FileGrowth = 5 MB
)
LOG ON
(
Name = 'TestDatabase_log',
FileName = 'C:\Program Files\Microsoft SQL Server\MSSQL15.MSSQLSERVER\MSSQL\DATA\TestDatabase.ldf',
Size = 2 MB,
MaxSize = 100 MB,
FileGrowth = 50 %
)
GO
ALTER DATABASE TestDatabase
MODIFY FILE
(
Name = 'TTestDatabase_meta',
Size = 20 MB
)
GO
DROP DATABASE TestDatabase
GO
CREATE TABLE Ogrenciler
(
OgrenciID int PRIMARY KEY IDENTITY(1,1),
Adi nvarchar(20) NOT NULL,
Soyadi nvarchar(20) NOT NULL,
Adresi nvarchar(255) NULL,
Telefonu char(14) NULL
)
GO
ALTER TABLE Ogrenciler
ADD GrupID int
GO
DROP TABLE Ogrenciler
GO
CREATE TABLE Gruplar
(
GrupID int IDENTITY(1,1),
Adi nvarchar(50) NOT NULL,
Suresi int NOT NULL CHECK(Suresi > 100),
LabSayisi int CHECK(LabSayisi > 0)
CONSTRAINT PK_GrupID PRIMARY KEY CLUSTERED (GrupID),
CONSTRAINT UQ_Adi UNIQUE (Adi)
)
GO
CREATE TABLE Ogrenciler
(
OgrenciID int IDENTITY(1,1),
Adi nvarchar(20) NOT NULL,
Soyadi nvarchar(20) NOT NULL,
Adresi nvarchar(255) NULL,
CepTelefonu char(14) NULL UNIQUE,
KayitTarihi datetime DEFAULT(GETDATE())
CONSTRAINT PK_OgrenciID PRIMARY KEY CLUSTERED (OgrenciID),
CONSTRAINT CK_KayitTarihi CHECK (KayitTarihi <= GETDATE())
)
GO
CREATE TABLE OgrencilerGruplar
(
GrupID int,
OgrenciID int,
CONSTRAINT PK_GrupID_OgrenciID PRIMARY KEY CLUSTERED (GrupID, OgrenciID),
CONSTRAINT FK_Gruplar_GrupID FOREIGN KEY (GrupID) REFERENCES Gruplar(GrupID),
CONSTRAINT FK_Ogrenciler_OgrenciID FOREIGN KEY (OgrenciID) REFERENCES Ogrenciler(OgrenciID)
)
GO
ALTER TABLE OgrencilerGruplar
DROP CONSTRAINT FK_Ogrenciler_OgrenciID
GO
ALTER TABLE OgrencilerGruplar
ADD CONSTRAINT FK_Ogrenciler_OgrenciID
FOREIGN KEY (OgrenciID)
REFERENCES Ogrenciler(OgrenciID)
ON DELETE CASCADE
ON UPDATE SET NULL
GO
ALTER TABLE Ogrenciler
ALTER COLUMN Adresi nvarchar(255) NOT NULL
GO