Welcome to CodeYourCraft's SQL Tutorial! In this lesson, we'll be building a simplified banking system to learn SQL from scratch. By the end, you'll have a solid understanding of SQL and be ready to tackle real-world database projects. 💡 Pro Tip: Bookmark this page for future reference!
SQL (Structured Query Language) is a standard language used for managing and manipulating databases. It allows you to create, query, update, and delete data in a database.
Before we dive into the banking system, let's set up a SQLite database for our project.
-- Create a new SQLite database named 'banking_system.db'
.open banking_system.db
-- Verify the database version
.verNow that our database is ready, let's create the tables we'll need for our banking system.
-- Create customers table with columns: id (primary key), name, email, and phone
CREATE TABLE customers (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL,
email TEXT UNIQUE NOT NULL,
phone TEXT NOT NULL
);-- Create accounts table with columns: id (primary key), customer_id (foreign key), account_number, and balance
CREATE TABLE accounts (
id INTEGER PRIMARY KEY AUTOINCREMENT,
customer_id INTEGER NOT NULL,
account_number TEXT NOT NULL UNIQUE,
balance REAL NOT NULL
);Now that our tables are created, let's insert some data.
-- Insert a new customer (John Doe, john.doe@example.com, 1234567890)
INSERT INTO customers (name, email, phone) VALUES ('John Doe', 'john.doe@example.com', '1234567890');-- Insert a new account for John Doe (1234567890, 1234567890001, 5000.00)
INSERT INTO accounts (customer_id, account_number, balance) VALUES (LAST_INSERT_ROWID, '1234567890001', 5000.00);-- Fetch all customers
SELECT * FROM customers;
-- Fetch customer with id 1
SELECT * FROM customers WHERE id = 1;-- Fetch all accounts
SELECT * FROM accounts;
-- Fetch account for customer with id 1
SELECT * FROM accounts WHERE customer_id = (SELECT id FROM customers WHERE name = 'John Doe');-- Update John Doe's email to johndoe@example.com
UPDATE customers SET email = 'johndoe@example.com' WHERE name = 'John Doe';-- Deposit 1000.00 into John Doe's account
UPDATE accounts SET balance = balance + 1000.00 WHERE customer_id = (SELECT id FROM customers WHERE name = 'John Doe');-- Delete John Doe (be careful!)
DELETE FROM customers WHERE name = 'John Doe';-- Delete John Doe's account
DELETE FROM accounts WHERE customer_id = (SELECT id FROM customers WHERE name = 'John Doe');What does SQL stand for?
What is the primary key for the customers table?
How do you insert a new customer with the given details: Jane Smith, jane.smith@example.com, 0987654321?
Congratulations on completing this SQL tutorial! You've learned how to create a simple banking system using SQL, including creating tables, inserting data, querying data, updating data, and deleting data. With this newfound knowledge, you're ready to tackle more complex database projects.
Remember, practice makes perfect, so don't be afraid to experiment with SQL queries on your own. Happy coding! 💡 Pro Tip: Use the SQLite command line to practice your SQL skills!
Stay tuned for more SQL tutorials at CodeYourCraft, where we help you master programming through practical, beginner-friendly lessons. Until next time! 🚀