Sie sind auf Seite 1von 6

Create Table Syntax

create table Sample_Company_info


(
Company_id int,
Company_name varchar(50),
loc varchar(50),
no_employee int
)
Select table Columns
select * from Sample_company_info
Insert Records into Table
insert into Sample_company_info
select 1,'ibm','hyderabad',500
union all
select 2,'wipro','chennai',700
union all
select 3,'accenture','mumbai',900
union all
select 4,'hexaware','kolkatta',600
union all
select 5,'virtusa','hyderabad',1000
union all
select 6,'deloitte','bangalore',1500
union all
select 7,'patni','pune',700
union all
select 8,'syntel','chennai',1000
union all
select 9,'accergent','bhuvaneswar',400
union all
select 10,'honeywell','hyderabad',700
UPDATE RECORDS
update sample_company_info
set no_employee=600 where company_name like'a%'
DELETE RECORDS
Delete from sample_company_info where company_name='zensar'
Constraints
column level-
not null,unique,check,primary,foreign,check,default
tables level-
unique,check,primary,foreign,check,default
column level constraint example:
create table Sample_Company_info1
(
Company_id int not null,
Company_name varchar(50) unique,
loc varchar(50),
no_employee int default 500
)

Table level constraint


create table Sample_Company_info123
(
Company_id int,
Company_name varchar(50) ,
loc varchar(50),
no_employee int default 500
constraint cn_pk primary key(company_name),constrint lc_uq unique(loc)
)
check constraint
create table bank_info
(bank_id int,
bank_name varchar(50),
customer_name varchar(50),
bal decimal(7,2)
constraint bal_ck check(bal>2000)
)
identity function:
for any column if we want to insert only unique val,ues then we can hand over th
e task to identity function.
colname<dtype>[width]identity[(seed,incr)]
create table college(college_id int identity,c_name varchar(20),loc varchar(10))
insert into college values(2'cbit','hyderabad')(display error)
insert into college values('cbit','hyderabad')(correct syntax)
ONDELETE CASCADE
delete the records in master table as well as referencing values in child table
Alter Command
-inc./dec. width of a column
-change datatype of column
-change null tonot null/not null to null
-add a new column to the table
-drop an existing column from the tabl,e
-add a constraint to a column of a table
-drop an existing constraint present on a column from the table
-to perform first 3 operations syntax is
alter table<tbl_name> alter column <col_name><data type>[width][null/not null]
add a new column to the table syntax
alter table<tbl_name> add column <col_name><data type>[width][constraint<consnam
e><constype>]
syntax to drop a column from the table
alter table <tablename> drop column<colname>
Syntax to add a constraint
alter table<tbl_name> add constraint <cnst_name><cnst_type>(collist)
Syntax to drop a constraint
alter table<tbl_name> drop constraint <cnst_name>
Drop Command
Truncate Command

Functions-(single row function,group function)


Single row function
-mathematical function
-string function
-date&time function
-system functuin
-mathematical function
ABS()
CEILING()
FLOOR()
LOG(N)
LOG10(N)
PI()
POWER(n,m)
ROUND(n,length[,function])
SIGN(n)
SQRT(n)
SQUARE(n)
-string functions (syntax: select function)
ASCII(S)
CHAR(N)
NCHAR(N)
CHARINDEX('O','HELLOWORLD')
LEFT(S,N)
RIGHT(S,N)
SUBSTRING(S,start,length)
LEN(S)
LOWEAR(S)
UPPAER(S)
LTRIM(S)
RTRIM(S)
REPLACE(S1,S2,S3)---REPLACE('HELLO','L','X')....HEXXO
REPLICATE(S,N)---REPLICATE('HEL',2)----HELHEL
REVERSE()
SOUNDEX(S)
DIFFERENCE(S1,S2)
SPACE(N)
-date and time function
GETDATE()
DAY(DATE)
MONTH(DATE)
YEAR(DATE)
DATENAME(DATEPART,DATE)
DATEPART(DATEPART,DATE)
DATEDIFF(DATEPART<STARTDATE,ENDDATE)
GETUTCDATE()
-conversion functions
CAST()
CONVERT().....select convert(varchar(50),getdate())
ISNUMERIC()
ISDATE()
ISNULL(V1,V2)
COALESCE(v1,v2,v3..vn)...>returns first not null expression from given list.it i
s similar to isnull().but COALESCE() take multiple values.
DATALENGTH()
HOST_NAME()
IDENT_CURRENT('tablename')
IDENT_SEED('tablename)
IDENT_INCR('tablename')
NULLIF(v1,v2)....>return first expression if the two expressions are not equal.i
f both the values are equal, returns null value
ROWCOUNT_BIG()...>returns no. of rows effected when the last statement executed
. select * from emp; select rowcount_big from emp
CASE
when condition then result expression
when condition then result expression
........
.......
else else_result_expression
end
SET OPERATORS
Count(expression)......>returns INT datatype values
Count_Big(expression).....>returns count of BIGINT datatype values.
SUM(expression)
AVG(expression)
MAX(expression)
MIN(expression)
STDDEV(expression)
VAR(expression)

OPERATORS
1.Arithmatic operators
2.assignment
3.logical
4.comparision
5.concatenation
1.Arithmatic operators:
+,-,*,/,%(mod)
2.assignment
=
3.comparision
>,<,<=,>=,=,!=,<>,!<,!>
4.logical
AND,OR,NOT,ALL,ANY,BETWEEN,EXISTS,IN,LIKE,NOT,OR,SOME

SET OPERATORS
UNION
UNION ALL
INTERSECT
MINUS

CLAUSES
-WHERE
-GROUP BY
-HAVING
-ORDER BY
SUB QUERY
JOINS
-EQUI JOIN
-NON EQUI JOIN
-SELF JOIN
-CARTESIAN JOIN
-OUTER JOIN
TRANSACTION
-commit
-rollback
-savepoint
creating a table from an existing table
-select * into <new_table> from <existing_table>
copying of datta from existing table to another table
-insert into <table>[collist] select <*/<col_list> from<exist_tbl>[Conditions
]
VIEWS

Das könnte Ihnen auch gefallen