Przejdź do głównej zawartości

SQL - Podstawy



SELECT * FROM branch;

CREATE TABLE table_name (
column1 datatype,
column2 datatype,
column3 datatype,
....
);
Typy danych w kolumnach

Text data types:

VARCHAR (40) - string z ograniczoną liczbą znaków
TEXT - string
BLOB - Binary Larg Objects - 65000 bytes of data
ENUM('X','Y','Z') - list of values in order we enter them
SET - values are ENUM up to 64 list items  
Number data types:
INT (size) - integer
FLOAT(size,d) - small number with floating decimal point, d - decimal point
DOUBLE(size,d) large number with floating decimal point

Date data types:
DATE() - YYYY-MM-DD
TIMESTAMP()
TIME()


SQL Keywords:

ADD - add column
ALTER - add delete modify columns in table or change data types of column in table
CREATE - create database, table, index, view, procedure
  • CREATE DATABASE
  • CREATE TABLE
  • CREATE INDEX
  • CREATE VIES
DELETE - delete rows from table
DESC - sort results descending order
DROP - delete column, constrains, database, index table or view
  • DROP COLUMN
  • DROP DATABASE
  • DROP INDEX
  • DROP CONSTRAINS
  • DROP TABLE
FOREIGN KEY - a constrain that is key used to link two tables together
UPDATE - updates existing rows in table

SELECT DISTINCT - find out all different values from specific column and table

https://www.w3schools.com/sql/sql_ref_keywords.asp

FUNKCIONS:

block of code which can calculate some values - count, average

COUNT(column name)

-- find the number of employees

SELECT COUNT(super_id)
FROM employee;

-- find female employees born after 1970

SELECT COUNT(emp_id)
FROM employee
WHERE sex = 'F' AND birth_day > '1971-01-01'

-- find the average of all employee's salaries

SELECT AVG(salary)
FROM employee
WHERE sex = 'M';

-- find the sum of all salaries

SELECT SUM(salary)
FROM employee;

Aggregation:

SELECT COUNT(column_name), column_name
FROM table_name
GROUP BY value;



SQL Wildcards = Regular Expressions

-- % = any # characters, _ = one character
-- fin any client who are in LCC

SELECT *
FROM client
WHERE client_name LIKE '%LLC';

SELECT *
FROM branch_supplier
WHERE supplier_name LIKE '% Label%';

-- find any employee born in October

SELECT *
FROM employee
WHERE birth_day LIKE '____-02%';

-- find any client who are schools

SELECT *
FROM client
WHERE client_name LIKE '%school%';

UNION - selection from multiply tables

-- find a list of employee and branch names
-- Union works only for the same number of columns X UNION X

SELECT first_name AS Company_Names
FROM employee
UNION
SELECT branch_name
FROM branch
UNION
SELECT client_name
FROM client;

-- find a list of all clients and branch suppliers names

SELECT client_name, client.branch_id
FROM client
UNION
SELECT supplier_name, branch_supplier.branch_id
FROM branch_supplier;

JOIN - join columns from different tables

Here are the different types of the JOINs in SQL:
  • (INNER) JOIN: Returns records that have matching values in both tables
  • LEFT (OUTER) JOIN: Return all records from the left table, and the matched records from the right table
  • RIGHT (OUTER) JOIN: Return all records from the right table, and the matched records from the left table
  • FULL (OUTER) JOIN: Return all records when there is a match in either left or right table


NESTED QUERIES - more SELECT statements

SELECT employee.first_name, employee.last_name
FROM employee
WHERE employee.emp_id IN (

             SELECT works_with.emp_id FROM works_with
             WHERE works_with.total_sales > 30000
);

ON DELETE CASCADE
ON DELETE SET

Komentarze

Popularne posty z tego bloga

Skrócony zapis if - instrukcja warunkowa java

Instrukcja warunkowa - warunek i rezultat. if (warunek) { jesli spełniony wykonań operacje i zwróć wynik; } warunek nie spełniony Możliwości skrócenia kodu instrukcji warunkowej if (i < 0) ? i-- : i++; Jeżeli i mniejsze od zera to i-- jezeli false to i++ if (i < 0) {     i--; } else {     i++; } Skrócony zapis instrukcji warunkowej else if (i < 0) ? i--;  inna_zmienna=4; : i++; if (i < 0) {     i--; } else {     i++;     inna_zmienna = 4; } Skrócony zapis if

Runnable and Call able - Java rekrutacja

Runnable - interfejs zawierający metode run() - obiekt implementujący tą metodę tworzy wątek thread public interface Runnable The Runnable interface should be implemented by any class whose instances are intended to be executed by a thread. The class must define a method of no arguments called run. This interface is designed to provide a common protocol for objects that wish to execute code while they are active. For example, Runnable is implemented by class Thread. Being active simply means that a thread has been started and has not yet been stopped.  In addition, Runnable provides the means for a class to be active while not subclassing Thread. A class that implements Runnable can run without subclassing Thread by instantiating a Thread instance and passing itself in as the target. In most cases, the Runnable interface should be used if you are only planning to override the run() method and no other Thread methods. This is important because classes should not be subclas...

wait and notify() Methods in Java - rekrutacja

Synchronizacja wątków. Procesor może wykonywać wiele zadań jednoczenśnie - concurrent software. Java wspiera współbieżność jest potrzebna synchronizacja ponieważ różne wątki threads mogą w tym samym czasie usiłować zmodyfikować ten sam zasób jeśli nie są zarządzane poprawinie. Object.wait() - zawiesza wątek - thread suspension Object.notify() - wznów wątek - thread wake up Object.notifyAll() - wznowienie wszystkich wątków