![]() "amount" IS 'can be negative or positive' COMMENT ON COLUMN "transfers". Let’s say we want each account to have a unique ID, so I’m gonna use an auto-increment id field for it.ĬREATE TABLE "accounts" ( "id" bigserial PRIMARY KEY, "owner" varchar NOT NULL, "balance" bigint NOT NULL, "currency" varchar NOT NULL, "created_at" timestamptz NOT NULL DEFAULT ( now ()) ) CREATE TABLE "entries" ( "id" bigserial PRIMARY KEY, "account_id" bigint NOT NULL, "amount" bigint NOT NULL, "created_at" timestamptz NOT NULL DEFAULT ( now ()) ) CREATE TABLE "transfers" ( "id" bigserial PRIMARY KEY, "from_account_id" bigint NOT NULL, "to_account_id" bigint NOT NULL, "amount" bigint NOT NULL, "created_at" timestamptz NOT NULL DEFAULT ( now ()) ) ALTER TABLE "entries" ADD FOREIGN KEY ( "account_id" ) REFERENCES "accounts" ( "id" ) ALTER TABLE "transfers" ADD FOREIGN KEY ( "from_account_id" ) REFERENCES "accounts" ( "id" ) ALTER TABLE "transfers" ADD FOREIGN KEY ( "to_account_id" ) REFERENCES "accounts" ( "id" ) CREATE INDEX ON "accounts" ( "owner" ) CREATE INDEX ON "entries" ( "account_id" ) CREATE INDEX ON "transfers" ( "from_account_id" ) CREATE INDEX ON "transfers" ( "to_account_id" ) CREATE INDEX ON "transfers" ( "from_account_id", "to_account_id" ) COMMENT ON COLUMN "entries". We use the Table keyword to declare a table, and use the as keyword to set a short alias name for it. I’m gonna change the name of this diagram to "Simple bank".įirst we will have an accounts table. We can use the Export tool at the top to save this diagram as a PDF or PNG file, or generate SQL codes for Postgres, MySQL or SQL server.Īlright, now let’s modify this schema for our database. Then its corresponding diagram will show up on the right. On the left, we define the table structure with some simple syntax. ![]() OK, let’s start by going to dbdiagram.io and click Go to app. ![]()
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |