├── Bank_Account_Data_Modelling_PostgreSQL.sql ├── Bank_Account_Data_Modelling_mysql.sql ├── README.md └── screenshots ├── 1.png ├── 2.png ├── 3.png └── 4.png /Bank_Account_Data_Modelling_PostgreSQL.sql: -------------------------------------------------------------------------------- 1 | CREATE TABLE "customers" ( 2 | "customer_id" SERIAL PRIMARY KEY, 3 | "first_name" VARCHAR, 4 | "last_name" VARCHAR, 5 | "city" VARCHAR, 6 | "mobile_no" VARCHAR, 7 | "pancard_no" VARCHAR, 8 | "dob" VARCHAR, 9 | "created_at" timestampz DEFAULT 'now()', 10 | "deleted_at" timestampz 11 | ); 12 | 13 | CREATE TABLE "branchs" ( 14 | "branch_id" SERIAL PRIMARY KEY, 15 | "branch_name" VARCHAR, 16 | "branch_location" VARCHAR, 17 | "created_at" timestampz DEFAULT 'now()', 18 | "deleted_at" timestampz 19 | ); 20 | 21 | CREATE TABLE "accounts" ( 22 | "account_id" bigserial PRIMARY KEY, 23 | "customer_id" int, 24 | "balance" bigint, 25 | "account_status" VARCHAR, 26 | "account_type" VARCHAR, 27 | "currency" VARCHAR, 28 | "created_at" timestampz DEFAULT 'now()', 29 | "deleted_at" timestampz 30 | ); 31 | 32 | CREATE TABLE "transactions" ( 33 | "transaction_id" bigserial PRIMARY KEY, 34 | "transaction_type" VARCHAR, 35 | "from_account_id" bigint, 36 | "to_account_id" bigint, 37 | "date_issued" date, 38 | "amount" bigint, 39 | "transaction_medium" VARCHAR, 40 | "created_at" timestampz DEFAULT 'now()', 41 | "deleted_at" timestampz 42 | ); 43 | 44 | CREATE TABLE "loans" ( 45 | "loan_id" bigserial PRIMARY KEY, 46 | "customer_id" int, 47 | "branch_id" int, 48 | "loan_amount" bigint, 49 | "date_issued" date, 50 | "created_at" timestampz DEFAULT 'now()', 51 | "deleted_at" timestampz 52 | ); 53 | 54 | ALTER TABLE "accounts" ADD FOREIGN KEY ("customer_id") REFERENCES "customers" ("customer_id"); 55 | 56 | ALTER TABLE "transactions" ADD FOREIGN KEY ("from_account_id") REFERENCES "accounts" ("account_id"); 57 | 58 | ALTER TABLE "transactions" ADD FOREIGN KEY ("to_account_id") REFERENCES "accounts" ("account_id"); 59 | 60 | ALTER TABLE "loans" ADD FOREIGN KEY ("customer_id") REFERENCES "customers" ("customer_id"); 61 | 62 | ALTER TABLE "loans" ADD FOREIGN KEY ("branch_id") REFERENCES "branchs" ("branch_id"); 63 | 64 | CREATE INDEX ON "customers" ("customer_id"); 65 | 66 | CREATE INDEX ON "customers" ("first_name"); 67 | 68 | CREATE INDEX ON "customers" ("last_name"); 69 | 70 | CREATE INDEX ON "branchs" ("branch_id"); 71 | 72 | CREATE INDEX ON "accounts" ("account_id"); 73 | 74 | CREATE INDEX ON "transactions" ("from_account_id"); 75 | 76 | CREATE INDEX ON "transactions" ("to_account_id"); 77 | 78 | CREATE INDEX ON "transactions" ("from_account_id", "to_account_id"); 79 | -------------------------------------------------------------------------------- /Bank_Account_Data_Modelling_mysql.sql: -------------------------------------------------------------------------------- 1 | CREATE TABLE `customers` ( 2 | `customer_id` int PRIMARY KEY AUTO_INCREMENT, 3 | `first_name` VARCHAR, 4 | `last_name` VARCHAR, 5 | `city` VARCHAR, 6 | `mobile_no` VARCHAR, 7 | `pancard_no` VARCHAR, 8 | `dob` VARCHAR, 9 | `created_at` timestampz DEFAULT "now()", 10 | `deleted_at` timestampz 11 | ); 12 | 13 | CREATE TABLE `branchs` ( 14 | `branch_id` int PRIMARY KEY AUTO_INCREMENT, 15 | `branch_name` VARCHAR, 16 | `branch_location` VARCHAR, 17 | `created_at` timestampz DEFAULT "now()", 18 | `deleted_at` timestampz 19 | ); 20 | 21 | CREATE TABLE `accounts` ( 22 | `account_id` bigserial PRIMARY KEY, 23 | `customer_id` int, 24 | `balance` bigint, 25 | `account_status` VARCHAR, 26 | `account_type` VARCHAR, 27 | `currency` VARCHAR, 28 | `created_at` timestampz DEFAULT "now()", 29 | `deleted_at` timestampz 30 | ); 31 | 32 | CREATE TABLE `transactions` ( 33 | `transaction_id` bigserial PRIMARY KEY, 34 | `transaction_type` VARCHAR, 35 | `from_account_id` bigint, 36 | `to_account_id` bigint, 37 | `date_issued` date, 38 | `amount` bigint, 39 | `transaction_medium` VARCHAR, 40 | `created_at` timestampz DEFAULT "now()", 41 | `deleted_at` timestampz 42 | ); 43 | 44 | CREATE TABLE `loans` ( 45 | `loan_id` bigserial PRIMARY KEY, 46 | `customer_id` int, 47 | `branch_id` int, 48 | `loan_amount` bigint, 49 | `date_issued` date, 50 | `created_at` timestampz DEFAULT "now()", 51 | `deleted_at` timestampz 52 | ); 53 | 54 | ALTER TABLE `accounts` ADD FOREIGN KEY (`customer_id`) REFERENCES `customers` (`customer_id`); 55 | 56 | ALTER TABLE `transactions` ADD FOREIGN KEY (`from_account_id`) REFERENCES `accounts` (`account_id`); 57 | 58 | ALTER TABLE `transactions` ADD FOREIGN KEY (`to_account_id`) REFERENCES `accounts` (`account_id`); 59 | 60 | ALTER TABLE `loans` ADD FOREIGN KEY (`customer_id`) REFERENCES `customers` (`customer_id`); 61 | 62 | ALTER TABLE `loans` ADD FOREIGN KEY (`branch_id`) REFERENCES `branchs` (`branch_id`); 63 | 64 | CREATE INDEX `customers_index_0` ON `customers` (`customer_id`); 65 | 66 | CREATE INDEX `customers_index_1` ON `customers` (`first_name`); 67 | 68 | CREATE INDEX `customers_index_2` ON `customers` (`last_name`); 69 | 70 | CREATE INDEX `branchs_index_3` ON `branchs` (`branch_id`); 71 | 72 | CREATE INDEX `accounts_index_4` ON `accounts` (`account_id`); 73 | 74 | CREATE INDEX `transactions_index_5` ON `transactions` (`from_account_id`); 75 | 76 | CREATE INDEX `transactions_index_6` ON `transactions` (`to_account_id`); 77 | 78 | CREATE INDEX `transactions_index_7` ON `transactions` (`from_account_id`, `to_account_id`); 79 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Bank-Database-Design 2 | # 📩 Latest Blog Posts : https://dev.to/jinxankit/data-modelling-database-design-4hhc 3 | Database design is structuring data and organised relationships in a database. It goes beyond how the database further works. 4 | I was learning about a cool stuff [dbdigram.io](https://dbdiagram.io) for designing a database so hitting a random try for Bank Database design. 5 | 6 | **Requirements** 7 | 1. Create and manage accounts { owner, branch, balance details...} 8 | 2. Record all transactions history from accounts -> to accounts etc. 9 | 3. Perform money transfer between 2 accounts consistently within a transactions 10 | 4. Maintaining a Loan Records 11 | 12 | **Database Design** 13 | 14 | The basics of database design is identifying what data needs to get stored, grouping such data into tables or collections and adding relationships between related data points. The whole database design process involves 15 | 16 | Using *Entity-Relationship(ER)* diagrams to visualize the objects that get stored and their relationships. 17 | 18 | Lets see how this can be designed on diagrams using [dbdigram.io](https://dbdiagram.io) and then converted to SQL and PostgreSQL. 19 | 20 | Exciting right? 21 | 22 | 23 | **How to use dbdigram** 24 | The tool is simple: you write code, it renders the ER diagram 👌. You can then export to PDF, PNG or generate SQL code with it ⭐️. 25 | 26 | It uses DBML (database markup language) to define and document database schemas. 27 | 28 | 29 | 30 | ![Pic_1](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/17gpz0f1eu92te908dab.png) 31 | 32 | *customer_id is primary key(pk keyword) and auto incremented* 33 | *created_at enclose timezone information as well* 34 | *deleted_at for maintaining the deletion record* 35 | 36 | ![Pic_2](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/a0z80gmdhev8ccsnt3av.png) 37 | 38 | *bigserial in PostgreSQL is basically a big autoincrementing integers (8-byte/64-bit)* 39 | *customer_id int [ref: > C.customer_id], customer_id is foreign key user here* 40 | 41 | ![Pic_3](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0ssvjfm3l68w41vq6ir4.png) 42 | 43 | *Hence the Final ER - Diagram Result:* 44 | ![Pic_4](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nrmnjjkfmauguzmq8525.png) 45 | 46 | **Let's write/generate mysql Code for this schema now** 47 | 48 | CREATE DATABASE bank; 49 | USE bank; 50 | 51 | CREATE TABLE `customers` ( 52 | `customer_id` int PRIMARY KEY AUTO_INCREMENT, 53 | `first_name` VARCHAR, 54 | `last_name` VARCHAR, 55 | `city` VARCHAR, 56 | `mobile_no` VARCHAR, 57 | `pancard_no` VARCHAR, 58 | `dob` VARCHAR, 59 | `created_at` timestampz DEFAULT "now()", 60 | `deleted_at` timestampz 61 | ); 62 | 63 | CREATE TABLE `branchs` ( 64 | `branch_id` int PRIMARY KEY AUTO_INCREMENT, 65 | `branch_name` VARCHAR, 66 | `branch_location` VARCHAR, 67 | `created_at` timestampz DEFAULT "now()", 68 | `deleted_at` timestampz 69 | ); 70 | 71 | CREATE TABLE `accounts` ( 72 | `account_id` bigserial PRIMARY KEY, 73 | `customer_id` int, 74 | `balance` bigint, 75 | `account_status` VARCHAR, 76 | `account_type` VARCHAR, 77 | `currency` VARCHAR, 78 | `created_at` timestampz DEFAULT "now()", 79 | `deleted_at` timestampz 80 | ); 81 | 82 | CREATE TABLE `transactions` ( 83 | `transaction_id` bigserial PRIMARY KEY, 84 | `transaction_type` VARCHAR, 85 | `from_account_id` bigint, 86 | `to_account_id` bigint, 87 | `date_issued` date, 88 | `amount` bigint, 89 | `transaction_medium` VARCHAR, 90 | `created_at` timestampz DEFAULT "now()", 91 | `deleted_at` timestampz 92 | ); 93 | 94 | CREATE TABLE `loans` ( 95 | `loan_id` bigserial PRIMARY KEY, 96 | `customer_id` int, 97 | `branch_id` int, 98 | `loan_amount` bigint, 99 | `date_issued` date, 100 | `created_at` timestampz DEFAULT "now()", 101 | `deleted_at` timestampz 102 | ); 103 | 104 | ALTER TABLE `accounts` ADD FOREIGN KEY (`customer_id`) REFERENCES `customers` (`customer_id`); 105 | 106 | ALTER TABLE `transactions` ADD FOREIGN KEY (`from_account_id`) REFERENCES `accounts` (`account_id`); 107 | 108 | ALTER TABLE `transactions` ADD FOREIGN KEY (`to_account_id`) REFERENCES `accounts` (`account_id`); 109 | 110 | ALTER TABLE `loans` ADD FOREIGN KEY (`customer_id`) REFERENCES `customers` (`customer_id`); 111 | 112 | ALTER TABLE `loans` ADD FOREIGN KEY (`branch_id`) REFERENCES `branchs` (`branch_id`); 113 | 114 | 115 | 116 | 117 | **PostgreSQL Code** 118 | 119 | 120 | 121 | CREATE TABLE `"customers"` ( 122 | `"customer_id"` SERIAL PRIMARY KEY, 123 | `"first_name"` VARCHAR, 124 | `"last_name"` VARCHAR, 125 | `"city"` VARCHAR, 126 | `"mobile_no"` VARCHAR, 127 | `"pancard_no"` VARCHAR, 128 | `"dob"` VARCHAR, 129 | `"created_at"` timestampz DEFAULT 'now()', 130 | `"deleted_at"` timestampz 131 | ); 132 | 133 | CREATE TABLE `"branchs"` ( 134 | `"branch_id"` SERIAL PRIMARY KEY, 135 | `"branch_name"` VARCHAR, 136 | `"branch_location"` VARCHAR, 137 | `"created_at"` timestampz DEFAULT 'now()', 138 | `"deleted_at"` timestampz 139 | ); 140 | 141 | CREATE TABLE `"accounts"` ( 142 | `"account_id"` bigserial PRIMARY KEY, 143 | `"customer_id"` int, 144 | `"balance"` bigint, 145 | `"account_status"` VARCHAR, 146 | `"account_type"` VARCHAR, 147 | `"currency"` VARCHAR, 148 | `"created_at"` timestampz DEFAULT 'now()', 149 | `"deleted_at"` timestampz 150 | ); 151 | 152 | CREATE TABLE `"transactions"` ( 153 | `"transaction_id"` bigserial PRIMARY KEY, 154 | `"transaction_type"` VARCHAR, 155 | `"from_account_id"` bigint, 156 | `"to_account_id"` bigint, 157 | `"date_issued"` date, 158 | `"amount"` bigint, 159 | `"transaction_medium"` VARCHAR, 160 | `"created_at"` timestampz DEFAULT 'now()', 161 | `"deleted_at"` timestampz 162 | ); 163 | 164 | CREATE TABLE `"loans"` ( 165 | `"loan_id"` bigserial PRIMARY KEY, 166 | `"customer_id"` int, 167 | `"branch_id"` int, 168 | `"loan_amount"` bigint, 169 | `"date_issued"` date, 170 | `"created_at"` timestampz DEFAULT 'now()', 171 | `"deleted_at"` timestampz 172 | ); 173 | 174 | ALTER TABLE `"accounts"` ADD FOREIGN KEY (`"customer_id"`) REFERENCES `"customers"` (`"customer_id"`); 175 | 176 | ALTER TABLE `"transactions"` ADD FOREIGN KEY (`"from_account_id"`) REFERENCES `"accounts"` (`"account_id"`); 177 | 178 | ALTER TABLE `"transactions"` ADD FOREIGN KEY (`"to_account_id"`) REFERENCES `"accounts"` (`"account_id"`); 179 | 180 | ALTER TABLE `"loans"` ADD FOREIGN KEY (`"customer_id"`) REFERENCES `"customers"` (`"customer_id"`); 181 | 182 | ALTER TABLE `"loans"` ADD FOREIGN KEY (`"branch_id"`) REFERENCES `"branchs"` (`"branch_id"`); 183 | 184 | 185 | 186 | **References:** 187 | https://github.com/holistics/dbml 188 | 189 | *Get the code here* 190 | https://github.com/jinxankit/Bank-DataBase-Design 191 | 192 | 193 | 194 | **THANK YOU!!!!!** 195 | 196 | 197 | 198 | -------------------------------------------------------------------------------- /screenshots/1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ankittkp/Bank-Database-Design/603585dae62171c0a8b5b39c361850dcd6af162c/screenshots/1.png -------------------------------------------------------------------------------- /screenshots/2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ankittkp/Bank-Database-Design/603585dae62171c0a8b5b39c361850dcd6af162c/screenshots/2.png -------------------------------------------------------------------------------- /screenshots/3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ankittkp/Bank-Database-Design/603585dae62171c0a8b5b39c361850dcd6af162c/screenshots/3.png -------------------------------------------------------------------------------- /screenshots/4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ankittkp/Bank-Database-Design/603585dae62171c0a8b5b39c361850dcd6af162c/screenshots/4.png --------------------------------------------------------------------------------