0% found this document useful (0 votes)
12 views10 pages

Database 2

This document provides an overview of basic SQL commands for managing databases in MySQL, including creating databases and tables, inserting and querying data, and updating or deleting records. It also explains how to establish relationships between tables using foreign keys and how to join tables to retrieve combined data. Key commands such as CREATE, INSERT, SELECT, UPDATE, and DELETE are highlighted with examples.

Uploaded by

chicho creative
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views10 pages

Database 2

This document provides an overview of basic SQL commands for managing databases in MySQL, including creating databases and tables, inserting and querying data, and updating or deleting records. It also explains how to establish relationships between tables using foreign keys and how to join tables to retrieve combined data. Key commands such as CREATE, INSERT, SELECT, UPDATE, and DELETE are highlighted with examples.

Uploaded by

chicho creative
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 10

DATABASE

SQL (Structured Query Language)


Basic SQL Commands

 Basic SQL Commands


 Create a Database
A database in MySQL is like a container where all your data is stored.
Creating a database is the first step when you start working with
MySQL:
 CREATE DATABASE mydatabase;
 This creates an empty database named mydatabase.
 Use a Database
After creating a database, you must select it before performing
operations on it:
 USE mydatabase;
Create a Table

Tables store data in rows and columns. Each table must have at least
one column. Here's an example of a simple users table:
 CREATE TABLE users (
 id INT AUTO_INCREMENT PRIMARY KEY,
 name VARCHAR(100),
 email VARCHAR(100),
 age INT
 );
Cont…

 id INT AUTO_INCREMENT PRIMARY KEY:


This column holds a unique ID for each row. It
automatically increments with each new entry, ensuring uniqueness.
 name, email, age:Other columns to store user data. Each has a data type like
VARCHAR (variable-length text) and INT (integer).
Insert Data

 Why Insert Data?


Tables are like empty containers. You need to insert data (rows) to make
the table useful.
 Insert Data into the Table
Let’s insert multiple users at once:
 INSERT INTO users (name, email, age)
 VALUES ('John Doe', '[email protected]', 30),
 ('Jane Smith', '[email protected]', 25);

 This inserts two rows into the users table.


 Make sure the column order (name, email, age) matches the values you're
inserting
Query Data

 Retrieve Data with SELECT


The SELECT statement is used to fetch data from tables:
 SELECT * FROM users;
 * means "select all columns."
 The result will display all rows and columns from the users table.
 Filter Data with WHERE
You can filter the data by specific criteria:
 SELECT * FROM users WHERE age > 28;
 This query returns only users older than 28.
Update and Delete Data.

 Update Data with UPDATE


The UPDATE statement is used to modify existing data:
 UPDATE users SET age = 32 WHERE name = 'John Doe';
 This changes John Doe’s age to 32.
 Delete Data with DELETE
The DELETE statement removes rows:
 DELETE FROM users WHERE name = 'Jane Smith';
 This removes Jane Smith's row from the table.
Altering Tables

 Add a Column with ALTER TABLE


If you need to add more data to a table (e.g., a phone number for
each user), you can add a new column:
 ALTER TABLE users ADD COLUMN phone VARCHAR(15);
 This adds a phone column that can store phone numbers up to 15 characters
long.
 Drop a Column
You can also remove unnecessary columns:
 ALTER TABLE users DROP COLUMN phone;
Create a Related Table

In real-world databases, you often need multiple tables that relate to


each other. For example, you could have a users table and an orders
table, where each order is linked to a specific user.
 CREATE TABLE orders (
 id INT AUTO_INCREMENT PRIMARY KEY,
 user_id INT,
 product_name VARCHAR(100),
 FOREIGN KEY (user_id) REFERENCES users(id)
 );
The user_id column references the id column in the users table,
establishing a relationship between the two tables
Insert Data with Foreign Keys.

 Insert Data with Foreign Keys


When inserting data into the orders table, you must include a valid
user_id:

 INSERT INTO orders (user_id, product_name)


 VALUES (1, 'Laptop'), (2, 'Smartphone');
Join Tables
Use JOIN to combine data from multiple tables:
 SELECT users.name, orders.product_name
 FROM users
 JOIN orders ON users.id = orders.user_id;
 This query retrieves each user’s name and the product they ordered.

You might also like