SQL
SQL
SQL Commands:
Drop:
It is a Data Definition Language Command (DDL). It is used to drop the whole table.
With the help of the “DROP” command we can drop (delete) the whole structure in one
go i.e.
it removes the named elements of the schema. By using this command the existence of
the whole table is finished or say lost.
Delete:
Basically, it is a Data Manipulation Language Command (DML). It is used to delete
one or more tuples of a table.
With the help of the “DELETE” command, we can either delete all the rows in one go
or can delete rows one by one. i.e.,
we can use it as per the requirement or the condition using the Where clause. It is
comparatively slower than the TRUNCATE command.
Truncate:
It is also a Data Definition Language Command (DDL). It is used to delete all the
rows of a relation (table) in one go.
With the help of the “TRUNCATE” command, we can’t delete the single row as here
WHERE clause is not used. By using this command the existence of all the rows of
the table is lost.
It is comparatively faster than the delete command as it deletes all the rows
fastly. The TRUNCATE command does not remove the structure of the table.
1. DB Engine
2. Analysis Services
3. Reporting Services
4. Integration Services
5. Mobile edition
alter:
Identity:
syntax: identity (seed value, increment value) - identity (1,1) (id int
identity(1,1))
Logical operators:
IS Operator:
It is used to get the records while comparing with null
LIKE: There are used to get the records based on required order
To work with like operator we use wild cards. (%, _,_ _, [a-m])
select * from employee where name like '%s'
Set operators: this operators will return the results of 2 select operators.
delete:
Truncate:
It is used to delete all the records using trancate in a table, we can delete one
by one record because it's not support where condition.
Truncate will reset identity in a table.
constraints:
1. unique - It will return unique values and 1 null value, because 1 null value
will treated as unique.
2. not null - it will not except the null values but it allows duplicate values.
3. Primary key - It will not accept null and duplicate values
4. check - It is special type of condition we can apply on any coulmn of table.
create table emp (id int, name varchar, age int check(age between 18 and 70);
5. default - This constrain accept default values when user not supplied column
values.
Though the column is having default constrain when we supply the value, the
supplied value will be stored.
6. Foreign key
Math functions:
abs(-23)
ceiling(25.75)
floor (34.5) -remove decimal value
square (4)
sqrt(49)
power()
String functions:
len()
ltrim()
rtrim()
lower()
upper()
ascii()
revers()
space()
concat()
concat_ws()
FORMAT()
Format("0.981", "Percent");
LOCATE(): This function is used to find the nth position of the given word in a
string.
Syntax: SELECT LOCATE('for', 'geeksforgeeks', 1);
LOWER(): This function is used to convert the upper case string into lower case.
REPEAT()
REPLACE():
REVERSE()
SPACE()
Upper()
multi-row functions:
1. max()
2. min()
3. avg()
4. sum()
5. count()
6. day(getdate())
7. month (getdate())
8. year(getdate())
select right('Sairam',3)
select left('Sairam',3)
delete from details where id not in (select max(id) from details group by column
names)
or
with details_DEL As
(
select *, row_number() over(partition by EMPNAME order by EMPNAME) as rownumber
from details
)
select * from details_DEL
*************************************
with details_DEL As
(
select *, row_number() over(partition by EMPNAME order by EMPNAME) as rownumber
from details
)
delete from details_DEL where rownumber>1
declare @dt datetime = (select order_date from orders where prod_id =103) select
convert(varchar(20), @dt, 0) String
For example : If you have 2 rows at rank 1 and you have 5 rows in total.
RANK() returns - 1, 1, 3, 4, 5
DENSE_RANK returns - 1, 1, 2, 3, 4
with Result as
(
select salary, dense_rank() over(order by salary desc) as TopSalary from employee
)
select top 1 salary from Result where TopSalary = 5
Joins:
A view contains rows and columns, just like a real table. The fields in a view are
fields from one or more real tables in the database.
You can add SQL statements and functions to a view and present the data as if the
data were coming from one single table.
SP:
A stored procedure is a prepared SQL code that you can save, so the code can be
reused over and over again.
So if you have an SQL query that you write over and over again, save it as a stored
procedure, and then just call it to execute it.
You can also pass parameters to a stored procedure, so that the stored procedure
can act based on the parameter value(s) that is passed.
EXEC procedure_name;
Indexes:
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.
An index helps to speed up select queries and where clauses, but it slows down data
input, with the update and the insert statements.
Indexes can be created or dropped with no effect on the data. In this article, we
will see how to create, delete, and uses the INDEX in the database.
1. cluster index:
It will create automatically when table is having primary key constrain
in sql server a table shold have only one cluster index
Triggers:
DDL triggers run in response to a variety of data definition language (DDL) events.
These events primarily correspond to Transact-SQL CREATE, ALTER, and DROP
statements, and
certain system stored procedures that perform DDL-like operations.
Logon triggers fire in response to the LOGON event that's raised when a user's
session is being established.
You can create triggers directly from Transact-SQL statements or from methods of
assemblies that are created in the Microsoft .NET Framework common language runtime
(CLR)
and uploaded to an instance of SQL Server. SQL Server lets you create multiple
triggers for any specific statement.
Schema:
Transactions:
Transactions group a set of tasks into a single execution unit. Each transaction
begins with a specific task and ends when all the tasks in the group successfully
complete.
If any of the tasks fail, the transaction fails. Therefore, a transaction has only
two results: success or failure.
Deadlock:
Deadlocks occur when two processes want to access resources that are mutually being
locked by each other. This locked situation can continue forever if nobody stops
it.
Cursors:
Cursor is a Temporary Memory or Temporary Work Station. It is Allocated by Database
Server at the Time of Performing DML(Data Manipulation Language)
operations on Table by User. Cursors are used to store Database Tables. There are 2
types of Cursors: Implicit Cursors, and Explicit Cursors.
These are explained as following below.
Implicit Cursors:
Implicit Cursors are also known as Default Cursors of SQL SERVER. These Cursors are
allocated by SQL SERVER when the user performs DML operations.
Explicit Cursors :
Explicit Cursors are Created by Users whenever the user requires them. Explicit
Cursors are used for Fetching data from Table in Row-By-Row Manner.
performance tuning: