Skip to content

Instantly share code, notes, and snippets.

@kendy-karakawa
Created February 17, 2023 02:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kendy-karakawa/e0db4559550800bb53170473818ec231 to your computer and use it in GitHub Desktop.
Save kendy-karakawa/e0db4559550800bb53170473818ec231 to your computer and use it in GitHub Desktop.
SQL Integridade e Consistência
CREATE TABLE states (
id SERIAL PRIMARY KEY,
name text NOT NULL
);
CREATE TABLE cities (
id SERIAL PRIMARY KEY,
name text NOT NULL,
"stateId" INTEGER NOT NULL REFERENCES states(id)
);
CREATE TABLE customerAdresses (
id SERIAL PRIMARY KEY,
"customerId" INTEGER UNIQUE NOT NULL REFERENCES customers(id),
street TEXT NOT NULL,
number INTEGER NOT NULL,
complement TEXT NOT NULL,
"postalCode" INTEGER NOT NULL,
"cityId" INTEGER NOT NULL REFERENCES cities(id)
);
CREATE TABLE customers (
id SERIAL PRIMARY KEY,
"fullName" TEXT NOT NULL,
cpf INTEGER NOT NULL,
email TEXT NOT NULL,
password TEXT NOT NULL
);
CREATE TABLE "bankAccount"(
id SERIAL PRIMARY KEY,
"customerId" INTEGER NOT NULL REFERENCES customers(id),
"accountNumber" INTEGER NOT NULL,
agency INTEGER NOT NULL,
"openDate" DATE NOT NULL DEFAULT NOW(),
"closeDate" DATE
)
CREATE TYPE trans AS ENUM ('deposit', 'withdraw');
drop type transactions
CREATE TABLE transactions (
id SERIAL PRIMARY KEY,
"bankAccountId" INTEGER NOT NULL REFERENCES "bankAccount"(id),
amount INTEGER NOT NULL,
type trans NOT NULL,
descriptions TEXT NOT NULL,
cancelled BOOL NOT NULL DEFAULT false
)
CREATE TABLE "creditCard"(
id SERIAL PRIMARY KEY,
"bankAccountId" INTEGER NOT NULL REFERENCES "bankAccount"(id),
name TEXT NOT NULL,
number INTEGER NOT NULL,
"securityCode" INTEGER NOT NULL,
"expirationMonth" INTEGER NOT NULL,
"expirationYear" INTEGER NOT NULL,
password TEXT NOT NULL,
limite INTEGER NOT NULL
)
CREATE TYPE typephone AS ENUM ('landline', 'mobile');
CREATE TABLE "customerPhones"(
id SERIAL PRIMARY KEY,
"customerId" INTEGER NOT NULL REFERENCES customers(id),
number INTEGER NOT NULL,
type typephone NOT NULL
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment