PostgreSQL
INSERT
The INSERT statement in PostgreSQL is used to add new rows of data to a table. When working with tables related to banking, such as customers, accounts, and transactions, the INSERT command helps populate the database with initial or transactional data. To use this command, you specify the table name, the columns to insert data into, and the corresponding values.
Example Scenario:
You have a banking system with tables for customers, accounts, and transactions, and you want to add new data to these tables using the INSERT statement.
Steps to Insert Data:
- Inserting a Customer into the
customersTable- Before inserting any data into a table, ensure that the table exists.
- Use the
INSERT INTOcommand to add data to thecustomerstable.
INSERT INTO customers (customer_id, name, email, phone) VALUES (1, 'John Doe', 'john.doe@example.com', '123-456-7890');- Explanation:
- The
INSERT INTOspecifies the tablecustomerswhere data is being inserted. (customer_id, name, email, phone)defines the columns where the data will go.VALUESspecifies the actual values to be inserted.
- The
- Inserting an Account into the
accountsTable- Insert new data into the
accountstable that tracks customer accounts.
INSERT INTO accounts (account_id, customer_id, account_type, balance) VALUES (101, 1, 'savings', 5000.00);- Explanation:
account_idis unique for each account.customer_idassociates the account with the customer.account_typecan be 'savings' or 'checking'.balanceholds the initial balance of the account.
- Insert new data into the
- Inserting a Transaction into the
transactionsTable- After setting up customers and accounts, transactions can be recorded in the
transactionstable.
INSERT INTO transactions (transaction_id, account_id, amount, transaction_date) VALUES (1001, 101, 1500.00, '2024-09-17');- Explanation:
transaction_idis the unique ID for the transaction.account_idlinks the transaction to the appropriate account.amountis the transaction amount.transaction_daterecords when the transaction occurred.
- After setting up customers and accounts, transactions can be recorded in the
- Inserting Multiple Records at Once
- PostgreSQL allows inserting multiple rows in one statement.
INSERT INTO customers (customer_id, name, email, phone) VALUES (2, 'Jane Smith', 'jane.smith@example.com', '987-654-3210'), (3, 'Emily Johnson', 'emily.johnson@example.com', '555-123-4567');- Explanation:
- This inserts two rows into the
customerstable with a singleINSERTstatement.
- This inserts two rows into the
- Inserting Data without Specifying All Columns
- You can insert data without specifying all columns, as long as default values or
NULLare acceptable for the missing columns.
INSERT INTO accounts (account_id, customer_id, account_type) VALUES (102, 2, 'checking');- Explanation:
- The
balancecolumn will take a default value (orNULLif no default is set) because it's not specified in theINSERT.
- The
- You can insert data without specifying all columns, as long as default values or
This format provides an easy-to-understand, step-by-step guide for beginners working with PostgreSQL's INSERT command in the context of banking applications.
To gain complete access, login with gmail or outlook, no need of signup. click here


Comments Not Found