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

String-1 > withouEnd2

Given a string, return a version without both the first and last char of the string. The string may be any length, including 0. withouEnd2("Hello") → "ell" withouEnd2("abc") → "b" withouEnd2("ab") → "" public String withouEnd2 ( String str ) { if ( str . length ()== 1 ){ return str . substring ( 1 ); } else if ( str . length ()== 0 ){ return str ; } return ( str . substring ( 1 , str . length ()- 1 )); }

String-1 > middleTwo

Given a string of even length, return a string made of the middle two chars, so the string "string" yields "ri". The string length will be at least 2. middleTwo("string") → "ri" middleTwo("code") → "od" middleTwo("Practice") → "ct" public String middleTwo ( String str ) { return str . substring (( str . length ()/ 2 - 1 ), str . length ()/ 2 + 1 ); }