Sql Server Statements and their Syntax
The below content contains Sql Server Statement and their syntax with sample code. Through this one can easily learn sql server and can query easily. This is mainly for the beginners to sql server. It explains many operator its and the syntax of the operator.
Some Sql Server Commands and their Syntax:
AND\OR Operator:
SYNTAX:
SELECT column_name(s) FROM table_name WHERE condition AND|OR conditionALTER TABLE :
SYNTAX:
ALTER TABLE table_name ADD column_name datatype
or
ALTER TABLE table_name DROP COLUMN column_nameAS:
SYNTAX:
SELECT column_name AS column_alias FROM table_name
or
SELECT column_name FROM table_name AS table_aliasBETWEEN :
SYNTAX:
SELECT column_name(s) FROM table_name WHERE column_name BETWEEN value1 AND value2CREATE DATABASE:
SYNTAX: CREATE DATABASE database_name
DROP DATABASE:
SYNTAX:
DROP DATABASE database_nameCREATE TABLE:
SYNTAX:
CREATE TABLE table_name
(
column_name1 data_type,
column_name2 data_type,
...
)CREATE INDEX:
SYNTAX:
CREATE INDEX index_name ON table_name (column_name)
or
CREATE UNIQUE INDEX index_name ON table_name (column_name)DROP INDEX:
SYNTAX:
DROP INDEX table_name.index_name CREATE VIEW:
SYNTAX:
CREATE VIEW view_name AS
SELECT column_name(s) FROM table_name WHERE conditionDELETE:
SYNTAX:
DELETE FROM table_name WHERE some_column=some_value
or
DELETE FROM table_name GROUP BY:
SYNTAX:
SELECT column_name, aggregate_function(column_name)
FROM table_name WHERE column_name operator value
GROUP BY column_nameHAVING:
SYNTAX:
SELECT column_name, aggregate_function(column_name)
FROM table_name WHERE column_name operator value
GROUP BY column_name
HAVING aggregate_function(column_name) operator valueIN:
SYNTAX:
SELECT column_name(s) FROM table_name
WHERE column_name IN (value1,value2,..)INSERT INTO:
SYNTAX:
INSERT INTO table_name
VALUES (value1, value2, value3,....)
or
INSERT INTO table_name (column1, column2, column3,...)
VALUES (value1, value2, value3,....)AS:
SYNTAX:INNER JOIN:
SYNTAX:
SELECT column_name(s)
FROM table_name1 INNER
JOIN table_name2 ON table_name1.column_name=table_name2.column_nameLEFT JOIN:
SYNTAX:
SELECT column_name(s)
FROM table_name1
INNER JOIN table_name2 ON table_name1.column_name=table_name2.column_nameRIGHT JOIN:
SYNTAX:
SELECT column_name(s)
FROM table_name1
RIGHT JOIN table_name2 ON table_name1.column_name=table_name2.column_nameFULL JOIN:
SYNTAX:
SELECT column_name(s)
FROM table_name1
FULL JOIN table_name2 ON table_name1.column_name=table_name2.column_nameLIKE:
SYNTAX:
SELECT column_name(s)
FROM table_name WHERE column_name LIKE patternORDER BY:
SYNTAX:
SELECT column_name(s) FROM table_name
ORDER BY column_name [ASC|DESC]SELECT:
SYNTAX:
SELECT column_name(s) FROM table_nameSELECT * :
SYNTAX: SELECT * FROM table_name
SELECT DISTINCT:
SYNTAX:
SELECT DISTINCT column_name(s) FROM table_nameSELECT INTO:
SYNTAX:
SELECT * INTO new_table_name [IN externaldatabase]
FROM old_table_name
or
SELECT column_name(s) INTO new_table_name [IN externaldatabase]
FROM old_table_nameSELECT TOP:
SYNTAX:
SELECT TOP number column_name(s) FROM table_nameTRUNCATE TABLE:
SYNTAX:TRUNCATE TABLE table_name
UNION:
SYNTAX:
SELECT column_name(s) FROM table_name1
UNION
SELECT column_name(s) FROM table_name2UNION ALL:
SYNTAX:
SELECT column_name(s) FROM table_name1
UNION ALL
SELECT column_name(s) FROM table_name2UPDATE:
SYNTAX:
UPDATE table_name SET column1=value, column2=value,...
WHERE some_column=some_valueWHERE:
SYNTAX:
SELECT column_name(s) FROM table_name
WHERE column_name operator value
reply to Sql Server Statements and their Syntax"
DDL statements to:
Create Database
Create tables
Alter table definitions
Rename, Truncate, and drop tables
DML statements to:
Insert rows into a table
Update rows in a table
Delete rows from a table
TCL statements to:
COMMIT
CREATE TABLE Product ( product_id VARCHAR(5),
product_name VARCHAR(10),
product_rate DECIMAL)
FOREIGN KEY constraints:
create table k (id int primary key, ename varchar(10))
insert into k values(12,'a')
insert into k values(13,'d')
insert into k values(14,'t')
insert into k values(15,'r')
insert into k values(16,'w')
select * from k
create table fk (fid int references k(id), fname varchar(20))
insert into fk values(12,'a')
insert into fk values(13,'d')
insert into fk values(14,'t')
insert into fk values(15,'r')
insert into fk values(16,'w')
insert into fk values(18,'r')
insert into fk values(19,'y')
select * from fk
Op of fk