html-flip-book-react 0.0.0-alpha.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/flip-book.d.ts +14 -0
- package/dist/flip-book.d.ts.map +1 -0
- package/dist/flip-book.js +1410 -0
- package/dist/flip-book.js.map +1 -0
- package/example/.vscode/settings.json +3 -0
- package/example/README.md +47 -0
- package/example/assets/pages_data/en/assets/cover.jpg +0 -0
- package/example/assets/pages_data/en/assets/sql-command.png +0 -0
- package/example/assets/pages_data/en/content/000-introduction.md +85 -0
- package/example/assets/pages_data/en/content/001-databases.md +39 -0
- package/example/assets/pages_data/en/content/002-install-mysql.md +162 -0
- package/example/assets/pages_data/en/content/003-creating-tables.md +304 -0
- package/example/assets/pages_data/en/content/004-basic-syntax.md +145 -0
- package/example/assets/pages_data/en/content/005-select.md +359 -0
- package/example/assets/pages_data/en/content/006-where.md +225 -0
- package/example/assets/pages_data/en/content/007-order-and-group-by.md +142 -0
- package/example/assets/pages_data/en/content/008-insert.md +86 -0
- package/example/assets/pages_data/en/content/009-update.md +92 -0
- package/example/assets/pages_data/en/content/010-delete.md +28 -0
- package/example/assets/pages_data/en/content/011-join.md +297 -0
- package/example/assets/pages_data/en/content/012-sql-command-categories.md +121 -0
- package/example/assets/pages_data/en/content/013-sub-queries.md +112 -0
- package/example/assets/pages_data/en/content/014-unions.md +124 -0
- package/example/assets/pages_data/en/content/015-Keys-in-a-Relational Database.md +51 -0
- package/example/assets/pages_data/en/content/016-Logical-operator-keywords.md +17 -0
- package/example/assets/pages_data/en/content/017-having-clause_aggregate-functions.md +184 -0
- package/example/assets/pages_data/en/content/018-essential-mysql-functions.md +190 -0
- package/example/assets/pages_data/en/content/019-triggers-in-sql.md +133 -0
- package/example/assets/pages_data/en/content/020-TCL-commands.md +154 -0
- package/example/assets/pages_data/en/content/021-DCL-commands.md +126 -0
- package/example/assets/pages_data/en/content/100-mysqldump.md +109 -0
- package/example/assets/pages_data/en/content/101-learn-materialize.md +267 -0
- package/example/assets/pages_data/en/content/999-conclusion.md +24 -0
- package/example/assets/pages_data/he/4.txt +2 -0
- package/example/assets/pages_data/he/5.txt +4 -0
- package/example/assets/pages_data/he/6.txt +4 -0
- package/example/index.html +21 -0
- package/example/package-lock.json +5324 -0
- package/example/package.json +39 -0
- package/example/src/App.css +52 -0
- package/example/src/App.tsx +25 -0
- package/example/src/EnBook.tsx +55 -0
- package/example/src/HeBook.tsx +44 -0
- package/example/src/index.tsx +12 -0
- package/example/vite-env.d.ts +1 -0
- package/example/vite.config.js +84 -0
- package/package.json +45 -0
- package/src/FlipBook.tsx +45 -0
- package/vite.config.js +66 -0
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
# SQL | DDL, DQL, DML, DCL and TCL Commands
|
|
2
|
+
Structured Query Language(SQL), as we all know, is the database language by which we can perform certain operations on the existing database. Also, we can use this language to create a database. SQL uses specific commands like Create, Drop, Insert, etc., to carry out the required tasks.
|
|
3
|
+
1. **DDL** – Data Definition Language
|
|
4
|
+
2. **DQl** – Data Query Language
|
|
5
|
+
3. **DML** – Data Manipulation Language
|
|
6
|
+
4. **DCL** – Data Control Language
|
|
7
|
+
|
|
8
|
+
#### Though many resources claim there to be another category of SQL clauses TCL – Transaction Control Language, so we will see in detail about TCL as well.
|
|
9
|
+

