Sitemap
Sumanr Bangera

Sumanr Bangera

A software engineer who is passionate about singing, blog writing, dancing, drawing, and yoga

SQL

--

SQL:

(3/10/2022)

This we start the SQL database. Database is a collection of tables, tables containing rows and columns. We use a SQL language in the database. Constraints are a set of rules. Constraints contain many types : Primary Key, Unique, Default, Check, Not null, Foreign Key.

Actually we learnt about this in the general training only. So we didn’t go into much detail about the basics. But Pallavi gives again a briefing of these topics with examples. In this we create a database then table. Next we insert some value to the table. Then we print that table using the Select statement.

DML(Data Manipulation language):

  • Insert → insert into Employee values(100, “IT”)
  • Update → update Employee set EAge=23 where EID=4
  • Delete → delete from Employee where EID=5

DQL(Data Query Language):

  • Selected column → select EName, EDesignation from Employee
  • Column alias(give a name to the column temporarily) → select EName as Name, EDesignation as Designation from Employee
  • Where → select *from Employee where EDeptNo = 104
  • relational/ logical → select * from Employee where EDeptNo > 102 and EAge > 23
  • Like → select * from Employee where EName like ‘S%’(Start letter S), ‘_e%’(Second letter is e), ‘___’(3 letter name), ‘[PS]%’(Name start with P and S), ‘[A-N]%’(A-N start letter), not like ‘[A-N]%’( other than A-N start words)
  • Distinct → select distinct EDesignation from Employee

DDL(Data Definition Language):

  • Create → create table demo
  • Alter → alter table demo add Dept varchar(10)
  • Update → update demo set Dept=100 where id< 3
  • Drop(it used to delete the column or table) → alter table demo drop column name (for the table deletion we use alter along with drop)…. drop table demo (it delete the table)

Order by:

We are able to order the rows of columns according to ascending or descending according to our need. But by default it is in ascending order.

  • For Single column: select * from Employee Order by EDeptNo desc (it will arrange the EDeptNo column in descending order)
  • For Multiple column: select * from Employee Order by EDeptNo, EAge (in this it will arrange the EDeptNo first to Ascending order then it will arrange the EAge)

Steps to push the code of SQL to the GitHub:

  1. Go to the saved SQL file folder, right click → git bash (it will open the command prompt)
  2. $ git init (create a local repository)
  3. $ git add .
  4. $ git commit -m ‘commit’
  5. $ git status
  6. In GitHub account create a repository manually (it is remote repository) give name to repository like TE_SQL, repository should be public and Readme.md file should be ticked the create. Open that file and copy paste that link in the command prompt.
  7. $ git remote add login origin (paste link).git
  8. $ git push origin master (it will create a repository and push the SQL code to GitHub)
  9. If we modify the code or we want to push another set of SQL code to GitHub then go for these 3 steps
    * $ git add .
    * $ git commit -m ‘commit’
    * $ git push origin master

Aggregate function:

These are the functions which help in the calculation purpose like sum, count, min, max, avg etc.
select avg(EAge) as ‘Average Age’ from Employee ( it calculates the average age of employees and displays it in the Average Age column).

Get Sumanr Bangera’s stories in your inbox

Join Medium for free to get updates from this writer.

Triggers: A trigger is a special type of stored procedure that automatically runs when an event occurs in the database server. We can observe the types in trigger
1. DDL triggers → CREATE,ALTER,DROP
2. DML triggers → INSERT,UPDATE,DELETE
3. CLR triggers → INSERT,UPDATE,DELETE
4. Logon triggers →Used in login form like just for admin login.
In this triggers we have two categories: Instead Of and After

Joins:

This Join clause combines rows from two or more tables based on a common column. Types of join clauses are inner, outer(left, right, full), self, cross.

  1. Inner: It retrieves the common set of values from both the tables.
  2. Outer: In this there are three types : left, right and full.

i) Left Join: All the rows from the left table and replace the non-matching values with NULL in the right table.

ii) Right Join: All the rows from the right table and replace the non-matching values with NULL in the left table.

iii) Full Join: All the rows from the right table AND left table and replace the non-matching values with NULL in the both tables.

3. Multiple Join: To show the joining of multiple tables.

4. Cross: The CROSS JOIN is used to generate a paired combination of each row of the first table with each row of the second table. This join type is also known as cartesian join.

Index:

An index is a schema object. It is used by the server to speed up the retrieval of rows by using a pointer. It can reduce disk I/O(input/output) by using a rapid path access method to locate data quickly. There are two types: clustered and non clustered.

Clustered index: Whenever a primary key is created automatically clustered index is created. Leaf node → The data which is stored in structured format. That particular table is known as a clustered table. Create a index for table →
create clustered index ind_TID on demo(TID)

Non Clustered: Whenever a unique key is created automatically a non clustered index is created. This data is stored in the unstructured format.

We can have only one clustered index for a table, if we want more than one index we go for a non clustered index. Using below command
create index ind_TName on demo(TName) [it create a non clustered index to the table demo of column TName]

Filtered index: We use an index like a filter using the where keyword.
create index ind_EDes on Employee(EDesignation) where EDesignation=’Trainee’ [it create a index called ind_EDes for the Employee table of EDesgination column which contains a Trainee as a designation]
select * from Employee where EDesignation=’Trainee’

Avoid creating too many indexes on a table. Create an index on only frequently used columns. If we right click in the query and go to the Display Estimated Execution Plan then we are able to see the working of the index.

Synonyms:

It is an alias/ alternative name for our table, view, procedure, function, trigger etc. Like giving another name to the table.
create synonym dep for Department
select * from dep
select * from Department [both select statements give the same answer].
To retrieve the details of synonyms we should type select * from sys.synonmys. From this command we get ID’s, type, created date etc.

View:

It is a virtual table which depends on another table. There are two types: 1. System view(SQL server views) 2. User-defined view(custom view). This custom view is further divided into two types i.e. simple and complex views.

Simple views: It affects the Single table.
create view Emp_View as select * from Employee where EDesignation = ‘Trainee’ [ it will create a view for the Trainee of the Employee table]
select * from Emp_view [retrieve the view]
If we insert a value to the view then it will reflect the changes in the original table. We can also update the values of view and we are able to delete the view also.

Complex view: It takes more than one table and it affects both the tables.

Thank You!!

--

--

Sumanr Bangera
Sumanr Bangera

Written by Sumanr Bangera

A software engineer who is passionate about singing, blog writing, dancing, drawing, and yoga