sql_tutorial
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| sql_tutorial [2024/10/08 14:57] – [SQL Alter Table] kalaschnikow | sql_tutorial [2024/10/09 10:19] (current) – [SQL Update] kalaschnikow | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| + | ====== SQL Tutorial ====== | ||
| + | |||
| + | My conspectus of a [[https:// | ||
| + | |||
| + | |||
| + | ===== SQL Home ===== | ||
| + | ... | ||
| + | |||
| + | ===== SQL Intro ===== | ||
| + | ... | ||
| + | |||
| + | ===== SQL Syntax ===== | ||
| + | <code sql> | ||
| + | ... | ||
| + | </ | ||
| + | |||
| + | ===== SQL Select ===== | ||
| + | <code sql> | ||
| + | |||
| + | <code sql> | ||
| + | |||
| + | <code sql> | ||
| + | |||
| + | <code sql> | ||
| + | SELECT COUNT (*) | ||
| + | FROM (SELECT DISTINCT Country FROM Customers);</ | ||
| + | |||
| + | ===== SQL Where ===== | ||
| + | <code sql> | ||
| + | SELECT * FROM Customers | ||
| + | WHERE Country=' | ||
| + | </ | ||
| + | |||
| + | * Operators: | ||
| + | * = | ||
| + | * > | ||
| + | * < | ||
| + | * >= | ||
| + | * <= | ||
| + | * <> | ||
| + | * != | ||
| + | * BETWEEN | ||
| + | * LIKE | ||
| + | * IN | ||
| + | |||
| + | ===== SQL Order By ===== | ||
| + | <code sql> | ||
| + | SELECT * FROM tab1 | ||
| + | ORDER BY col1 ASC | DESC; | ||
| + | </ | ||
| + | |||
| + | ===== SQL And ===== | ||
| + | <code sql> | ||
| + | ... | ||
| + | </ | ||
| + | |||
| + | ===== SQL Or ===== | ||
| + | <code sql> | ||
| + | ... | ||
| + | </ | ||
| + | |||
| + | ===== SQL Not ===== | ||
| + | <code sql> | ||
| + | ... | ||
| + | </ | ||
| + | |||
| + | ===== SQL Insert Into ===== | ||
| + | <code sql> | ||
| + | INSERT INTO Customers (CustomerName, | ||
| + | VALUES (' | ||
| + | </ | ||
| + | |||
| + | |||
| + | ===== SQL NULL values ===== | ||
| + | <code sql> | ||
| + | ... | ||
| + | </ | ||
| + | |||
| + | ===== SQL Update ===== | ||
| + | <code sql> | ||
| + | UPDATE Customers | ||
| + | SET ContactName = ' | ||
| + | WHERE CustomerID = 1; | ||
| + | </ | ||
| + | |||
| + | ===== SQL Delete ===== | ||
| + | <code sql> | ||
| + | ... | ||
| + | </ | ||
| + | |||
| + | |||
| + | ===== SQL Select top ===== | ||
| + | <code sql> | ||
| + | ... | ||
| + | </ | ||
| + | |||
| + | ===== SQL Aggregate functions ===== | ||
| + | <code sql> | ||
| + | ... | ||
| + | </ | ||
| + | |||
| + | ===== SQL MIN() and MAX() functions ===== | ||
| + | <code sql> | ||
| + | ... | ||
| + | </ | ||
| + | |||
| + | |||
| + | ===== SQL COUNT() function ===== | ||
| + | <code sql> | ||
| + | ... | ||
| + | </ | ||
| + | |||
| + | ===== SQL SUM() function ===== | ||
| + | <code sql> | ||
| + | ... | ||
| + | </ | ||
| + | |||
| + | ===== SQL AVG() function ===== | ||
| + | <code sql> | ||
| + | ... | ||
| + | </ | ||
| + | |||
| + | ===== SQL LIKE ===== | ||
| + | <code sql> | ||
| + | ... | ||
| + | </ | ||
| + | |||
| + | ===== SQL Wildcards ===== | ||
| + | <code sql> | ||
| + | ... | ||
| + | </ | ||
| + | |||
| + | ===== SQL IN ===== | ||
| + | <code sql> | ||
| + | ... | ||
| + | </ | ||
| + | |||
| + | ===== SQL Between ===== | ||
| + | <code sql> | ||
| + | ... | ||
| + | </ | ||
| + | |||
| + | ===== SQL Aliases ===== | ||
| + | <code sql> | ||
| + | ... | ||
| + | </ | ||
| + | |||
| + | |||
| + | ===== SQL Joins ===== | ||
| + | <code sql> | ||
| + | ... | ||
| + | </ | ||
| + | |||
| + | ===== SQL Inner Join ===== | ||
| + | <code sql> | ||
| + | ... | ||
| + | </ | ||
| + | |||
| + | ===== SQL Left Join ===== | ||
| + | <code sql> | ||
| + | ... | ||
| + | </ | ||
| + | |||
| + | ===== SQL Right Join ===== | ||
| + | <code sql> | ||
| + | ... | ||
| + | </ | ||
| + | |||
| + | |||
| + | ===== SQL Full Join ===== | ||
| + | <code sql> | ||
| + | ... | ||
| + | </ | ||
| + | |||
| + | ===== SQL Self Join ===== | ||
| + | <code sql> | ||
| + | ... | ||
| + | </ | ||
| + | |||
| + | ===== SQL Union ===== | ||
| + | <code sql> | ||
| + | ... | ||
| + | </ | ||
| + | |||
| + | |||
| + | ===== SQL Group By ===== | ||
| + | <code sql> | ||
| + | ... | ||
| + | </ | ||
| + | |||
| + | ===== SQL Having ===== | ||
| + | <code sql> | ||
| + | ... | ||
| + | </ | ||
| + | |||
| + | ===== SQL Exists ===== | ||
| + | <code sql> | ||
| + | ... | ||
| + | </ | ||
| + | |||
| + | |||
| + | ===== SQL ... ===== | ||
| + | <code sql> | ||
| + | ... | ||
| + | </ | ||
| + | |||
| + | |||
| + | ===== SQL Select Into ===== | ||
| + | |||
| + | The SELECT INTO statement copies data from one table into a new table. | ||
| + | |||
| + | <code sql> | ||
| + | SELECT * INTO CustomersBackup2017 | ||
| + | FROM Customers; | ||
| + | </ | ||
| + | |||
| + | ===== SQL ... ===== | ||
| + | <code sql> | ||
| + | ... | ||
| + | </ | ||
| + | |||
| + | |||
| + | ===== SQL Stored Procedures ===== | ||
| + | <code sql> | ||
| + | -- create stored procedure | ||
| + | CREATE PROCEDURE SelectAllCustomers | ||
| + | AS | ||
| + | SELECT * FROM Customers | ||
| + | GO; | ||
| + | |||
| + | -- execute stored procedure | ||
| + | EXEC SelectAllCustomers; | ||
| + | </ | ||
| + | |||
| + | |||
| + | ===== SQL ... ===== | ||
| + | <code sql> | ||
| + | ... | ||
| + | </ | ||
| + | |||
| + | ===== SQL Create DB ===== | ||
| + | <code sql> | ||
| + | CREATE DATABASE testDB; | ||
| + | </ | ||
| + | |||
| + | ===== SQL Drop DB ===== | ||
| + | <code sql> | ||
| + | DROP DATABASE testDB; | ||
| + | </ | ||
| + | |||
| + | ===== SQL ... ===== | ||
| + | <code sql> | ||
| + | ... | ||
| + | </ | ||
| + | |||
| + | ===== SQL Create Table ===== | ||
| + | <code sql> | ||
| + | CREATE TABLE Persons ( | ||
| + | PersonID int, | ||
| + | LastName varchar(255), | ||
| + | FirstName varchar(255), | ||
| + | Address varchar(255), | ||
| + | City varchar(255) | ||
| + | ); | ||
| + | </ | ||
| + | |||
| + | |||
| + | ===== SQL Drop Table ===== | ||
| + | <code sql> | ||
| + | DROP TABLE Shippers; | ||
| + | </ | ||
| + | |||
| + | |||
| + | <code sql> | ||
| + | -- truncate table deletes all data in the table, but not the table itself | ||
| + | TRUNCATE TABLE table_name; | ||
| + | </ | ||
| + | |||
| + | ===== SQL Alter Table ===== | ||
| + | <code sql> | ||
| + | ALTER TABLE Customers | ||
| + | ADD Email varchar(255); | ||
| + | |||
| + | ALTER TABLE Customers | ||
| + | DROP COLUMN Email; | ||
| + | |||
| + | -- change data type | ||
| + | ALTER TABLE table_name | ||
| + | ALTER COLUMN column_name new_datatype; | ||
| + | </ | ||
| + | |||
| + | ===== SQL ... ===== | ||
| + | <code sql> | ||
| + | ... | ||
| + | </ | ||
| + | |||
| + | ===== SQL constraints ===== | ||
| + | <code sql> | ||
| + | CREATE TABLE table_name ( | ||
| + | column1 datatype constraint, | ||
| + | column2 datatype constraint, | ||
| + | column3 datatype constraint, | ||
| + | .... | ||
| + | ); | ||
| + | </ | ||
| + | |||
| + | Constraints: | ||
| + | |||
| + | * NOT NULL - Ensures that a column cannot have a NULL value | ||
| + | * UNIQUE - Ensures that all values in a column are different | ||
| + | * PRIMARY KEY - A combination of a NOT NULL and UNIQUE. Uniquely identifies each row in a table | ||
| + | * FOREIGN KEY - Prevents actions that would destroy links between tables | ||
| + | * CHECK - Ensures that the values in a column satisfies a specific condition | ||
| + | * DEFAULT - Sets a default value for a column if no value is specified | ||
| + | * CREATE INDEX - Used to create and retrieve data from the database very quickly | ||
| + | |||
| + | |||
| + | ===== SQL ... ===== | ||
| + | <code sql> | ||
| + | ... | ||
| + | </ | ||
| + | |||
| + | ===== SQL Primary Key ===== | ||
| + | <code sql> | ||
| + | -- one column as PK | ||
| + | CREATE TABLE Persons ( | ||
| + | ID int NOT NULL PRIMARY KEY, | ||
| + | LastName varchar(255) NOT NULL, | ||
| + | FirstName varchar(255), | ||
| + | Age int | ||
| + | ); | ||
| + | |||
| + | -- multiple columns as PK | ||
| + | CREATE TABLE Persons ( | ||
| + | ID int NOT NULL, | ||
| + | LastName varchar(255) NOT NULL, | ||
| + | FirstName varchar(255), | ||
| + | Age int, | ||
| + | CONSTRAINT PK_Person PRIMARY KEY (ID, | ||
| + | ); | ||
| + | </ | ||
| + | |||
| + | ===== SQL Foreign Key ===== | ||
| + | |||
| + | FOREIGN KEY constraint prevents invalid data from being inserted into the foreign key column, because it has to be one of the values contained in the parent table. | ||
| + | |||
| + | <code sql> | ||
| + | CREATE TABLE Orders ( | ||
| + | OrderID int NOT NULL PRIMARY KEY, | ||
| + | OrderNumber int NOT NULL, | ||
| + | PersonID int FOREIGN KEY REFERENCES Persons(PersonID) | ||
| + | ); | ||
| + | </ | ||
| + | |||
| + | ===== SQL ... ===== | ||
| + | <code sql> | ||
| + | ... | ||
| + | </ | ||
| + | |||
| + | ===== SQL Index ===== | ||
| + | Indexes are used to retrieve data from the database more quickly than otherwise. The users cannot see the indexes, they are just used to speed up searches/ | ||
| + | |||
| + | <code sql> | ||
| + | CREATE INDEX idx_pname | ||
| + | ON Persons (LastName, FirstName); | ||
| + | </ | ||
| + | |||
| + | ===== SQL ... ===== | ||
| + | <code sql> | ||
| + | ... | ||
| + | </ | ||
| + | |||
| + | ===== SQL Views ===== | ||
| + | <code sql> | ||
| + | CREATE VIEW view_name AS | ||
| + | SELECT column1, column2, ... | ||
| + | FROM table_name | ||
| + | WHERE condition; | ||
| + | </ | ||
| + | |||
| + | ===== SQL Injection ===== | ||
| + | SQL injection is a code injection technique that might destroy your database. | ||
| + | |||
| + | SQL injection is one of the most common web hacking techniques. | ||
| + | |||
| + | SQL injection is the placement of malicious code in SQL statements, via web page input. | ||
| + | |||
| + | <code sql> | ||
| + | -- this statement delivers not the data only from user 105, but the full table | ||
| + | SELECT * FROM Users WHERE UserId = 105 OR 1=1; | ||
| + | </ | ||
| + | |||
| + | ===== SQL ... ===== | ||
| + | <code sql> | ||
| + | ... | ||
| + | </ | ||
| + | |||
| + | |||
| + | |||
| + | |||
| + | |||
| + | |||
| + | |||
| + | |||
| + | |||