|
|
10
|
+
|
|
11
|
+
### DDL (Data Definition Language):
|
|
12
|
+
DDL or Data Definition Language consists of the SQL commands used to define the database schema. It simply deals with descriptions of the database schema and is used to create and modify the structure of database objects in the database. These commands usually are not used by a general user, who should be accessing the database via an application.
|
|
13
|
+
|
|
14
|
+
#### List of DDL commands:
|
|
15
|
+
- `CREATE`: This command is used to create the database or its objects (like table, index, function, views, store procedure, and triggers).
|
|
16
|
+
```sql
|
|
17
|
+
CREATE TABLE Persons (
|
|
18
|
+
PersonID int,
|
|
19
|
+
LastName varchar(255),
|
|
20
|
+
FirstName varchar(255),
|
|
21
|
+
Address varchar(255),
|
|
22
|
+
City varchar(255)
|
|
23
|
+
);
|
|
24
|
+
```
|
|
25
|
+
- `DROP`: This command is used to delete objects from the database.
|
|
26
|
+
|
|
27
|
+
```sql
|
|
28
|
+
DROP TABLE table_name;
|
|
29
|
+
```
|
|
30
|
+
- `ALTER`: This is used to alter the structure of the database.
|
|
31
|
+
```sql
|
|
32
|
+
ALTER TABLE Persons
|
|
33
|
+
ADD Age int;
|
|
34
|
+
```
|
|
35
|
+
- `TRUNCATE`: This is used to remove all records from a table, including all spaces allocated for the records.
|
|
36
|
+
```sql
|
|
37
|
+
TRUNCATE TABLE Persons;
|
|
38
|
+
```
|
|
39
|
+
- `COMMENT`: This is used to add comments to the data dictionary.
|
|
40
|
+
```sql
|
|
41
|
+
--SELECT * FROM Customers;
|
|
42
|
+
SELECT * FROM Persons;
|
|
43
|
+
```
|
|
44
|
+
-` RENAME`: This is used to rename an object existing in the database.
|
|
45
|
+
```sql
|
|
46
|
+
ALTER TABLE Persons
|
|
47
|
+
RENAME COLUMN Age TO Year;
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
#### DQL (Data Query Language):
|
|
51
|
+
**DQL** statements are used for performing queries on the data within schema objects. The purpose of the DQL Command is to get some schema relation based on the query passed to it. We can define DQL as follows. It is a component of the SQL statement that allows getting data from the database and imposing order upon it. It includes the SELECT statement. This command allows getting the data out of the database to perform operations with it. When a SELECT is fired against a table(s), the result is compiled into a different temporary table, which is displayed or perhaps received by the program, i.e. a front-end.
|
|
52
|
+
|
|
53
|
+
#### List of DQL:
|
|
54
|
+
`SELECT`: It is used to retrieve data from the database.
|
|
55
|
+
```sql
|
|
56
|
+
SELECT * FROM table_name;
|
|
57
|
+
```
|
|
58
|
+
```
|
|
59
|
+
+--------+--------------+------------+--------+---------+
|
|
60
|
+
| emp_id | emp_name | hire_date | salary | dept_id |
|
|
61
|
+
+--------+--------------+------------+--------+---------+
|
|
62
|
+
| 1 | Ethan Hunt | 2001-05-01 | 5000 | 4 |
|
|
63
|
+
| 2 | Tony Montana | 2002-07-15 | 6500 | 1 |
|
|
64
|
+
| 3 | Sarah Connor | 2005-10-18 | 8000 | 5 |
|
|
65
|
+
| 4 | Rick Deckard | 2007-01-03 | 7200 | 3 |
|
|
66
|
+
| 5 | Martin Blank | 2008-06-24 | 5600 | NULL |
|
|
67
|
+
+--------+--------------+------------+--------+---------+
|
|
68
|
+
```
|
|
69
|
+
The SQL commands that deal with the manipulation of data present in the database belong to DML or Data Manipulation Language, including most of the SQL statements. It is the component of the SQL statement that controls access to data and the database. DCL statements are grouped with DML statements.
|
|
70
|
+
|
|
71
|
+
#### List of DML commands:
|
|
72
|
+
- `INSERT `: It is used to insert data into a table.
|
|
73
|
+
```sql
|
|
74
|
+
INSERT INTO Customers
|
|
75
|
+
(CustomerName, ContactName, Address, City, PostalCode, Country)
|
|
76
|
+
VALUES
|
|
77
|
+
('Cardinal', 'Tom B. Erichsen', 'Skagen 21', 'Stavanger', '4006', 'Norway');
|
|
78
|
+
```
|
|
79
|
+
- `UPDATE`: It is used to update existing data within a table.
|
|
80
|
+
|
|
81
|
+
```sql
|
|
82
|
+
UPDATE Customers
|
|
83
|
+
SET ContactName='Alfred Schmidt', City='Frankfurt'
|
|
84
|
+
WHERE CustomerID = 1;
|
|
85
|
+
```
|
|
86
|
+
- `DELETE `: It is used to delete records from a database table.
|
|
87
|
+
```sql
|
|
88
|
+
DELETE FROM Customers WHERE CustomerName='Alfreds Futterkiste';
|
|
89
|
+
```
|
|
90
|
+
- `LOCK`: Table control concurrency.
|
|
91
|
+
```sql
|
|
92
|
+
LOCK TABLES table_name [READ | WRITE]
|
|
93
|
+
--------------------------------------
|
|
94
|
+
UNLOCK TABLES;
|
|
95
|
+
```
|
|
96
|
+
- `CALL`: Call a PL/SQL or JAVA subprogram.
|
|
97
|
+
```sql
|
|
98
|
+
CREATE PROCEDURE procedure_name
|
|
99
|
+
AS sql_statement
|
|
100
|
+
GO;
|
|
101
|
+
```
|
|
102
|
+
#### Execute a Stored Procedure
|
|
103
|
+
```sql
|
|
104
|
+
EXEC procedure_name;
|
|
105
|
+
```
|
|
106
|
+
- `EXPLAIN PLAN`: It describes the access path to data.
|
|
107
|
+
|
|
108
|
+
#### DCL (Data Control Language):
|
|
109
|
+
DCL includes commands such as GRANT and REVOKE, which mainly deal with the database system's rights, permissions, and other controls.
|
|
110
|
+
|
|
111
|
+
##### List of DCL commands:
|
|
112
|
+
- `GRANT`: This command gives users access privileges to the database.
|
|
113
|
+
- `REVOKE`: This command withdraws the user’s access privileges given by using the GRANT command.
|
|
114
|
+
|
|
115
|
+
Though many resources claim there to be another category of SQL clauses TCL – Transaction Control Language, we will see in detail about TCL. TCL commands deal with the transaction within the database.
|
|
116
|
+
|
|
117
|
+
##### List of TCL commands:
|
|
118
|
+
- `COMMIT`: Commits a Transaction.
|
|
119
|
+
- `ROLLBACK`: Rollbacks a transaction in case of any error occurs.
|
|
120
|
+
- `SAVEPOINT`:Sets a savepoint within a transaction.
|
|
121
|
+
- `SET TRANSACTION`: Specify characteristics for the transaction.
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
# SQL Sub Queries
|
|
2
|
+
|
|
3
|
+
### A subquery is a SQL query nested inside a larger query.
|
|
4
|
+
|
|
5
|
+
- A subquery may occur in
|
|
6
|
+
- A SELECT clause
|
|
7
|
+
- A FROM clause
|
|
8
|
+
- A WHERE clause
|
|
9
|
+
|
|
10
|
+
- The subquery can be nested inside a SELECT, INSERT, UPDATE, or DELETE statement or inside another subquery.
|
|
11
|
+
- A subquery is usually added within the WHERE Clause of another SQL SELECT statement.
|
|
12
|
+
- The inner query executes first before its parent query so that the results of an inner query can be passed to the outer query.
|
|
13
|
+
|
|
14
|
+
#### You can use a subquery in a SELECT, INSERT, DELETE, or UPDATE statement to perform the following tasks:
|
|
15
|
+
- Compare an expression to the result of the query.
|
|
16
|
+
- Determine if an expression is included in the results of the query.
|
|
17
|
+
- Check whether the query selects any rows.
|
|
18
|
+
|
|
19
|
+
**_Subqueries with the `SELECT` Statement_**:
|
|
20
|
+
|
|
21
|
+
Consider the CUSTOMERS table having the following records
|
|
22
|
+
|
|
23
|
+
+----+----------+-----+-----------+----------+
|
|
24
|
+
| ID | NAME | AGE | ADDRESS | SALARY |
|
|
25
|
+
+----+----------+-----+-----------+----------+
|
|
26
|
+
| 1 | Ramesh | 35 | Ahmedabad | 2000.00 |
|
|
27
|
+
| 2 | Khilan | 25 | Delhi | 1500.00 |
|
|
28
|
+
| 3 | Kaushik | 23 | Kota | 2000.00 |
|
|
29
|
+
| 4 | Chaitali | 25 | Mumbai | 6500.00 |
|
|
30
|
+
| 5 | Hardik | 27 | Bhopal | 8500.00 |
|
|
31
|
+
| 6 | Komal | 22 | MP | 4500.00 |
|
|
32
|
+
| 7 | Muffy | 24 | Indore | 10000.00 |
|
|
33
|
+
+----+----------+-----+-----------+----------+
|
|
34
|
+
Now, let us check the following subquery with a SELECT statement.
|
|
35
|
+
|
|
36
|
+
_**Example**_:
|
|
37
|
+
```sql
|
|
38
|
+
SELECT *
|
|
39
|
+
FROM CUSTOMERS
|
|
40
|
+
WHERE ID IN (
|
|
41
|
+
SELECT ID
|
|
42
|
+
FROM CUSTOMERS
|
|
43
|
+
WHERE SALARY > 4500
|
|
44
|
+
);
|
|
45
|
+
```
|
|
46
|
+
This would produce the following result.
|
|
47
|
+
|
|
48
|
+
+----+----------+-----+---------+----------+
|
|
49
|
+
| ID | NAME | AGE | ADDRESS | SALARY |
|
|
50
|
+
+----+----------+-----+---------+----------+
|
|
51
|
+
| 4 | Chaitali | 25 | Mumbai | 6500.00 |
|
|
52
|
+
| 5 | Hardik | 27 | Bhopal | 8500.00 |
|
|
53
|
+
| 7 | Muffy | 24 | Indore | 10000.00 |
|
|
54
|
+
+----+----------+-----+---------+----------+
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
**_Subqueries with the `UPDATE` Statement_**:
|
|
58
|
+
|
|
59
|
+
The subquery can be used in conjunction with the UPDATE statement. Either single or multiple columns in a table can be updated when using a subquery with the UPDATE statement.
|
|
60
|
+
|
|
61
|
+
**_Example_**:
|
|
62
|
+
|
|
63
|
+
Assuming, we have CUSTOMERS_BKP table available which is backup of CUSTOMERS table. The following example updates SALARY by 0.25 times in the CUSTOMERS table for all the customers whose AGE is greater than or equal to 27.
|
|
64
|
+
```sql
|
|
65
|
+
UPDATE CUSTOMERS
|
|
66
|
+
SET SALARY = SALARY * 0.25
|
|
67
|
+
WHERE AGE IN (
|
|
68
|
+
SELECT AGE
|
|
69
|
+
FROM CUSTOMERS_BKP
|
|
70
|
+
WHERE AGE >= 27
|
|
71
|
+
);
|
|
72
|
+
```
|
|
73
|
+
This would impact two rows and finally CUSTOMERS table would have the following records.
|
|
74
|
+
|
|
75
|
+
+----+----------+-----+-----------+----------+
|
|
76
|
+
| ID | NAME | AGE | ADDRESS | SALARY |
|
|
77
|
+
+----+----------+-----+-----------+----------+
|
|
78
|
+
| 1 | Ramesh | 35 | Ahmedabad | 125.00 |
|
|
79
|
+
| 2 | Khilan | 25 | Delhi | 1500.00 |
|
|
80
|
+
| 3 | Kaushik | 23 | Kota | 2000.00 |
|
|
81
|
+
| 4 | Chaitali | 25 | Mumbai | 6500.00 |
|
|
82
|
+
| 5 | Hardik | 27 | Bhopal | 2125.00 |
|
|
83
|
+
| 6 | Komal | 22 | MP | 4500.00 |
|
|
84
|
+
| 7 | Muffy | 24 | Indore | 10000.00 |
|
|
85
|
+
+----+----------+-----+-----------+----------+
|
|
86
|
+
|
|
87
|
+
**_Subqueries with the `DELETE` Statement_**:
|
|
88
|
+
|
|
89
|
+
The subquery can be used in conjunction with the DELETE statement like with any other statements mentioned above.
|
|
90
|
+
|
|
91
|
+
**_Example_**:
|
|
92
|
+
|
|
93
|
+
Assuming, we have a CUSTOMERS_BKP table available which is a backup of the CUSTOMERS table. The following example deletes the records from the CUSTOMERS table for all the customers whose AGE is greater than or equal to 27.
|
|
94
|
+
```sql
|
|
95
|
+
DELETE FROM CUSTOMERS
|
|
96
|
+
WHERE AGE IN (
|
|
97
|
+
SELECT AGE
|
|
98
|
+
FROM CUSTOMERS_BKP
|
|
99
|
+
WHERE AGE >= 27
|
|
100
|
+
);
|
|
101
|
+
```
|
|
102
|
+
This would impact two rows and finally the CUSTOMERS table would have the following records.
|
|
103
|
+
|
|
104
|
+
+----+----------+-----+---------+----------+
|
|
105
|
+
| ID | NAME | AGE | ADDRESS | SALARY |
|
|
106
|
+
+----+----------+-----+---------+----------+
|
|
107
|
+
| 2 | Khilan | 25 | Delhi | 1500.00 |
|
|
108
|
+
| 3 | Kaushik | 23 | Kota | 2000.00 |
|
|
109
|
+
| 4 | Chaitali | 25 | Mumbai | 6500.00 |
|
|
110
|
+
| 6 | Komal | 22 | MP | 4500.00 |
|
|
111
|
+
| 7 | Muffy | 24 | Indore | 10000.00 |
|
|
112
|
+
+----+----------+-----+---------+----------+
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
# SQL - UNIONS CLAUSE
|
|
2
|
+
|
|
3
|
+
The SQL UNION clause/operator is used to combine the results of two or more SELECT statements without returning any duplicate rows.
|
|
4
|
+
|
|
5
|
+
- While using this UNION clause, each SELECT statement must have:
|
|
6
|
+
|
|
7
|
+
- The same number of columns selected
|
|
8
|
+
- The same number of column expressions
|
|
9
|
+
- The same data type and
|
|
10
|
+
- Have them in the same order
|
|
11
|
+
|
|
12
|
+
But they need not have to be in the same length.
|
|
13
|
+
|
|
14
|
+
_Example_
|
|
15
|
+
|
|
16
|
+
Consider the following two tables.
|
|
17
|
+
|
|
18
|
+
Table 1 − customers table is as follows:
|
|
19
|
+
|
|
20
|
+
+----+----------+-----+-----------+----------+
|
|
21
|
+
| id | name | age | address | salary |
|
|
22
|
+
+----+----------+-----+-----------+----------+
|
|
23
|
+
| 1 | Ramesh | 32 | Ahmedabad | 2000.00 |
|
|
24
|
+
| 2 | Khilan | 25 | Delhi | 1500.00 |
|
|
25
|
+
| 3 | kaushik | 23 | Kota | 2000.00 |
|
|
26
|
+
| 4 | Chaitali | 25 | Mumbai | 6500.00 |
|
|
27
|
+
| 5 | Hardik | 27 | Bhopal | 8500.00 |
|
|
28
|
+
| 6 | Komal | 22 | MP | 4500.00 |
|
|
29
|
+
| 7 | Muffy | 24 | Indore | 10000.00 |
|
|
30
|
+
+----+----------+-----+-----------+----------+
|
|
31
|
+
|
|
32
|
+
Table 2 − orders table is as follows:
|
|
33
|
+
|
|
34
|
+
+-----+---------------------+-------------+--------+
|
|
35
|
+
| oid | date | customer_id | amount |
|
|
36
|
+
+-----+---------------------+-------------+--------+
|
|
37
|
+
| 102 | 2009-10-08 00:00:00 | 3 | 3000 |
|
|
38
|
+
| 100 | 2009-10-08 00:00:00 | 3 | 1500 |
|
|
39
|
+
| 101 | 2009-11-20 00:00:00 | 2 | 1560 |
|
|
40
|
+
| 103 | 2008-05-20 00:00:00 | 4 | 2060 |
|
|
41
|
+
+-----+---------------------+-------------+--------+
|
|
42
|
+
|
|
43
|
+
Now, let us join these two tables in our SELECT statement as follows:
|
|
44
|
+
|
|
45
|
+
```sql
|
|
46
|
+
SELECT id, name, amount, date
|
|
47
|
+
FROM customer
|
|
48
|
+
LEFT JOIN orders
|
|
49
|
+
ON customers.id = orders.customer_id
|
|
50
|
+
UNION
|
|
51
|
+
SELECT id, name, amount, date
|
|
52
|
+
FROM customer
|
|
53
|
+
RIGHT JOIN orders
|
|
54
|
+
ON customers.id = orders.customer_id
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
This would produce the following result:
|
|
58
|
+
|
|
59
|
+
### The UNION ALL Clause
|
|
60
|
+
The UNION ALL operator is used to combine the results of two SELECT statements including duplicate rows.
|
|
61
|
+
|
|
62
|
+
The same rules that apply to the UNION clause will apply to the UNION ALL operator.
|
|
63
|
+
|
|
64
|
+
_Example_ -
|
|
65
|
+
Consider the following two tables:
|
|
66
|
+
|
|
67
|
+
* Table 1 − customers table is as follows:
|
|
68
|
+
|
|
69
|
+
+----+----------+-----+-----------+----------+
|
|
70
|
+
| id | name | age | address | salary |
|
|
71
|
+
+----+----------+-----+-----------+----------+
|
|
72
|
+
| 1 | Ramesh | 32 | Ahmedabad | 2000.00 |
|
|
73
|
+
| 2 | Khilan | 25 | Delhi | 1500.00 |
|
|
74
|
+
| 3 | kaushik | 23 | Kota | 2000.00 |
|
|
75
|
+
| 4 | Chaitali | 25 | Mumbai | 6500.00 |
|
|
76
|
+
| 5 | Hardik | 27 | Bhopal | 8500.00 |
|
|
77
|
+
| 6 | Komal | 22 | MP | 4500.00 |
|
|
78
|
+
| 7 | Muffy | 24 | Indore | 10000.00 |
|
|
79
|
+
+----+----------+-----+-----------+----------+
|
|
80
|
+
|
|
81
|
+
* Table 2 − orders table is as follows:
|
|
82
|
+
|
|
83
|
+
+-----+---------------------+-------------+--------+
|
|
84
|
+
| oid | date | customer_id | amount |
|
|
85
|
+
+-----+---------------------+-------------+--------+
|
|
86
|
+
| 102 | 2009-10-08 00:00:00 | 3 | 3000 |
|
|
87
|
+
| 100 | 2009-10-08 00:00:00 | 3 | 1500 |
|
|
88
|
+
| 101 | 2009-11-20 00:00:00 | 2 | 1560 |
|
|
89
|
+
| 103 | 2008-05-20 00:00:00 | 4 | 2060 |
|
|
90
|
+
+-----+---------------------+-------------+--------+
|
|
91
|
+
|
|
92
|
+
Now, let us join these two tables in our SELECT statement as follows :
|
|
93
|
+
```sql
|
|
94
|
+
SELECT id, name, amount, date
|
|
95
|
+
FROM customers
|
|
96
|
+
LEFT JOIN orders
|
|
97
|
+
ON customers.id = order.customer_id
|
|
98
|
+
UNION ALL
|
|
99
|
+
SELECT id, name, amount, date
|
|
100
|
+
FROM customers
|
|
101
|
+
RIGHT JOIN orders
|
|
102
|
+
ON customers.id = orders.customer_id;
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
This would produce the following result:
|
|
106
|
+
|
|
107
|
+
+------+----------+--------+---------------------+
|
|
108
|
+
| id | name | amount | date |
|
|
109
|
+
+------+----------+--------+---------------------+
|
|
110
|
+
| 1 | Ramesh | NULL | NULL |
|
|
111
|
+
| 2 | Khilan | 1560 | 2009-11-20 00:00:00 |
|
|
112
|
+
| 3 | kaushik | 3000 | 2009-10-08 00:00:00 |
|
|
113
|
+
| 3 | kaushik | 1500 | 2009-10-08 00:00:00 |
|
|
114
|
+
| 4 | Chaitali | 2060 | 2008-05-20 00:00:00 |
|
|
115
|
+
| 5 | Hardik | NULL | NULL |
|
|
116
|
+
| 6 | Komal | NULL | NULL |
|
|
117
|
+
| 7 | Muffy | NULL | NULL |
|
|
118
|
+
| 3 | kaushik | 3000 | 2009-10-08 00:00:00 |
|
|
119
|
+
| 3 | kaushik | 1500 | 2009-10-08 00:00:00 |
|
|
120
|
+
| 2 | Khilan | 1560 | 2009-11-20 00:00:00 |
|
|
121
|
+
| 4 | Chaitali | 2060 | 2008-05-20 00:00:00 |
|
|
122
|
+
+------+----------+--------+---------------------+
|
|
123
|
+
|
|
124
|
+
Note : **There are two other clauses (i.e., operators), which are like the UNION clause.**
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
# Relational Keys- Keys in a Relational Database
|
|
2
|
+
|
|
3
|
+
A database must be able to inhibit inconsistency occurring due to incorrect data. It must have certain identified attributes in relations to uniquely distinguish the tuples. No two tuples in a relation should have same value for all attributes since it will lead to duplicity of data. duplicity of data leads to inconsistency . Relational database systems have the concept of Relational Keys to distinguish between different records.
|
|
4
|
+
|
|
5
|
+
## Types of Relational Keys
|
|
6
|
+
### *Super Keys*
|
|
7
|
+
|
|
8
|
+
A relation’s tuples can be uniquely identified by various combinations of attributes. Super Keys is defined as a set of one attribute or combinations of two or more attributes that help in distinguishing between tuples in a relation.
|
|
9
|
+
|
|
10
|
+
For example, the Customer ID attribute of the relation Customer is unique for all customers. The Customer ID can be used to identify each customer tuple in the relation. Customer ID is a Super Key for relation Customer.
|
|
11
|
+
|
|
12
|
+
Customer Name attribute of Customer cannot be considered as Super Key because many customers for the organization can have same name. However when combined with Customer ID it becomes a Super Key {CustomerID, CustomerName}. It means that Super Key can have additional attributes. Consider any key K which is identified as a super key. Any superset of key K is also a super key. For example the possible Super Keys for Customer Relation are
|
|
13
|
+
|
|
14
|
+
- [ CustomerID, CustomerName, Customer Address ]
|
|
15
|
+
- [ CustomerID, CustomerName, Customer Contact Number ]
|
|
16
|
+
- [ CustomerID, Customer Contact Number ]
|
|
17
|
+
|
|
18
|
+
### *Candidate Keys*
|
|
19
|
+
|
|
20
|
+
If we take a key from the set of super keys for which we don’t have any proper subset defined as a superkey, it is called a candidate key. In other words the minimal attribute super keys are termed as candidate keys.
|
|
21
|
+
|
|
22
|
+
If we can identify some distinct sets of attributes which identify the tuples uniquely they fall in the category of candidate keys. For example the possible Candidate Keys for Customer Relation are
|
|
23
|
+
|
|
24
|
+
- [ CustomerID ]
|
|
25
|
+
- [ CustomerName, Customer Address ]
|
|
26
|
+
- [ CustomerName, Customer Contact Number ]
|
|
27
|
+
- [ Customer Address, Customer Contact Number ]
|
|
28
|
+
|
|
29
|
+
### *Primary Key*
|
|
30
|
+
|
|
31
|
+
Out of all possible candidate keys only one is chosen by the database designer as the key to identify the records in a relation in a database. This selected candidate key is called the Primary Key. It is the property of the relation and not of tuples. The primary key attribute(s) does not allow any duplicate values. It also inhibits leaving the primary key attribute without any value (NOT NULL).
|
|
32
|
+
|
|
33
|
+
**A relation can have only one primary key.**
|
|
34
|
+
|
|
35
|
+
In the Customer Database example {Customer ID} is the attribute taken as the primary key of customer relation. While picking up a candidate key as primary key the designer should ensure that it is an attribute or group of attributes that do not change or may change extremely rarely.
|
|
36
|
+
|
|
37
|
+
### *Alternate Keys*
|
|
38
|
+
|
|
39
|
+
After selecting one key among candidate keys as primary key, the rest of candidate keys are called the alternate keys. In the customer Database these candidate keys are the alternate keys.
|
|
40
|
+
|
|
41
|
+
- [ CustomerName, Customer Address ]
|
|
42
|
+
- [ CustomerName, Customer Contact Number ]
|
|
43
|
+
- [ Customer Address, Customer Contact Number ]
|
|
44
|
+
|
|
45
|
+
### *Foreign Key*
|
|
46
|
+
|
|
47
|
+
A foreign key is used to reference values from one relation into another relation. This is possible when the attribute or combination of attributes is primary key in the referenced relation. The relation in which the primary key of a relation is referenced is called the referencing table. The foreign key constraint implements the referential integrity in a database. The referencing relation attribute can have only those values which exist in the primary key attribute(s) of the referenced relation
|
|
48
|
+
|
|
49
|
+
**A relation can have multiple foreign key**
|
|
50
|
+
|
|
51
|
+
For example in the customer database the orders' relation (referencing relation) has the structure (Order ID, Customer ID, Order Date, Order Status, Total Billing Amount). The attribute Customer ID is the foreign key referencing Customer ID from customer relation (referenced relation). It means that orders can be placed only for the customers whose customer details are already available in the customer relation.
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
# Logical Operator Keywords
|
|
2
|
+
|
|
3
|
+
Here are the most important Logical Operators summarized in a table.
|
|
4
|
+
|
|
5
|
+
Logical Operators can be used for conditions as they show a result in form of a `boolean` (True/False) or Unknown.
|
|
6
|
+
So, e.g. if an exact value is `True` for a value, a Logical Operator can proof that it's True.
|
|
7
|
+
|
|
8
|
+
| Logical Operator | Explanation |
|
|
9
|
+
|------------------|-------------|
|
|
10
|
+
| ALL | If all comparisons are True: return True |
|
|
11
|
+
| ANY | If any comparison is True: return True |
|
|
12
|
+
| AND | If both expressions are True: return True |
|
|
13
|
+
| EXISTS | If a subquery contains rows: return True |
|
|
14
|
+
| IN | If compared value is equal to at least one value: return True |
|
|
15
|
+
| BETWEEN | If there are values in given range: return True |
|
|
16
|
+
| NOT | Reverses the value of any boolean |
|
|
17
|
+
| OR | If either expression is True: return True |
|
|
@@ -0,0 +1,184 @@
|
|
|
1
|
+
# `HAVING` Clause
|
|
2
|
+
|
|
3
|
+
Unlike where clause which imposes conditions on columns `Having` clause enables you to specify conditions that filter which group results appear in the results.
|
|
4
|
+
|
|
5
|
+
## Syntax
|
|
6
|
+
|
|
7
|
+
```sql
|
|
8
|
+
SELECT column_name(s)
|
|
9
|
+
FROM table_name
|
|
10
|
+
WHERE condition
|
|
11
|
+
GROUP BY column_name(s)
|
|
12
|
+
HAVING condition
|
|
13
|
+
ORDER BY column_name(s);
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
## Description
|
|
17
|
+
|
|
18
|
+
- Used with `aggregate functions`
|
|
19
|
+
- Must follow `GROUP BY` clause in the query
|
|
20
|
+
|
|
21
|
+
## Aggregate Functions
|
|
22
|
+
- SQL aggregation is the task of collecting a set of values to return a single value.
|
|
23
|
+
- An aggregate function is a function where the values of multiple rows are grouped together as input on certain criteria to form a single value of more significant meaning.
|
|
24
|
+
|
|
25
|
+
## Aggregate Functions Examples
|
|
26
|
+
|
|
27
|
+
Suppose this are the table given to us
|
|
28
|
+
|
|
29
|
+
|Students | table||
|
|
30
|
+
|--------|-----------|--------|
|
|
31
|
+
| rollno | name | class |
|
|
32
|
+
| 1 | Sanskriti | TE |
|
|
33
|
+
| 1 | Shree | BE |
|
|
34
|
+
| 2 | Harry | TE |
|
|
35
|
+
| 3 | John | TE |
|
|
36
|
+
| 3 | Shivani | TE |
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
|purchase | table||
|
|
40
|
+
|------------|-------|---------------|
|
|
41
|
+
| item | price | customer_name |
|
|
42
|
+
| Pen | 10 | Sanskriti |
|
|
43
|
+
| Bag | 1000 | Sanskriti |
|
|
44
|
+
| Vegetables | 500 | Sanskriti |
|
|
45
|
+
| Shoes | 5000 | Sanskriti |
|
|
46
|
+
| Water Bottle | 800 | XYZ |
|
|
47
|
+
| Mouse | 120 | ABC |
|
|
48
|
+
| Sun Glasses | 1350 | ABC |
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
### AVG function
|
|
52
|
+
|
|
53
|
+
Calculates `average` of the given column of values
|
|
54
|
+
|
|
55
|
+
```sql
|
|
56
|
+
SELECT AVG(price) AS Avg_Purchase, customer_name
|
|
57
|
+
FROM purchase
|
|
58
|
+
GROUP BY customer_name;
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
| Avg_Purchase | customer_name |
|
|
62
|
+
|--------------|---------------|
|
|
63
|
+
| 1627.5000 | Sanskriti |
|
|
64
|
+
|
|
65
|
+
### SUM function
|
|
66
|
+
|
|
67
|
+
Calculates `sum` of values of given column.
|
|
68
|
+
|
|
69
|
+
```sql
|
|
70
|
+
SELECT SUM(price) AS Total_Bill, customer_name
|
|
71
|
+
FROM purchase
|
|
72
|
+
GROUP BY customer_name;
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
| Total_Bill | customer_name |
|
|
76
|
+
|------------|---------------|
|
|
77
|
+
| 6510 | Sanskriti |
|
|
78
|
+
|
|
79
|
+
### COUNT function
|
|
80
|
+
|
|
81
|
+
Gives `count` of entries/ values in given column.
|
|
82
|
+
|
|
83
|
+
```sql
|
|
84
|
+
SELECT COUNT(item) AS Total_Items, customer_name
|
|
85
|
+
FROM purchase
|
|
86
|
+
GROUP BY customer_name;
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
| Total_Items | customer_name |
|
|
90
|
+
|-------------|---------------|
|
|
91
|
+
| 4 | Sanskriti |
|
|
92
|
+
|
|
93
|
+
### MAX function
|
|
94
|
+
|
|
95
|
+
Return `maximum` value from the number of values in the column.
|
|
96
|
+
|
|
97
|
+
```sql
|
|
98
|
+
SELECT MAX(price) AS Highest_Purchase, customer_name
|
|
99
|
+
FROM purchase
|
|
100
|
+
GROUP BY customer_name;
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
| Highest_Purchase | customer_name |
|
|
104
|
+
|-----------------|---------------|
|
|
105
|
+
| 5000 | Sanskriti |
|
|
106
|
+
|
|
107
|
+
### MIN function
|
|
108
|
+
|
|
109
|
+
Return `minimum` value from the number of values in the column.
|
|
110
|
+
|
|
111
|
+
```sql
|
|
112
|
+
SELECT MIN(price) AS Lowest_Purchase, customer_name
|
|
113
|
+
FROM purchase
|
|
114
|
+
GROUP BY customer_name;
|
|
115
|
+
```
|
|
116
|
+
| Lowest_Purchase | customer_name |
|
|
117
|
+
|-----------------|---------------|
|
|
118
|
+
| 10 | Sanskriti |
|
|
119
|
+
|
|
120
|
+
|
|
121
|
+
## Having clause Examples
|
|
122
|
+
|
|
123
|
+
### Example 1
|
|
124
|
+
|
|
125
|
+
```sql
|
|
126
|
+
SELECT COUNT(class) AS strength, class
|
|
127
|
+
FROM Students
|
|
128
|
+
GROUP BY class
|
|
129
|
+
HAVING COUNT(class) > 2;
|
|
130
|
+
```
|
|
131
|
+
|
|
132
|
+
Above query gives number of students in a class `having` number of students > 2
|
|
133
|
+
|
|
134
|
+
| strength | class |
|
|
135
|
+
|----------|-------|
|
|
136
|
+
| 4 | TE |
|
|
137
|
+
|
|
138
|
+
### Example 2
|
|
139
|
+
|
|
140
|
+
```sql
|
|
141
|
+
SELECT customer_name, MIN(price) AS MIN_PURCHASE
|
|
142
|
+
FROM purchase
|
|
143
|
+
GROUP BY customer_name
|
|
144
|
+
HAVING MIN(price) > 10;
|
|
145
|
+
```
|
|
146
|
+
Above query finds `minimum` price which is > 10
|
|
147
|
+
|
|
148
|
+
| customer_name | MIN_PURCHASE |
|
|
149
|
+
|---------------|------------|
|
|
150
|
+
| XYZ | 800 |
|
|
151
|
+
| ABC | 120 |
|
|
152
|
+
|
|
153
|
+
### Example 3
|
|
154
|
+
|
|
155
|
+
```sql
|
|
156
|
+
SELECT customer_name, AVG(price) AS Average_Purchase
|
|
157
|
+
FROM purchase
|
|
158
|
+
GROUP BY customer_name
|
|
159
|
+
HAVING AVG(price) > 550
|
|
160
|
+
ORDER BY customer_name DESC;
|
|
161
|
+
```
|
|
162
|
+
Above query calculates `average` of price and prints customer name and average price which is greater than 550 with descending `order` of customer names.
|
|
163
|
+
|
|
164
|
+
| customer_name | Average_Purchase |
|
|
165
|
+
|---------------|------------------|
|
|
166
|
+
| XYZ | 800.0000 |
|
|
167
|
+
| Sanskriti | 1627.5000 |
|
|
168
|
+
| ABC | 735.0000 |
|
|
169
|
+
|
|
170
|
+
### Example 4
|
|
171
|
+
|
|
172
|
+
```sql
|
|
173
|
+
SELECT customer_name, SUM(price) AS Total_Purchase
|
|
174
|
+
FROM purchase
|
|
175
|
+
WHERE customer_name
|
|
176
|
+
LIKE "S%"
|
|
177
|
+
GROUP BY customer_name
|
|
178
|
+
HAVING SUM(price) > 1000;
|
|
179
|
+
```
|
|
180
|
+
Calculates `SUM` of price and returns customer name and sum > 1000.
|
|
181
|
+
|
|
182
|
+
| customer_name | Total_Purchase |
|
|
183
|
+
|---------------|----------------|
|
|
184
|
+
| Sanskriti | 6510 |
|