Sql documentation
Installation
The installation and usage of MySQL can be done in many ways; one is to install WAMP/XAMPP server and using SQL through it another is to use an online sql emulator, the best way is to use a MySQL server.
Getting started with MySQL
All the operations in SQL can be classified into 3 types
DDL: Data definition language includes operation like create, alter, drop
DML: Data manipulation language includes operation like select, insert, update, delete
DCL: Data control language includes operations like commit, rollback, grant, revoke
Basic commands
- Showing all the databases
There are diffenet databases that resides inside your computer at any point of time. Using this command shows all the avalable databases on your local machine.
|
|
- Creating a database
To use or import and use a database, a database should always be created
|
|
- Using a database
To view/edit/modify table data the use keyword is used to enter a database.
|
|
- Showing all the tables within a table
There can be multiple table with a database.
|
|
- Showing all the data from a database
The * keyword is for all in MySQL, you can also use individual column names to show the data.
|
|
- Fully deleting a database drop database
There are two types of deletion in SQL one is delete which just deletes the values inside the table and not deletes the whole table, meanwhile using the drop keyword deletes the table along with the table structure.
|
|
- Fully deleting a table drop table \
|
|
Creating tables in MySQL
Tables can be created in MySQL by using the create command.
Note that using constraints and primary key is optional, but highly reccommended since they reduce the potential errors in the future.
Datatypes in MySQL
There are 3 major datatypes in MySQL namely string, numeric and datetime datatypes.
Srno | Name | Use | Type |
---|---|---|---|
1 | CHAR(SIZE) | Stores a fixed length charecter | string |
2 | VARCHAR(SIZE) | Stores a variable length charecter | string |
3 | TEXT(SIZE) | Stores text | string |
4 | INT | Stores integer | numeric |
5 | FLOAT(PRECISION) | Stores float | numeric |
6 | DATE | Stores date in YYYY/MM/DD format | datetime |
7 | DATETIME | Stores date and time | datetime |
8 | TIMESTAMP | Stores time | datetime |
Wildcards
Modifying tables
A SQL table can be modified by the use of the alter command.
- Adding a column
- Removing a column
- Renaming a column
- Changing column datatype
Updating and deleting values
- Command for deleting This deletes a column from the sql table
This deletes all the values but retains the table structure
|
|
College stuff
Problem statement
- Retrieve the details of all employees working in the company.
|
|
- Retrive the firstname, lastname, ssn, salary of all the employees.
|
|
- Display the details of all the employees whose SSN is H55.
|
|
Problem statement
- Find the sum of salaries of all the employees, the max sal, min sal and the avg sal
- Retrieve the total no of employes in the company.
|
|
- Retrieve the total no of employes in the research department
|
|
- Count the number of distinct salary values in the database
|
|
Updating SQL tables
Extra stuff
redis, sql vs nosql, graph database
- REDIS: Redis (remote dictionary server) is an in memory database highly optimized to store and serve data which needs to be retrived and modified faster, something like metadata of a server.
Relational algebra
These are the following signs used in relational algebra and the use of them.
- The Select operator: The select operator is used in horizontal partitioning i.e. showing all the rows and columns selected its symbol is $\sigma$. Select is commutative, although using two or more select statements is very seldom.
- The project operator: The project operator is used to show all rows selected however very rarely this includes all columns and selected columns need to be specified. Its symbol is $\pi$. Project is not commitative.
- Rename operation: The rename operation is denoted by the symbok $\rho$.
- Union operator: The union operator is denoted by the symbol $\cup$.
- Intersection operator: The intersection operator is denoted by the symbol $\cap$.
- Set difference operator: The set difference operator is denoted by the symbol -.
- Join operator: The join operator is denoted by the symbok $\bowtie$. The operator shown here is the natural jon, for outer join there are 3 types, namely left outer join, right outer join and full outer join. There also exists another function called as equijoin.
- Aggregate functions: The F symbol is used to display aggregate function.
- Division: Division can be used to divide the rows and columns where the argument or the data in column 1 matched with column 2.
Anomalies
Database anomalies There are 3 kinds of database anomalies, update anomaly, insert anomaly and delete anomaly. Changing of data using update may change the values for all the tuples which inlclude the same key. Same with insert with the databases being not assigned. Delete causing all the tuples related to a certain value being deleted.
- Ways to reduce this
- Minimize NULL values
- There should not be any spurious values (repeted values)
- Join relations using equijoin
- Partial Dependency: When an non-key attribute is determined by a part, but not the whole, of a COMPOSITE primary key.
- Transitive Dependency: When a non-key attribute determines another non-key attribute.
Normalization of data
Data and database creation may be a tedious task with multiple tables, multiple values and these stuff may bring out a error in the long term of the lifeycle of the program. This might also be suboptimal in the lifespan of the program. To resolve this we use normal forms and normalisation of data there are 4 forms of normalization that will be covered in this part and will follow ACID.
- 1NF: Atomicity refers to the deletion of multivalued attribute.
- 2NF: Complete dependency refers to the removal of any partial dependency of one column to other.
- 3NF: Transitive dependency refers to the removal of any transitive properties in which
- BCNF: BCNF states that the left side of any key must be candidate key or super key.
Transactions
- Singleuser
- Multiusers
- Concurrency
- Interleaved processing
- Parallel processing
What is a transaction?
Why is transaction recovery needed?
Type of transaction failure
- Computer failure
- Transaction failure
- Local errors
- Concurrency enforcement
- Disk failure
- Physical problem
Desirable properties of transaction
- Atomicity
- Consistency
- Isolation
- Durablity
Possible problems
- Lost update problem
- Remproary update problem
- Incorrect summary problem
2PL
- Growing phase
- Shrinking phase
Lock based protocols
- Shared lock
- Exclusive lock
Composite vs candiadte key vs super key
PL/SQL
PL/SQL stands for procedural language sql developed by oracle in the 1980s to give procedural generated sql code. This can be used in mysql in the following way.
However before writing the code the user must select the database on which the PLSQL should be run upon whether operating on that very database or working independently.
Code for generating fibonacci series in PLSQL
|
|
Note that when you are declaring variables the name of the variable comes first followed by the variable type. The delimiter only works if you are using mysql and if an older oracle database is being used then it is not needed.
Code to check whether a number is prime or not
|
|
Loops in PL/SQL
In MySQL, you can use loops to repeat a block of code multiple times until a certain condition is met. There are two main types of loops in MySQL: WHILE
loops and LOOP
loops.
WHILE Loops:
A
WHILE
loop repeats a block of code as long as a specified condition evaluates toTRUE
. Here’s the basic structure of aWHILE
loop:For example, you can use a
WHILE
loop to iterate over a set of rows in a table:In this example, the loop will continue executing as long as the
counter
variable is less than 10.LOOP Loops:
A
LOOP
loop is an unconditional loop that continues to execute until you explicitly use theLEAVE
statement to exit the loop. Here’s the basic structure of aLOOP
loop:You can use a
LOOP
loop when you need to perform an action at least once and then decide whether to continue or exit based on a condition.Here’s an example:
In this example, the loop will execute at least once and then exit when the
counter
variable is greater than or equal to 10.
Loops can be useful in MySQL for tasks such as iterating through result sets, implementing control structures, and performing repetitive calculations. However, it’s important to use them carefully to avoid infinite loops, which can lead to high resource consumption and system instability. Always ensure that your loop conditions will eventually evaluate to FALSE
to exit the loop.
Program for generating fibonacci serie