Welcome to our SQL Tutorial! In this comprehensive guide, we'll be building a CRM (Customer Relationship Management) System using SQL. By the end of this tutorial, you'll have a solid understanding of SQL and how to use it in a real-world application.
Let's start with the basics. SQL (Structured Query Language) is a language used to communicate with databases. It allows us to create, read, update, and delete data in a structured way.
Before we can start, we need to create a database. In SQL, this is done using the CREATE DATABASE command.
CREATE DATABASE crm;Now, let's select our database:
USE crm;Tables are where we store our data. In our CRM system, we'll need tables for Customers, Orders, and Products.
CREATE TABLE Customers (
id INT PRIMARY KEY,
name VARCHAR(100),
email VARCHAR(100),
phone VARCHAR(15)
);
CREATE TABLE Products (
id INT PRIMARY KEY,
name VARCHAR(100),
price DECIMAL(10, 2)
);
CREATE TABLE Orders (
id INT PRIMARY KEY,
customer_id INT,
product_id INT,
quantity INT,
FOREIGN KEY (customer_id) REFERENCES Customers(id),
FOREIGN KEY (product_id) REFERENCES Products(id)
);Now that we have our tables, let's insert some data.
INSERT INTO Customers (id, name, email, phone) VALUES (1, 'John Doe', 'john.doe@example.com', '123-456-7890');
INSERT INTO Products (id, name, price) VALUES (1, 'Product 1', 10.99);
INSERT INTO Orders (id, customer_id, product_id, quantity) VALUES (1, 1, 1, 2);We can query our data using SELECT statements.
SELECT * FROM Customers;
SELECT * FROM Products;
SELECT * FROM Orders;We can join tables to get more specific data.
SELECT Customers.name, Orders.quantity FROM Customers
JOIN Orders ON Customers.id = Orders.customer_id;We can update data using the UPDATE command.
UPDATE Customers SET email = 'john.doe@newemail.com' WHERE id = 1;We can delete data using the DELETE command.
DELETE FROM Orders WHERE id = 1;What does SQL stand for?
What does the `CREATE DATABASE` command do?