2021-09-22 17:23:25 +00:00
|
|
|
= SQL =
|
|
|
|
|
|
|
|
Stuctered Query Language is a language that describes a method of fetching and
|
|
|
|
describing the relationship between different types of data
|
|
|
|
|
2022-02-28 21:30:01 +00:00
|
|
|
== Filtering data ==
|
|
|
|
|
|
|
|
To filter data we by some specifiers we use the `WHERE` clause. The where
|
|
|
|
clause can filter based on the default comparison operators. It can also do
|
|
|
|
string comparisons.
|
|
|
|
|
|
|
|
This system also works with dates, where all dates are in format `YYYY-MM-DD`.
|
|
|
|
|
|
|
|
=== IN ===
|
|
|
|
|
|
|
|
To see if an attribute is in a set we can use the `IN` keyword in conjunction
|
|
|
|
with a `WHERE` statement. For example,
|
|
|
|
`SELECT * FROM holidays WHERE Country IN ('spain', 'portugaul', 'USA');`
|
|
|
|
|
|
|
|
=== NOT ===
|
|
|
|
|
|
|
|
The `NOT` keyword can be placed before a conditional to specify that we want
|
|
|
|
everything that does NOT mean that condition.
|
|
|
|
|
|
|
|
=== LIKE ===
|
|
|
|
|
|
|
|
`LIKE` lets us filter on patterns. We can use the character `%` to specify a
|
|
|
|
wildcard, for any number of characters. We can use the character `_` to give an
|
|
|
|
exact number of wildcard characters we are looking for. For example,
|
|
|
|
|
|
|
|
`SELECT * FROM Bands WHERE BandName LIKE '%light%';`
|
|
|
|
|
|
|
|
will return all bands with the string 'light' in their name.
|
|
|
|
|
|
|
|
=== DISTINCT ===
|
|
|
|
|
|
|
|
`DISTINCT` filters results by only returning unique values in a column
|
|
|
|
|
2022-02-28 21:15:01 +00:00
|
|
|
== Views ==
|
|
|
|
|
|
|
|
A view can be created with the following command
|
|
|
|
|
|
|
|
{{{
|
|
|
|
CREATE VIEW [view_name] AS
|
|
|
|
SELECT column1, column2, ...
|
|
|
|
FROM table
|
|
|
|
WHERE condition;
|
|
|
|
}}}
|
|
|
|
|
|
|
|
For example, this view returns all products witha price higher than the average
|
|
|
|
price of the table
|
|
|
|
|
|
|
|
{{{
|
|
|
|
CREATE VIEW [Products above average price] AS
|
|
|
|
SELECT Name, Price
|
|
|
|
FROM Products
|
|
|
|
WHERE Price > (SELECT AVG(Price) FROM Products)
|
|
|
|
|
|
|
|
SELECT * FROM [Prodcuts above average price]
|
|
|
|
}}}
|
|
|
|
|
2022-02-28 21:30:01 +00:00
|
|
|
Views *can* be inserted into, you simply must specify the columns being
|
|
|
|
inserted into.
|
|
|
|
|
2022-02-28 21:15:01 +00:00
|
|
|
== Acess control commands ==
|
|
|
|
|
|
|
|
Two commands for managing access rights
|
|
|
|
|
|
|
|
* grant
|
|
|
|
* used to grant one or more access rights or can be used to assign a user to
|
|
|
|
a role
|
|
|
|
* revoke
|
|
|
|
* revokes the acess rights
|
|
|
|
|
|
|
|
Typical access rights include
|
|
|
|
|
|
|
|
* select
|
|
|
|
* insert update
|
|
|
|
* delete
|
|
|
|
* references
|
2021-09-22 17:23:25 +00:00
|
|
|
|
|
|
|
== Also see ==
|
|
|
|
|
2022-02-28 21:15:01 +00:00
|
|
|
[[databases]]
|
2021-09-22 17:23:25 +00:00
|
|
|
|
2022-02-28 21:15:01 +00:00
|
|
|
[[index]]
|