Pages

Follow us on Facebook

All basic interview questions asked in interviews.





Q: How to create table in sql server:

We will create here employee table:
 
create table tblEmployee(emp_id int ,emp_name varchar(50),emp_sal int)

press f5 it will execute above sql query and tblEmployee table will be created.
----------------------------------------------------------------------------------------------
How to insert records in sql table:

insert into tblEmployee values(1,'Satish',50000)

---------------------------------------------------------------------------------------------
How to select records in tblEmplyee

select * from tblEmployee
----------------------------------------------------------------------------------------------
How to create table in which employee id is incremented automatically,you dont need to insert it.

create table tblEmployee (emp_id int identity(1,1),emp_name varchar(50),emp_sal int)
----------------------------------------------------------------------------------------------
How to insert records in tblEmployee having emp_id as identity column that means auto increamented:

insert into tblEmployee('Satish',50000)
----------------------------------------------------------------------------------------------
In this example id is generaed automatically and increamented by 1 as you insert records.


Insert recods in some columns and not in all columns:

insert into tblEmployee (emp_name) values ( 'Satish' )
----------------------------------------------------------------------------------------------
Delete all records from tblEmployee

delete from tblEmployee
----------------------------------------------------------------------------------------------

Delete single record from tblEmployee with id 1:
 
delete from tblEmployee where emp_id=1

or delete from tblEmployee where emp_name='Satish'
----------------------------------------------------------------------------------------------
How to add new column in table:

alter table tblEmployee add department_id int
----------------------------------------------------------------------------------------------

How to delete column from table
 
alter table tblEmployee drop column department_id
----------------------------------------------------------------------------------------------

How to delete all records from table:

truncate table tblEmployee
---------------------------------------------------------------------------------------------- 
How to delete table from sql server:

drop table tblEmployee
----------------------------------------------------------------------------------------------
How to change any column datatype:

alter table tblEmployee alter column emp_name varchar(30)
---------------------------------------------------------------------------------------------- 
How to change table name:

sp_rename 'tblEmployee', 'tbl_Products'
----------------------------------------------------------------------------------------------
How to change column name:

sp_rename 'tblEmployee.emp_name','employeeName','column'
----------------------------------------------------------------------------------------------
How to add primary key in table:

For this action there must be unique records in that column

alter table tblEmployee add primary key(emp_id) 
----------------------------------------------------------------------------------------------
How to drop (remove) primary key from table:

alter table tblEmployee drop primary key
 ----------------------------------------------------------------------------------------------