Počinjem sada malo ovime da se bavim.
Do sada sam radila samo neke male upite nad MySQL bazicama.
Dobar link za početnike (tj takve kao što sam ja).
*****
Par opštih stvari vezanih za SQL :
SQL stands for Structured Query Language
SQL lets you access and manipulate databases
SQL is an ANSI (American National Standards Institute) standard
SQL keywords are NOT case sensitive
*****
Some of The Most Important SQL Commands :
SELECT – extracts data from a database
UPDATE – updates data in a database
DELETE – deletes data from a database
INSERT INTO – inserts new data into a database
CREATE DATABASE – creates a new database
ALTER DATABASE – modifies a database
CREATE TABLE – creates a new table
ALTER TABLE – modifies a table
DROP TABLE – deletes a table
CREATE INDEX – creates an index (search key)
DROP INDEX – deletes an index
*****
Uobičajena forma SQL upita :
SELECT column_name1,column_name2 FROM table_name;
SELECT DISTINCT column_name,column_name FROM table_name;
U ovom slučaju se traže sve JEDINSTVENE kombinacije OBE kolone, I to se prikazuje.
SELECT column_name,column_name FROM table_name WHERE column_name operator value;
Pri čemu se “WHERE” klauzula može odnositi na bilo koju vrednost iz navedenih kolona, npr :
select distinct Ime,Prezime,ImeOca from dbo.ZAPOSLENI where Ime=’VESNA’;
*****
Operatori i njihov opis :
= Equal
<> Not equal. Note: In some versions of SQL this operator may be written as !=
> Greater than
< Less than >= Greater than or equal
<= Less than or equal
BETWEEN Between an inclusive range
LIKE Search for a pattern
IN To specify multiple possible values for a column
The AND operator displays a record if both the first condition AND the second condition are true.
The OR operator displays a record if either the first condition OR the second condition is true.
*****
SELECT column_name, column_name FROM table_name ORDER BY column_name ASC|DESC, column_name ASC|DESC;
*****
Kako povezati dve tabele u istoj bazi, tako da jednim upitom prikazujemo podatke iz obe tabele :
Tabele su : Orders i Customers, a kolone Orders.CustomerID i Customers.CustomerID su iste u obe tabele (tj preko njih se ove tabele mogu povezati).
Upit :
SELECT Orders.OrderID, Customers.CustomerName, Orders.OrderDate
FROM Orders
INNER JOIN Customers
ON Orders.CustomerID=Customers.CustomerID;
SQL joins are used to combine rows from two or more tables.
*****
WildCards :
% A substitute for zero or more characters – koristi se samo sa LIKE, ne sa =!!!!
_ A substitute for a single character – koristi se samo sa LIKE, ne sa =!!!!
[charlist] Sets and ranges of characters to match
[^charlist] or [!charlist] Matches only a character NOT specified within the brackets
*****
Spajanje podataka iz tabela
An SQL JOIN clause is used to combine rows from two or more tables, based on a common field between them.
The most common type of join is: SQL INNER JOIN (simple join). An SQL INNER JOIN returns all rows from multiple tables where the join condition is met (link).
The INNER JOIN keyword selects all rows from both tables as long as there is a match between the columns in both tables.
The LEFT JOIN keyword returns all rows from the left table (table1), with the matching rows in the right table (table2). The result is NULL in the right side when there is no match.
The RIGHT JOIN keyword returns all rows from the right table (table2), with the matching rows in the left table (table1). The result is NULL in the left side when there is no match.
The FULL OUTER JOIN keyword returns all rows from the left table (table1) and from the right table (table2).
The FULL OUTER JOIN keyword combines the result of both LEFT and RIGHT joins.
Vizuelni prikaz (uzet odavde) :
visual_sql_joins_orig