To rename the columns(attributes)in a table the syntax is as follows:
syntax:
alter table table_name rename column current_column_name to new_column_name;
Example:
alter table student rename column stud_dept to dept_id;
Showing posts with label SQL. Show all posts
Showing posts with label SQL. Show all posts
Monday, January 24, 2011
Renaming a Table name
To rename the table name the syntax is as follows:
Syntax:
rename current_table_name to new_table_name;
Example:
rename students to student;
Syntax:
rename current_table_name to new_table_name;
Example:
rename students to student;
Monday, January 3, 2011
SQL SERVER – Definition, Comparison and Difference between HAVING and WHERE Clause
In recent interview sessions in hiring process I asked this question to every prospect who said they know basic SQL. Surprisingly, none answered me correct. They knew lots of things in details but not this simple one. One prospect said he does not know cause it is not on this Blog. Well, here we are with same topic online.
Answer in one line is : HAVING specifies a search condition for a group or an aggregate function used in SELECT statement.
HAVING can be used only with the SELECT statement. HAVING is typically used in a GROUP BY clause. When GROUP BY is not used, HAVING behaves like a WHERE clause.
A HAVING clause is like a WHERE clause, but applies only to groups as a whole, whereas the WHERE clause applies to individual rows. A query can contain both a WHERE clause and a HAVING clause. The WHERE clause is applied first to the individual rows in the tables . Only the rows that meet the conditions in the WHERE clause are grouped. The HAVING clause is then applied to the rows in the result set. Only the groups that meet the HAVING conditions appear in the query output. You can apply a HAVING clause only to columns that also appear in the GROUP BY clause or in an aggregate function. (Reference :BOL)
Example of HAVING and WHERE in one query:
SELECT titles.pub_id, AVG(titles.price)
FROM titles INNER JOIN publishers
ON titles.pub_id = publishers.pub_id
WHERE publishers.state = 'CA'
GROUP BY titles.pub_id
HAVING AVG(titles.price) > 10
Sometimes you can specify the same set of rows using either a WHERE clause or a HAVING clause. In such cases, one method is not more or less efficient than the other. The optimizer always automatically analyzes each statement you enter and selects an efficient means of executing it. It is best to use the syntax that most clearly describes the desired result. In general, that means eliminating undesired rows in earlier clauses.
Answer in one line is : HAVING specifies a search condition for a group or an aggregate function used in SELECT statement.
HAVING can be used only with the SELECT statement. HAVING is typically used in a GROUP BY clause. When GROUP BY is not used, HAVING behaves like a WHERE clause.
A HAVING clause is like a WHERE clause, but applies only to groups as a whole, whereas the WHERE clause applies to individual rows. A query can contain both a WHERE clause and a HAVING clause. The WHERE clause is applied first to the individual rows in the tables . Only the rows that meet the conditions in the WHERE clause are grouped. The HAVING clause is then applied to the rows in the result set. Only the groups that meet the HAVING conditions appear in the query output. You can apply a HAVING clause only to columns that also appear in the GROUP BY clause or in an aggregate function. (Reference :BOL)
Example of HAVING and WHERE in one query:
SELECT titles.pub_id, AVG(titles.price)
FROM titles INNER JOIN publishers
ON titles.pub_id = publishers.pub_id
WHERE publishers.state = 'CA'
GROUP BY titles.pub_id
HAVING AVG(titles.price) > 10
Sometimes you can specify the same set of rows using either a WHERE clause or a HAVING clause. In such cases, one method is not more or less efficient than the other. The optimizer always automatically analyzes each statement you enter and selects an efficient means of executing it. It is best to use the syntax that most clearly describes the desired result. In general, that means eliminating undesired rows in earlier clauses.
Wednesday, January 6, 2010
SQL Server Datatypes
Here the following are the list of datatypes used in SQL:
bigint
binary
bit
char
datetime
decimal
float
image
int
money
nchar
ntext
nvarchar
numeric
real
smalldatetime
smallint
smallmoney
sysname
text
timestamp
tinyint
varbinary
varchar
uniqueidentifier
bigint
binary
bit
char
datetime
decimal
float
image
int
money
nchar
ntext
nvarchar
numeric
real
smalldatetime
smallint
smallmoney
sysname
text
timestamp
tinyint
varbinary
varchar
uniqueidentifier
Altering column in a table
Syntax:
alter table table_name alter column column_name datatype(size),..n;
Here we alter datatype and size of the columns.
Example:
Alter table quest ALTER COLUMN quest_name char(100);
In this i have created a table named 'quest' with column as 'quest_name char(30)', so am in the situation to enter question name more than 30 characters so i change the size to 100.
alter table table_name alter column column_name datatype(size),..n;
Here we alter datatype and size of the columns.
Example:
Alter table quest ALTER COLUMN quest_name char(100);
In this i have created a table named 'quest' with column as 'quest_name char(30)', so am in the situation to enter question name more than 30 characters so i change the size to 100.
Selecting (display) data from a table
Syntax:
select * from table_name;
Example:
select * from dept;
This command selects and displays entire data from a table.
select * from table_name;
Example:
select * from dept;
This command selects and displays entire data from a table.
Inserting data into a table
Syntax:
insert into table_name values(column_value,column_value,column_value,..n);
The column which have datatype as:
char
varchar
nvarchar
text
are enclosed within single quotes(' ').
Example:
insert into sub values(1,'computer science','cs3yr');
insert into table_name values(column_value,column_value,column_value,..n);
The column which have datatype as:
char
varchar
nvarchar
text
are enclosed within single quotes(' ').
Example:
insert into sub values(1,'computer science','cs3yr');
Creating table in a database by refercencing foregin key
We can create foregin key by referencing primary key.
Before referencing a column the table must exist with that column name with same datatype and size.
Syntax:
create table table_name(column_name datatype(size),column_name1 datatype(size),column_name2 datatype(size) REFERENCES reference_table_name(column_name,..n);
Example:
create table dept(dept_id char(10) PRIMARY KEY,dept_name char(30),dept_year char(10));
create table sem(sem_id char(10) PRIMARY KEY,sem_name char(30),dept_id char(10) REFERENCES dept (dept_id));
In this example am creating a table name named 'dept' with primary key as 'dept_id', then i have created a table named 'sem' which has its column name as 'dept_id' references the table dept.
Before referencing a column the table must exist with that column name with same datatype and size.
Syntax:
create table table_name(column_name datatype(size),column_name1 datatype(size),column_name2 datatype(size) REFERENCES reference_table_name(column_name,..n);
Example:
create table dept(dept_id char(10) PRIMARY KEY,dept_name char(30),dept_year char(10));
create table sem(sem_id char(10) PRIMARY KEY,sem_name char(30),dept_id char(10) REFERENCES dept (dept_id));
In this example am creating a table name named 'dept' with primary key as 'dept_id', then i have created a table named 'sem' which has its column name as 'dept_id' references the table dept.
Creating table in a database with primary key
Syntax:
create table table_name(column_name datatype(size) PRIMARY KEY,column_name1 datatype(size),..n);
Example:
create table sem(sem_id int PRIMARY KEY,sem_name char(30));
create table table_name(column_name datatype(size) PRIMARY KEY,column_name1 datatype(size),..n);
Example:
create table sem(sem_id int PRIMARY KEY,sem_name char(30));
Creating table in a database
Syntax:
create table table_name(column_name datatype(size),column_name1 datatype(size),..n);
Size might be optional depends upon the datatype and column you are using in that table.
Example:
create table sem(sem_id int,sem_name char(30),dept_date date);
create table table_name(column_name datatype(size),column_name1 datatype(size),..n);
Size might be optional depends upon the datatype and column you are using in that table.
Example:
create table sem(sem_id int,sem_name char(30),dept_date date);
Creating a database in SQL Server
Syntax:
create database database_name;
Example:
create database project;
create database database_name;
Example:
create database project;
Subscribe to:
Posts (Atom)