SQL Query-10

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 1

/* DEFAULT Constraint: Instead of inserting null values need to insert Default value then we

have to use DEFAULT constraint. Syntax: Create table (columname datatype


default(value),columname datatype ,....) */ create table [Default1](id int, name varchar(25)
default('xyz')) Insert into [Default1] values(1,'abc') Select * from [Default1] Insert into [Default1]
(id) values(2) /* CHECK:Based on the condition records are inserted or updated Syntax: create
table (columname datatype check(condition),columname datatype,....) */ create table [Check1]
(id int check(id>10),name varchar(20)) Insert into [Check1] values(12,'abc') Insert into [Check1]
values(5,'abc')--Error. Because ID column allows more than 10 Insert into [Check1]
values(10,'xyz')--Error. Because ID column allows more than 10 /* UNIQUE:It doesn't allow
duplicate values. But it will allow 1 NULL value Syntax: Create table (columnname datatype
UNIQUE,columnname datatype,...) */ Create table [UNIQUE](id int unique,name varchar(20))
Insert into [UNIQUE] values(1,'abc') select * from [UNIQUE] Insert into [UNIQUE]
values(1,'abc')--Error. Because duplicates are not allowed in ID column Insert into [UNIQUE]
(name) values('xyz') select * from [UNIQUE] /* PRIMARY KEY: Duplicate values are not allowed
and null values are also not allowed Syntax: Create table (columnname datatype primary
key,columnname datatype,.....) */ create table [PrimaryKey](id int primary key,name varchar(25))
Insert into [PrimaryKey] values(1,'abc') Insert into [PrimaryKey] values(1,'abc')--Error. Duplicates
are not allowed for primary key column i.e ID Insert into [PrimaryKey](name) values('xyz')--Error.
Nulls are not allowed in ID column /* FOREIGN KEY: Providing a relationship between the
datasets is called Foreign key. Foreign key constraint is created on 2 tables Syntax: Create table
(columnname datatype primary key,columnname datatype,....) Create table (columnname
datatype,columnname datatype,..foreign key references firsttablename(columnname)) */ Create
table [FirstTable1](id int primary key,name varchar(20)) create table [FirstTable2](name
varchar(20),id int foreign key references firsttable1(id)) Insert into [FirstTable1] values(1,'abc')
Insert into [FirstTable2] values('xyz',1) delete from [FirstTable1] where id=1--Error. Because the
same ID=1 record is available in "FirstTable2" table
/*=====================================Aggregation
Functions============================ Aggregation Functions: Aggregation functions
are returns a single value. Aggregation functions are SUM,MIN,MAX,AVG,COUNT SUM: It will
calculate the Total of any Numeric or Decimal columns Syntax: SUM(Columnname) */ select *
from Alldatatypes select sum(id) [total] from Alldatatypes /* MIN: It will calculate minimum value
for numeric or decimal or date columns Syntax: MIN(columnname) */ select * from Alldatatypes
select MIN(PeriodDate) [Min Date] from Alldatatypes /*MAX: It will calculate maximum value for
numeric or decimal or date columns Syntax: MAX(columnname) */ select * from Alldatatypes
select max(bonus) [Max Bonus]from Alldatatypes /* AVG: It will calculate Sum of values/No of
values. In Average it will allows Numeric or Decimal columns only Syntax: AVG(columnname) */
Select * from Alldatatypes select AVG(salary) [Average] from Alldatatypes /* COUNT: It will
calculate no of values in a column. In Count function will allows Numeric or Decimal or Date or
Character columns only Syntax: COUNT(columnname) */ select * from Alldatatypes select
count(*) from Alldatatypes--It will returns total no of records in a table Select Count(name) from
Alldatatypes--It will returns total no of records in a column select count(1) from Alldatatypes--It
will returns total no of records of the first column in a table

You might also like