-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCreateTables.sql.txt
172 lines (120 loc) · 4.69 KB
/
CreateTables.sql.txt
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
USE master
DECLARE @DBName sysname
DECLARE @DataPath nvarchar(200)
DECLARE @DataFileName nvarchar(200)
DECLARE @BasePath nvarchar(200)
DECLARE @LogPath nvarchar(200)
DECLARE @LogFileName nvarchar(200)
DECLARE @SQLString nvarchar(max)
-- Nome e caminho para a BD
SET @DBName = 'MEI_TRAB'
/*
-- Apagar a Base de dados
DECLARE @SQLDropDB nvarchar(max)
IF ( EXISTS( SELECT * FROM [dbo].[sysdatabases] Where name = @DBName) )
Begin
Set @SQLDropDB = 'USE '+ @DBName + '
DROP DATABASE '+ @DBName
-- print (@SQLDropDB) -- Uncomment to see the DDL Command
Exec(@SQLDropDB)
end
SET @BasePath = 'c:\TBD\' -- Caminho para a base de dados. Alterar se necessário!!!
SET @DataPath = @BasePath + @DBName +'\'
SET @LogPath = @BasePath + @DBName +'\'
-- Criar as directorias (pastas)
SET NOCOUNT ON;
DECLARE @DirectoryExists int;
EXEC master.dbo.xp_fileexist @DataPath, @DirectoryExists OUT;
IF @DirectoryExists = 0
EXEC master.sys.xp_create_subdir @DataPath;
EXEC master.dbo.xp_fileexist @LogPath, @DirectoryExists OUT;
IF @DirectoryExists = 0
EXEC master.sys.xp_create_subdir @LogPath;
-- Criar a Base de dados
-- Nome dos ficheiros de dados e log
SET @DataFileName = @DataPath +'Trab2dat.mdf'
SET @LogFileName = @LogPath +'Trab2log.ldf'
SET @SQLString = 'CREATE DATABASE ' + @DBName +
' ON
( NAME = ''Trab1_dat'',
FILENAME ='''+ @DataFileName + ''',
SIZE = 10,
MAXSIZE = 50,
FILEGROWTH = 5 )
LOG ON
( NAME = ''Trab1_log'',
FILENAME ='''+ @LogFileName + ''',
SIZE = 5MB,
MAXSIZE = 25MB,
FILEGROWTH = 5MB )'
SET NOCOUNT OFF;
-------------------------------------------------------------------------------
--
-- Se não existir a BD então vamos criá-la...
-- (if not exists the database then create them)
--
IF (NOT EXISTS( SELECT * FROM [dbo].[sysdatabases] Where name = @DBName) )
Begin
--print (@SQLString)
exec(@SQLString)
end
*/
-- Criar as tabelas na base de dados recém-criada
-------------------------------------------------------------------------------
-- Criar as tabelas
-- (create the database tables)
-------------------------------------------------------------------------------
-- Tabela de Encomenda
SET @SQLString =
'CREATE TABLE Encomenda ( -- Não está em 3FN!!!
EncID int NOT NULL CHECK (EncID >= 1),
ClienteID int NOT NULL CHECK (ClienteID >= 1),
Nome nvarchar(50) NOT NULL, -- Nome cliente
Morada nvarchar(30) NOT NULL DEFAULT ''Covilhã'', -- Morada cliente
CONSTRAINT PK_Encomenda PRIMARY KEY (EncID) -- Chave primária
)'
SET @SQLString = 'USE '+ @DBName +
' if not exists (select * from dbo.sysobjects where id = object_id(N''[dbo].[Encomenda]'')) begin '+
@SQLString +' end'
EXEC ( @SQLString)
-- Linhas da encomenda / produtos encomendados
SET @SQLString =
'CREATE TABLE EncLinha (
EncId int NOT NULL,
ProdutoID int NOT NULL,
Designacao nvarchar (50) NOT NULL , -- Designação produto
Preco decimal(10,2) NOT NULL DEFAULT 10.0 CHECK (Preco >= 0.0),
Qtd decimal(10,2) NOT NULL DEFAULT 1.0 CHECK (Qtd >= 0.0), -- Qtd produto
CONSTRAINT PK_EncLinha
PRIMARY KEY (EncId, ProdutoID), -- constraint type: primary key
CONSTRAINT FK_EncId FOREIGN KEY (EncId)
REFERENCES Encomenda(EncId)
ON UPDATE CASCADE
ON DELETE NO ACTION
)'
SET @SQLString = 'USE '+ @DBName +
' if not exists (select * from dbo.sysobjects where id = object_id(N''[dbo].[EncLinha]'')) begin '+
@SQLString +' end'
EXEC ( @SQLString)
-- Tabela de Log
-- https://docs.microsoft.com/en-us/sql/t-sql/statements/create-table-transact-sql-identity-property
SET @SQLString =
'CREATE TABLE LogOperations (
NumReg int IDENTITY(1,1), -- Auto increment
EventType char(1), -- I, U, D (Insert, Update, Delete)
-- Log
Objecto varchar(30),
Valor varchar(100),
Referencia varchar(100),
-- Dados sobre o utilizador e posto de trabalho
UserID nvarchar(30) NOT NULL DEFAULT USER_NAME(),
TerminalD nvarchar(30) NOT NULL DEFAULT HOST_ID(),
TerminalName nvarchar(30) NOT NULL DEFAULT HOST_NAME(),
-- Quanto ocorreu a operação
DCriacao datetime NOT NULL DEFAULT GetDate(),
CONSTRAINT PK_LogOperations PRIMARY KEY (NumReg)
)'
SET @SQLString = 'USE '+ @DBName +
' if not exists (select * from dbo.sysobjects where id = object_id(N''[dbo].[LogOperations]'')) begin '+
@SQLString +' end'
EXEC ( @SQLString)