Thursday, October 13, 2022

My SQl

 SQL:

SQL is an ANSI (American National Standards Institute) standard but there are many different versions of the SQL language.

What is SQL?

SQL is Structured Query Language, which is a computer language for storing, manipulating and retrieving data stored in relational database.

SQL is the standard language for Relation Database System. All relational database management systems like MySQL, MS Access, Oracle, Sybase, Informix, postgres and SQL Server use SQL as standard database language.

Also, they are using different dialects, such as:

  • MS SQL Server using T-SQL,

  • Oracle using PL/SQL,

  • MS Access version of SQL is called JET SQL (native format) etc.

Why SQL?

  • Allows users to access data in relational database management systems.

  • Allows users to describe the data.

  • Allows users to define the data in database and manipulate that data.

  • Allows to embed within other languages using SQL modules, libraries & pre-compilers.

  • Allows users to create and drop databases and tables.

  • Allows users to create view, stored procedure, functions in a database.

  • Allows users to set permissions on tables, procedures, and views

History:

  • 1970 -- Dr. Edgar F. "Ted" Codd of IBM is known as the father of relational databases. He described a relational model for databases.

  • 1974 -- Structured Query Language appeared.

  • 1978 -- IBM worked to develop Codd's ideas and released a product named System/R.

  • 1986 -- IBM developed the first prototype of relational database and standardized by ANSI. The first relational database was released by Relational Software and its later becoming Oracle.


SQL Process:

When you are executing an SQL command for any RDBMS, the system determines the best way to carry out your request and SQL engine figures out how to interpret the task.

There are various components included in the process. These components are Query Dispatcher, Optimization Engines, Classic Query Engine and SQL Query Engine, etc. Classic query engine handles all non-SQL queries but SQL query engine won't handle logical files.

Following is a simple diagram showing SQL Architecture:

SQL Architecture

SQL Commands:

The standard SQL commands to interact with relational databases are CREATE, SELECT, INSERT, UPDATE, DELETE and DROP. These commands can be classified into groups based on their nature:

DDL - Data Definition Language:

Command

Description

CREATE

Creates a new table, a view of a table, or other object in database

ALTER

Modifies an existing database object, such as a table.

DROP

Deletes an entire table, a view of a table or other object in the database.


DML - Data Manipulation Language:

Command

Description

SELECT

Retrieves certain records from one or more tables

INSERT

Creates a record

UPDATE

Modifies records

DELETE

Deletes records

DCL - Data Control Language:

Command

Description

GRANT

Gives a privilege to user

REVOKE

Takes back privileges granted from user



DQL - Data Quarry Language:

Command

Description

SELECT

Retrieves certain records from one or more tables 



What is RDBMS?

RDBMS stands for Relational Database Management System. RDBMS is the basis for SQL, and for all modern database systems like MS SQL Server, IBM DB2, Oracle, MySQL, and Microsoft Access.

A Relational database management system (RDBMS) is a database management system (DBMS) that is based on the relational model as introduced by E. F. Codd.

What is table?

The data in RDBMS is stored in database objects called tables. The table is a collection of related data entries and it consists of columns and rows.

Remember, a table is the most common and simplest form of data storage in a relational database. Following is the example of a CUSTOMERS table:

+----+----------+-----+-----------+----------+

| ID | NAME     | AGE | ADDRESS   | SALARY   |

+----+----------+-----+-----------+----------+

|1|Ramesh|32|Ahmedabad|2000.00|

|2|Khilan|25|Delhi|1500.00|

|3|kaushik|23|Kota|2000.00|

|4|Chaitali|25|Mumbai|6500.00|

|5|Hardik|27|Bhopal|8500.00|

|6|Komal|22| MP        |4500.00|

|7|Muffy|24|Indore|10000.00|

+----+----------+-----+-----------+----------+

What is field?

Every table is broken up into smaller entities called fields. The fields in the CUSTOMERS table consist of ID, NAME, AGE, ADDRESS and SALARY.

A field is a column in a table that is designed to maintain specific information about every record in the table.

What is record or row?

A record, also called a row of data, is each individual entry that exists in a table. For example there are 7 records in the above CUSTOMERS table. Following is a single row of data or record in the CUSTOMERS table:

