Advertisement
ListonFermi

SQL Theory Notes

Mar 19th, 2024 (edited)
258
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.59 KB | Writing | 0 0
  1. Learn the Sql database and complete basics of database operations using terminal.
  2.  
  3. ACID properties.
  4. Normalization.
  5. Constrains.
  6. Relationships.
  7. Joins.
  8. 3-Schema architecture.
  9. Indexing.
  10. Aggregate functions.
  11. Scalar functions.
  12. SQL queries.
  13. Foreign key Primary key.
  14. Closure.
  15. Groupby.
  16. Having.
  17. Transactions.
  18. DML, DDL, DCL.
  19.  
  20. SQL:
  21. SQL- Structured Query Language is a domain specific used to manage data in RDBMS- Relational Database Management Systems.
  22. SQL Database:
  23. A SQL database server stores and organizes data in tables and rows.
  24. It's used for storing, retrieving, updating and deleting- All the CRUD operations of data.
  25. It's key concepts include:
  26. Normalization : For efficient data organisation.
  27. Primary key, secondary key: To establish relationships between tables.
  28.  
  29. ACID Properties:
  30. Transactions :
  31. Transactions are the set of tasks coming under one execution unit.
  32. If any tasks in between the begininning and ending task fails. The transaction fails.
  33. Therefore transaction has only 2 results: SUCCESS or FAILURE
  34. A db transaction should be atomic, consistent, isolated and durable
  35. Atomicity: Transaction result can be either fully succesful or completely unsuccessful
  36. Consistency:
  37. Isolated:
  38. Durablitiy:
  39.  
  40. Normalization:
  41. Normalization is a database technique to organize the database in a efficient manner by avoiding data redundancy.
  42.  
  43. Anamolies: Problems that occur in poorly planned, unnormalized databases where datas exist in single table.
  44. Insert Anamoly, delete anamoly
  45.  
  46. 1NF:
  47. * Making cells atomic-having single unit. (single value)
  48. *Each column should have unique value.
  49. 2NF:
  50. *Removing Partial dependencies
  51. *Each non-key attribute should dependent on the primary key and not to other columns.
  52. 3NF:
  53. *Removing transitive dependencies
  54.  
  55.  
  56. Constrains:
  57. Constrains in SQL are rules given to columns or tables.
  58. Uses:
  59. *To maintain data integrity : To enforce that only valid and consistent datas can be added to the table.
  60. *To establish relations (FOREIGN KEY (department_id) REFERENCES departments(id))
  61. *To avoid data redundancy in the required cases. (PRIMARY KEY, UNIQUE)
  62.  
  63. PRIMARY KEY (id),
  64. FOREIGN KEY (department_id) REFERENCES departments(id)
  65. UNIQUE (email)
  66. age INT NOT NULL
  67. CHECK (age >= 18)
  68.  
  69. OPERATORS:
  70. Arthmetic operators (+,-,/,*,%) : SELECT salary * 1.1 AS increased_salary FROM employees;
  71. Comparison operators (>,<,<>,!=,=) : SELECT * FROM products WHERE price > 100;
  72. Logical Operators (AND, OR, NOT) : SELECT * FROM employees WHERE department = 'IT' AND salary > 50000;
  73. Concatenation Operator (||): SELECT first_name || ' ' || last_name AS full_name FROM employees;
  74. LIKE Operator(Pattern matching): SELECT * FROM person WHERE firstname LIKE 'F%';
  75. IN Operator:
  76. BETWEEN Operator:
  77. IS NULL Operator:
  78. EXISTS Operator: Used to check for the existence of rows returned by a subquery.
  79. Returns TRUE if the subquery returns at least one row; otherwise, returns FALSE.
  80. Example: SELECT * FROM employees WHERE EXISTS (SELECT * FROM orders WHERE orders.employee_id = employees.id);
  81.  
  82.  
  83. 3-Schema architecture:
  84. It's a database design framework, it seperates the db into 3 levels: External, Conceptual and Internal Schema.
  85.  
  86.  
  87. Aggregate functions:
  88. Aggregate functions compute a single result from a set of input values.
  89. Values of multiple rows are grouped together as input to return a single value output.
  90. COUNT(), SUM(), MIN(), MAX(), AVG()
  91.  
  92. Scalar functions:
  93. Scalar functions compute a single result from a single input.
  94. UPPER(), LOWER(), MIDDLE()
  95.  
  96. DQL: Data Query Language
  97.  
  98. DDL : Data Definition Language
  99.  
  100. DCL : Data Control Language
  101.  
  102. DML : Data Manipulation Language
  103.  
  104. /*
  105. FROM CHATGPT:
  106.  
  107. Introduction to SQL:
  108.  
  109. SQL stands for Structured Query Language.
  110. It's a standard language for managing relational databases.
  111.  
  112. Data Manipulation:
  113. SQL allows you to manipulate data in various ways:
  114. SELECT: Retrieves data from a database.
  115. INSERT: Adds new data to a database.
  116. UPDATE: Modifies existing data in a database.
  117. DELETE: Removes data from a database.
  118.  
  119. Data Definition:
  120. SQL also allows you to define and modify the structure of databases and tables:
  121. CREATE DATABASE: Creates a new database.
  122. CREATE TABLE: Creates a new table within a database.
  123. ALTER TABLE: Modifies the structure of an existing table.
  124. DROP TABLE: Deletes a table from a database.
  125.  
  126. Querying Data:
  127. SQL queries are used to retrieve specific data from databases:
  128. SELECT statement is the primary tool for querying data.
  129. WHERE clause allows you to specify conditions for selecting data.
  130. GROUP BY clause groups rows based on a specific column.
  131. ORDER BY clause sorts the result set based on specified columns.
  132.  
  133. Joins:
  134. Joins are used to combine data from multiple tables:
  135. INNER JOIN: Returns records that have matching values in both tables.
  136. LEFT JOIN: Returns all records from the left table, and the matched records from the right table.
  137. RIGHT JOIN: Returns all records from the right table, and the matched records from the left table.
  138. FULL OUTER JOIN: Returns all records when there is a match in either left or right table.
  139. Data Integrity:
  140.  
  141. SQL ensures data integrity through various constraints:
  142. PRIMARY KEY: Uniquely identifies each record in a table.
  143. FOREIGN KEY: Establishes a link between two tables.
  144. UNIQUE: Ensures that all values in a column are unique.
  145. NOT NULL: Specifies that a column cannot contain NULL values.
  146. Indexing:
  147.  
  148. Indexes are used to optimize query performance:
  149. They allow for faster retrieval of data by creating pointers to rows in tables.
  150. Common types include single-column indexes and composite indexes.
  151. Transactions:
  152.  
  153. SQL transactions ensure data consistency and reliability:
  154. BEGIN TRANSACTION: Starts a new transaction.
  155. COMMIT: Saves changes made during the transaction to the database.
  156. ROLLBACK: Discards changes made during the transaction, reverting to the last committed state.
  157. Security:
  158.  
  159. SQL databases implement security measures to protect data:
  160. Authentication ensures that only authorized users can access the database.
  161. Authorization controls the level of access granted to users.
  162. Encryption secures data both at rest and in transit.
  163. Conclusion:
  164.  
  165. SQL is a powerful language for managing relational databases, offering a wide range of functionality for data manipulation, querying, and database administration.
  166. Mastering SQL basics is essential for effectively working with databases and extracting meaningful insights from data.
  167. */
  168.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement