Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
26 views

SQL For Database Management

This document contains information about SQL, basic SQL Operations, advanced SQL Concepts

Uploaded by

Turuczko Robert
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views

SQL For Database Management

This document contains information about SQL, basic SQL Operations, advanced SQL Concepts

Uploaded by

Turuczko Robert
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Getting Started with SQL

What is SQL?

SQL (Structured Query Language) is a standard programming language specifically designed for
managing and manipulating databases. It allows you to create, read, update, and delete database
records.

Key Concepts

 Database: A collection of organized data.


 Table: A structure within a database that stores data in rows and columns.
 Record: A single entry in a table, also known as a row.
 Field: A column in a table that contains a specific type of information.

Basic SQL Syntax

SQL statements are composed of various clauses, expressions, and predicates. Here is a simple
example of a SQL query:

sql
Copy code
SELECT column1, column2
FROM table_name
WHERE condition;

Setting Up a Database

To practice SQL, you need a database management system (DBMS). Popular DBMSs include
MySQL, PostgreSQL, SQLite, and Microsoft SQL Server. For simplicity, we'll use SQLite, a
lightweight, file-based database.

1. Install SQLite:
o Download and install SQLite from sqlite.org.
2. Create a Database:
o Open a terminal or command prompt.
o Run the following command to create a new database file:

sh
Copy code
sqlite3 mydatabase.db

3. Create a Table:
o Within the SQLite prompt, create a table named users:

sql
Copy code
CREATE TABLE users (
id INTEGER PRIMARY KEY,
name TEXT,
email TEXT
);

Basic SQL Operations


Inserting Data

To add data to a table, use the INSERT INTO statement.

sql
Copy code
INSERT INTO users (name, email)
VALUES ('Alice', 'alice@example.com');

You can insert multiple records at once:

sql
Copy code
INSERT INTO users (name, email)
VALUES
('Bob', 'bob@example.com'),
('Charlie', 'charlie@example.com');

Querying Data

To retrieve data from a table, use the SELECT statement.

sql
Copy code
SELECT * FROM users;

This will return all columns and rows from the users table. You can specify particular columns
to retrieve:

sql
Copy code
SELECT name, email FROM users;

Filtering Data

To filter records, use the WHERE clause.


sql
Copy code
SELECT * FROM users WHERE name = 'Alice';

Updating Data

To update existing records, use the UPDATE statement.

sql
Copy code
UPDATE users
SET email = 'alice.new@example.com'
WHERE name = 'Alice';

Deleting Data

To delete records, use the DELETE statement.

sql
Copy code
DELETE FROM users WHERE name = 'Charlie';

SQL Functions

SQL provides various functions to perform operations on data.

Aggregate Functions

 COUNT: Returns the number of rows that match a specified criterion.

sql
Copy code
SELECT COUNT(*) FROM users;

 SUM: Returns the total sum of a numeric column.

sql
Copy code
SELECT SUM(salary) FROM employees;

 AVG: Returns the average value of a numeric column.

sql
Copy code
SELECT AVG(salary) FROM employees;

 MAX: Returns the maximum value of a column.

sql
Copy code
SELECT MAX(salary) FROM employees;

 MIN: Returns the minimum value of a column.

sql
Copy code
SELECT MIN(salary) FROM employees;
String Functions

 UPPER: Converts a string to uppercase.

sql
Copy code
SELECT UPPER(name) FROM users;

 LOWER: Converts a string to lowercase.

sql
Copy code
SELECT LOWER(name) FROM users;

 LENGTH: Returns the length of a string.

sql
Copy code
SELECT LENGTH(name) FROM users;

Advanced SQL Concepts


Joining Tables

To combine rows from two or more tables, use the JOIN clause.

INNER JOIN

Returns records that have matching values in both tables.

sql
Copy code
SELECT users.name, orders.order_date
FROM users
INNER JOIN orders ON users.id = orders.user_id;
LEFT JOIN

Returns all records from the left table and the matched records from the right table. The result is
NULL from the right side if there is no match.
sql
Copy code
SELECT users.name, orders.order_date
FROM users
LEFT JOIN orders ON users.id = orders.user_id;

Creating and Using Indexes

Indexes are used to speed up the retrieval of data.

sql
Copy code
CREATE INDEX idx_user_name ON users (name);

Transactions

Transactions ensure that a series of SQL operations are executed as a single unit of work. Use the
BEGIN TRANSACTION, COMMIT, and ROLLBACK statements.

sql
Copy code
BEGIN TRANSACTION;

UPDATE accounts
SET balance = balance - 100
WHERE user_id = 1;

UPDATE accounts
SET balance = balance + 100
WHERE user_id = 2;

COMMIT;

If something goes wrong, you can roll back the transaction:

sql
Copy code
ROLLBACK;

Stored Procedures

Stored procedures are a collection of SQL statements that can be executed as a single procedure.

sql
Copy code
CREATE PROCEDURE AddUser (
IN userName VARCHAR(50),
IN userEmail VARCHAR(50)
)
BEGIN
INSERT INTO users (name, email)
VALUES (userName, userEmail);
END;

Conclusion

You've now learned the basics of SQL, including how to create and manipulate databases, insert
and query data, and use advanced concepts like joins, indexes, transactions, and stored
procedures. SQL is a powerful language that forms the backbone of database management and
manipulation. Continue practicing and exploring more advanced SQL features to enhance your
database management skills.

Happy querying!

You might also like