+----+----------+-----+-----------+----------+

|1|Ramesh|32|Ahmedabad|2000.00|

+----+----------+-----+-----------+----------+

A record is a horizontal entity in a table.

What is column?

A column is a vertical entity in a table that contains all information associated with a specific field in a table.

For example, a column in the CUSTOMERS table is ADDRESS, which represents location description and would consist of the following:

+-----------+

| ADDRESS   |

+-----------+

|Ahmedabad|

|Delhi|

|Kota|

|Mumbai|

|Bhopal|

| MP        |

|Indore|

+----+------+

What is NULL value?

A NULL value in a table is a value in a field that appears to be blank, which means a field with a NULL value is a field with no value.

It is very important to understand that a NULL value is different than a zero value or a field that contains spaces. A field with a NULL value is one that has been left blank during record creation.

SQL Constraints:

Constraints are the rules enforced on data columns on table. These are used to limit the type of data that can go into a table. This ensures the accuracy and reliability of the data in the database.

Constraints could be column level or table level. Column level constraints are applied only to one column where as table level constraints are applied to the whole table.

Following are commonly used constraints available in SQL:

  • NOT NULL Constraint: Ensures that a column cannot have NULL value.

  • DEFAULT Constraint: Provides a default value for a column when none is specified.

  • UNIQUE Constraint: Ensures that all values in a column are different.

  • PRIMARY Key: Uniquely identified each rows/records in a database table.

  • FOREIGN Key: Uniquely identified a rows/records in any another database table.

  • CHECK Constraint: The CHECK constraint ensures that all values in a column satisfy certain conditions.

  • INDEX: Use to create and retrieve data from the database very quickly.

FOREIGN Key:

  • A foreign key is a key used to link two tables together. This is sometimes called a referencing key. 

  • Foreign Key is a column or a combination of columns whose values match a Primary Key in a different table. 

  • The relationship between 2 tables matches the Primary Key in one of the tables with a Foreign Key in the second table. 

  • If a table has a primary key defined on any field(s), then you can not have two records having the same value of that field(s).

Data Integrity:

The following categories of the data integrity exist with each RDBMS:

  • Entity Integrity: There are no duplicate rows in a table.

  • Domain Integrity: Enforces valid entries for a given column by restricting the type, the format, or the range of values.

  • Referential integrity: Rows cannot be deleted, which are used by other records.

  • User-Defined Integrity: Enforces some specific business rules that do not fall into entity, domain or referential integrity.

INDEX: 

  • The INDEX is used to create and retrieve data from the database very quickly. Index can be created by using single or group of columns in a table. When index is created, it is assigned a ROWID for each row before it sorts out the data. 

  • Proper indexes are good for performance in large databases, but you need to be careful while creating index. Selection of fields depends on what you are using in your SQL queries.


SQL SELECT Statement:

SELECT column1, column2....columnN 

FROM table_name; 

SQL DISTINCT Clause:

The SQL DISTINCT keyword is used in conjunction with SELECT statement to eliminate all the duplicate records and fetching only unique records. 

There may be a situation when you have multiple duplicate records in a table. While fetching such records, it makes more sense to fetch only unique records instead of fetching duplicate records. 

Syntax:

The basic syntax of DISTINCT keyword to eliminate duplicate records is as follows: 


SELECT DISTINCT column1, column2,.....columnN 

FROM table_name 

WHERE [condition]

SQL WHERE Clause:

The SQL WHERE clause is used to specify a condition while fetching the data from single table or joining with multiple tables. 

If the given condition is satisfied, then only it returns specific value from the table. You would use WHERE clause to filter the records and fetching only necessary records. 

The WHERE clause is not only used in SELECT statement, but it is also used in UPDATE, DELETE statement, etc., which we would examine in subsequent chapters.


SELECT column1, column2....columnN 

FROM table_name 

WHERE CONDITION; 

SQL AND/OR Clause:

SELECT column1, column2....columnN 

FROM table_name 

WHERE CONDITION-1 {AND|OR} CONDITION-2;


SQL IN Clause:

SELECT column1, column2....columnN 

FROM table_name 

WHERE column_name IN (val-1, val-2,...val-N); 

