-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsqlteste.txt
More file actions
52 lines (40 loc) · 1.39 KB
/
Copy pathsqlteste.txt
File metadata and controls
52 lines (40 loc) · 1.39 KB
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
CREATE DATABASE db_testeSQL;
GO
USE db_testeSQL;
CREATE TABLE Customer(
IdCustomer integer PRIMARY KEY NOT NULL IDENTITY(1,1),
NmCustomer varchar(255) NOT NULL,
CpfCnpj numeric NOT NULL
);
CREATE TABLE AddressType(
CdAddressType char(1) PRIMARY KEY NOT NULL,
AddressType varchar(50) NOT NULL
);
CREATE TABLE CustomerAddress(
IdCustomer integer,
CdAddressType char(1),
Street varchar(255) NOT NULL,
Lot integer NOT NULL,
Reference varchar(255) NULL,
ZipCode varchar(50) NOT NULL
CONSTRAINT FK_IdCustomer FOREIGN KEY (IdCustomer) REFERENCES Customer(IdCustomer),
CONSTRAINT FK_CdAddressType FOREIGN KEY (CdAddressType) REFERENCES AddressType(CdAddressType),
CONSTRAINT PK_CustomerAddress PRIMARY KEY (IdCustomer, CdAddressType)
);
INSERT INTO AddressType(CdAddressType, AddressType)
VALUES
('R', 'Residêncial'),
('C', 'Comercial'),
('O', 'Outros');
SELECT * FROM AddressType;
INSERT INTO Customer(NmCustomer, CpfCnpj)
VALUES
('Joãozinho Silva', 88877766655);
SELECT SCOPE_IDENTITY() AS IdLastCustomer;
INSERT INTO CustomerAddress(IdCustomer, CdAddressType, Street, Lot, Reference, ZipCode)
VALUES
(1, 'R','Rua das Flores',1,null,'01234-567'),
(1, 'C','Rua das Pedras',100,'Conjuntos 200','01234-567');
SELECT * FROM CustomerAddress;
DELETE FROM CustomerAddress WHERE IdCustomer = (SELECT IdCustomer FROM Customer WHERE CpfCnpj = 88877766655);
DELETE FROM Customer WHERE CpfCnpj = 88877766655;