Professional Documents
Culture Documents
SQL New
SQL New
Local server
PhpMyAdmin - ծրագիր է, GUI որի միջոցով աշխատում ենք MySQL տվյալների
բազայի հետ. Այս ծրագրի մեջ գրելում ենք SQL հրահանգներ, ստեղծում
աղյուսակներ, տվյալների բազաներ, տվյալների բազայի մեջ ինֆո
ավելացնում
Local server
XAMPP
OpenServer
MAMP
ՏԲ -ի ստեղծում
Ստեղծում են ՏԲ, table
CREATE DATABASE name;
Select/Select distinct
SELECT * FROM table_name; - select all table
SELECT column1, column2, ... FROM table_name; - select column/s from table
SELECT DISTINCT column1, column2, ...FROM table_name;
- select different values from table, we use it when column contains many duplicate
values and we want to get different values
SELECT COUNT(DISTINCT Country) FROM Customers; - These are lists the
number of different (distinct) customer countries
NOT
SELECT * FROM Customers
WHERE NOT City='Paris';
It can be used with and/or/= operators
NOT BETWEEN
Select customers with a customerID not between 10 and 60
SELECT * FROM Customers
WHERE CustomerID NOT BETWEEN 10 AND 60;
NOT IN
AND
The AND operator displays a record if all the conditions are TRUE.
SELECT * FROM Customers WHERE City = 'Berlin' AND PostalCode > 2000;
OR
The OR operator displays a record if any of the conditions are TRUE։
SELECT *
FROM Customers
WHERE Country = 'Germany' OR Country = 'Spain';
ORDER BY Keyword
It is used to sort in ascending or descending order.
Default - ASC,
SELECT column1, column2, ...
FROM table_name
ORDER BY column1, column2, ... ASC|DESC;
INSERT INTO
It is used to insert new records in a table.
1. INSERT INTO table_name
VALUES (value1, value2, value3, ...);
2. INSERT INTO table_name (column1, column2, column3, ...)
VALUES (value1, value2, value3, ...);
3. INSERT INTO table_name (column1, column2, column3, ...)
VALUES (value1, value2, value3, ...),
(value1, value2, value3, ...)
(value1, value2, value3, ...);
NULL Values
It is show empty columns
We can test NULL values with IS NULL and IS NOT NULL operators
SELECT column_names
FROM table_name
WHERE column_name IS NULL;
Or
SELECT column_names
FROM table_name
WHERE column_name IS NOT NULL;
UPDATE
It is used to changing existing values
UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition;
UPDATE Customers
SET ContactName = 'Alfred Schmidt', City= 'Frankfurt'
WHERE CustomerID = 1;
If you forget the WHERE clause, all records in the table will be updated!
DELETE
Delete rows
DELETE FROM table_name WHERE condition;
DELETE FROM Customers WHERE CustomerName='Alfreds Futterkiste';
DROP TABLE
It for deleted table
DROP TABLE Customers;
SELECT *
FROM (SELECT column_name(s) FROM table_name ORDER BY column_name(s))
WHERE ROWNUM <= number;
Functions
MIN() and MAX() Functions
SELECT MIN(column_name)
FROM table_name
WHERE condition;
To give the column a new name, use the AS keyword:
SELECT MIN(Price) AS SmallestPrice
FROM Products;
COUNT() Function
It returns the number of rows
SELECT COUNT(column_name)
FROM table_name
WHERE condition;
SUM() Function
It returns the total sum of a numeric column.
SELECT SUM(column_name)
FROM table_name
WHERE condition;
SELECT SUM(Quantity) AS total
FROM OrderDetails;
total amount
SELECT SUM(Price * Quantity)
FROM OrderDetails
LEFT JOIN Products ON OrderDetails.ProductID = Products.ProductID;
AVG() Function
SELECT AVG(column_name)
FROM table_name
WHERE condition;
SELECT AVG(Price) AS [average price]
FROM Products;
Higher Than Average:
SELECT * FROM Products
WHERE price > (SELECT AVG(price) FROM Products);
LIKE Operator
The LIKE operator is used in a WHERE clause to search for a specified pattern in a column.
Two symbols are used with Like operator
% represents zero, one, or multiple characters
_ represents one, single character
Return all customers from a city that starts SELECT * FROM Customers
with 'L' followed by one wildcard character, WHERE city LIKE 'L_nd__';
then 'nd' and then two wildcard
characters:
Return all customers that starts with "a" SELECT * FROM Customers
and are at least 3 characters in length: WHERE CustomerName LIKE 'a__%';
Return all customers that have "r" in the SELECT * FROM Customers
second position: WHERE CustomerName LIKE '_r%';
Symbo Description
l
Wildcard Characters
Select all records where the first letter of SELECT * FROM Customers
the City is an "a" or a "c" or an "s". WHERE City LIKE '[acs]%';
Select all records where the first letter of SELECT * FROM Customers
the City starts with anything from an "a" to WHERE City LIKE '[a-f]%';
an "f".
Select all records where the first letter of SELECT * FROM Customers
the City is NOT an "a" or a "c" or an "f". WHERE City LIKE '[!acf%';
IN Operator
SELECT column_name(s)
FROM table_name
WHERE column_name IN (value1, value2, ...);
NOT IN
IN (SELECT)/NOT IN (SELECT)
SELECT column_name(s)
FROM table_name
WHERE column_name BETWEEN value1 AND value2;