SQL BETWEEN Clause:

SELECT column1, column2....columnN 

FROM table_name 

WHERE column_name BETWEEN val-1 AND val-2; 

SQL LIKEClause:

SELECT column1, column2....columnN 

FROM table_name 

WHERE column_name LIKE { PATTERN }; 

SQL ORDER BY Clause:

SELECT column1, column2....columnN 

FROM table_name 

WHERE CONDITION 

ORDER BY column_name {ASC|DESC}; 

SQL GROUP BY Clause:

SELECT SUM(column_name) 

FROM table_name 

WHERE CONDITION 

GROUP BY column_name; 

SQL COUNT Clause:

SELECT COUNT(column_name) 

FROM table_name 

WHERE CONDITION; 

SQL HAVING Clause:

SELECT SUM(column_name) 

FROM table_name 

WHERE CONDITION 

GROUP BY column_name 

HAVING (arithematic function condition); 

SQL CREATE TABLE Statement:

CREATE TABLE is the keyword telling the database system what you want to do. In this case, you want to create a new table. The unique name or identifier for the table follows the CREATE TABLE statement.


CREATE TABLE table_name 


column1 datatype, 

column2 datatype, 

column3 datatype, 

..... 

columnN datatype, 

PRIMARY KEY( one or more columns ) 

); 

SQL DROP TABLE Statement:


 The SQL DROP TABLE statement is used to remove a table definition and all data, indexes, triggers, constraints, and permission specifications for that table. 

NOTE: You have to be careful while using this command because once a table is deleted then all the information available in the table would also be lost forever.


DROP TABLE table_name; 

SQL CREATE INDEX Statement:

CREATE UNIQUE INDEX index_name 

ON table_name ( column1, column2,...columnN); 

SQL DROP INDEX Statement:

ALTER TABLE table_name 

DROP INDEX index_name; 

SQL DESC Statement:

DESC table_name; 

SQL TRUNCATE TABLE Statement:

TRUNCATE TABLE table_name; 

SQL ALTER TABLE Statement:

ALTER TABLE table_name {ADD|DROP|MODIFY} column_name {data_ype}; 

SQLALTER TABLE Statement (Rename):

ALTER TABLE table_name RENAME TO new_table_name; 

SQL INSERT INTO Statement:


The SQL INSERT INTO Statement is used to add new rows of data to a table in the database.

INSERT INTO table_name( column1, column2....columnN) 

VALUES ( value1, value2....valueN); 

SQL UPDATE Statement:

UPDATE table_name TUTORIALS POINT Simply Easy Learning 


SET column1 = value1, column2 = value2....columnN=valueN 

[ WHERE CONDITION ]; 

SQL DELETE Statement:

DELETE FROM table_name 

WHERE {CONDITION}; 

SQL CREATE DATABASE Statement:

CREATE DATABASE database_name; 

SQL DROP DATABASE Statement:

DROP DATABASE database_name; 

SQL USE Statement:

USE DATABASE database_name; 

SQL COMMIT Statement:

COMMIT; 

SQL ROLLBACK Statement:

ROLLBACK;




SQL Comparison Operators:


Operator 

Description 

Example 

Checks if the values of two operands are equal or not, if yes then condition becomes true. 


(a = b) is not true. 

!= 

Checks if the values of two operands are equal or not, if values are not equal then condition becomes true. 


(a != b) is true. 

<> 

Checks if the values of two operands are equal or not, if values are not equal then condition becomes true. 


(a <> b) is true. 

Checks if the value of left operand is greater than the value of right operand, if yes then condition becomes true. 


(a > b) is not true. 

Checks if the value of left operand is less than the value of right operand, if yes then condition becomes true. 


(a < b) is true. 

>= 

Checks if the value of left operand is greater than or equal to the value of right operand, if yes then condition becomes true. 

(a >= b) is not true. 

Database Normalization

Database normalization is the process of efficiently organizing data in a database. There are two reasons of the normalization process:

  • Eliminating redundant data, for example, storing the same data in more than one tables.

  • Ensuring data dependencies make sense.

Both of these are worthy goals as they reduce the amount of space a database consumes and ensure that data is logically stored. Normalization consists of a series of guidelines that help guide you in creating a good database structure.

