Build your first database! By the end of this lesson, you'll create a real contact manager database that can store and organize information about your friends, family, or team members.
A Contact Manager Database that can:
This is exactly how apps like your phone's contacts, WhatsApp, or LinkedIn manage millions of users!
Let's start by creating a simple database right away! Think of a database like a super-powered spreadsheet that never forgets.
-- This is your first database table!
-- It's like creating a new spreadsheet
CREATE TABLE contacts (
id INTEGER PRIMARY KEY,
name TEXT NOT NULL,
email TEXT,
phone TEXT,
favorite BOOLEAN DEFAULT FALSE
);
-- Add your first contact
INSERT INTO contacts (name, email, phone, favorite)
VALUES ('Your Name', 'you@email.com', '123-456-7890', TRUE);
Try it now! Copy this code and we'll run it together.
When you save a contact on your phone, it's stored in a database that:
-- Create a more advanced contacts table
CREATE TABLE contacts (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL,
email TEXT UNIQUE,
phone TEXT,
company TEXT,
favorite BOOLEAN DEFAULT FALSE,
created_at DATETIME DEFAULT CURRENT_TIMESTAMP
);
-- Add multiple contacts at once
INSERT INTO contacts (name, email, phone, company, favorite) VALUES
('Elon Musk', 'elon@tesla.com', '555-0001', 'Tesla', TRUE),
('Tim Cook', 'tim@apple.com', '555-0002', 'Apple', FALSE),
('Sundar Pichai', 'sundar@google.com', '555-0003', 'Google', FALSE),
('Your Best Friend', 'friend@email.com', '555-0004', NULL, TRUE);
-- Find all favorite contacts
SELECT * FROM contacts WHERE favorite = TRUE;
-- Find contacts from specific companies
SELECT name, email FROM contacts WHERE company = 'Apple';
-- Search by name (partial match)
SELECT * FROM contacts WHERE name LIKE '%Elon%';
-- Update a phone number
UPDATE contacts
SET phone = '555-9999'
WHERE name = 'Elon Musk';
-- Mark someone as favorite
UPDATE contacts
SET favorite = TRUE
WHERE email = 'tim@apple.com';
What you just built is a relational database - the most common type used by:
Database Type | Best For | Real Example |
---|---|---|
Relational (SQL) | Structured data with relationships | Your contacts app, banking systems |
Document (NoSQL) | Flexible data like user profiles | MongoDB for blog posts |
Key-Value | Super-fast lookups | Redis for gaming leaderboards |
Graph | Complex relationships | Facebook's friend connections |
-- Add a category column to organize contacts
ALTER TABLE contacts ADD COLUMN category TEXT;
-- Update existing contacts with categories
UPDATE contacts SET category = 'Tech Leaders'
WHERE company IN ('Tesla', 'Apple', 'Google');
UPDATE contacts SET category = 'Personal'
WHERE company IS NULL;
-- Create a table to store notes about contacts
CREATE TABLE notes (
id INTEGER PRIMARY KEY AUTOINCREMENT,
contact_id INTEGER,
note TEXT,
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (contact_id) REFERENCES contacts(id)
);
-- Add a note about a contact
INSERT INTO notes (contact_id, note)
VALUES (1, 'Met at tech conference, interested in AI projects');
-- Count contacts by company
SELECT company, COUNT(*) as employee_count
FROM contacts
GROUP BY company;
-- Find recently added contacts
SELECT name, created_at
FROM contacts
ORDER BY created_at DESC
LIMIT 5;
Your Feature | Real-World Use |
---|---|
Storing Contacts | LinkedIn's 800M+ user profiles |
Search by Name | Google Search indexing billions of pages |
Favorite Contacts | Instagram's "Close Friends" feature |
Contact Notes | CRM systems like Salesforce |
Categories | Netflix's movie genre organization |
All powered by databases similar to what you just built!
-- Add social media handles
ALTER TABLE contacts ADD COLUMN twitter TEXT;
ALTER TABLE contacts ADD COLUMN linkedin TEXT;
-- Find all contacts with social media
SELECT name, twitter, linkedin
FROM contacts
WHERE twitter IS NOT NULL OR linkedin IS NOT NULL;
-- Track meetings with contacts
CREATE TABLE meetings (
id INTEGER PRIMARY KEY AUTOINCREMENT,
contact_id INTEGER,
meeting_date DATE,
meeting_notes TEXT,
follow_up_required BOOLEAN DEFAULT FALSE,
FOREIGN KEY (contact_id) REFERENCES contacts(id)
);
-- Export contacts to share with team
SELECT name || ',' || email || ',' || phone as contact_info
FROM contacts
WHERE category = 'Tech Leaders';
-- Create a user access log
CREATE TABLE access_log (
id INTEGER PRIMARY KEY AUTOINCREMENT,
action TEXT,
user_id TEXT,
timestamp DATETIME DEFAULT CURRENT_TIMESTAMP
);
-- Log when someone views sensitive data
INSERT INTO access_log (action, user_id)
VALUES ('viewed_contacts', 'admin_user');
-- Create a simple permission system
CREATE TABLE user_permissions (
user_id TEXT PRIMARY KEY,
can_read BOOLEAN DEFAULT TRUE,
can_write BOOLEAN DEFAULT FALSE,
can_delete BOOLEAN DEFAULT FALSE
);
How would you protect email addresses in your contact manager from being seen by everyone?
You've built a real working database that can:
Command | What It Does | Example |
---|---|---|
CREATE TABLE | Makes a new table | CREATE TABLE contacts (...) |
INSERT INTO | Adds data | INSERT INTO contacts VALUES (...) |
SELECT | Finds data | SELECT * FROM contacts |
UPDATE | Changes data | UPDATE contacts SET phone = '...' |
DELETE | Removes data | DELETE FROM contacts WHERE id = 1 |
ALTER TABLE | Modifies structure | ALTER TABLE contacts ADD COLUMN ... |
Your contact manager is just the beginning. The same skills you learned today power:
Remember: Every app you use daily relies on databases. Now you know how to build them!