Normalization guidelines are divided into normal forms; think of form as the format or the way a database structure is laid out. The aim of normal forms is to organize the database structure so that it complies with the rules of first normal form, then second normal form, and finally third normal form.

It's your choice to take it further and go to fourth normal form, fifth normal form, and so on, but generally speaking, third normal form is enough.

There are many popular RDBMS available to work with. This tutorial gives a brief overview of few most popular RDBMS. This would help you to compare their basic features.

MySQL

MySQL is an open source SQL database, which is developed by Swedish company MySQL AB. MySQL is pronounced "my ess-que-ell," in contrast with SQL, pronounced "sequel."MySQL is supporting many different platforms including Microsoft Windows, the major Linux distributions, UNIX, and Mac OS X.MySQL has free and paid versions, depending on its usage (non-commercial/commercial) and features. MySQL comes with a very fast, multi-threaded, multi-user, and robust SQL database server.

JOINS

The SQL Joins clause is used to combine records from two or more tables in a database. A JOIN is a means for combining fields from two tables by using values common to each.


SQL Join Types:

There are different types of joins available in SQL: 

 INNER JOIN: returns rows when there is a match in both tables. 

 LEFT JOIN: returns all rows from the left table, even if there are no matches in the right table. 

 RIGHT JOIN: returns all rows from the right table, even if there are no matches in the left table. 

 FULL JOIN: returns rows when there is a match in one of the tables. 

 SELF JOIN: is used to join a table to itself as if the table were two tables, temporarily renaming at least one table in the SQL statement. 

 CARTESIAN JOIN: returns the Cartesian product of the sets of records from the two or more joined tables. 


INNER JOIN

The most frequently used and important of the joins is the INNER JOIN. They are also referred to as an EQUIJOIN. 

The INNER JOIN creates a new result table by combining column values of two tables (table1 and table2) based upon the join-predicate. The query compares each row of table1 with each row of table2 to find all pairs of rows which satisfy the join-predicate. When the join-predicate is satisfied, column values for each matched pair of rows of A and B are combined into a result row.


SELECT table1.column1, table2.column2... 

FROM table1 

INNER JOIN table2 

ON table1.common_filed = table2.common_field;


LEFT JOIN

The SQL LEFT JOIN returns all rows from the left table, even if there are no matches in the right table. This means that if the ON clause matches 0 (zero) records in right table, the join will still return a row in the result, but with NULL in each column from right table. 

This means that a left join returns all the values from the left table, plus matched values from the right table or NULL in case of no matching join predicate.

SELECT table1.column1, table2.column2... 

FROM table1 

LEFT JOIN table2 

ON table1.common_filed = table2.common_field;


RIGHT JOIN

The SQL RIGHT JOIN returns all rows from the right table, even if there are no matches in the left table. This means that if the ON clause matches 0 (zero) records in left table, the join will still return a row in the result, but with NULL in each column from left table. 

This means that a right join returns all the values from the right table, plus matched values from the left table or NULL in case of no matching join predicate.

SELECT table1.column1, table2.column2... 

FROM table1 

RIGHT JOIN table2 

ON table1.common_filed = table2.common_field;


FULL JOIN

The SQL FULL JOIN combines the results of both left and right outer joins. 

The joined table will contain all records from both tables, and fill in NULLs for missing matches on either side.

SELECT table1.column1, table2.column2... 

FROM table1 

FULL JOIN table2 

ON table1.common_filed = table2.common_field;



SELF JOIN

The SQL SELF JOIN is used to join a table to itself as if the table were two tables, temporarily renaming at least one table in the SQL statement.

SELECT a.column_name, b.column_name... 

FROM table1 a, table1 b 

WHERE a.common_filed = b.common_field;


CARTESIAN JOIN

The CARTESIAN JOIN or CROSS JOIN returns the cartesian product of the sets of records from the two or more joined tables. Thus, it equates to an inner join where the join-condition always evaluates to True or where the join-condition is absent from the statement.

SELECT table1.column1, table2.column2... 

FROM table1, table2 [, table3 ]


how to call api in angular

                            Api Call in service file Making API calls is a common task in Angular applications, and it can be achieved using...