NOTES

Thursday 30 June 2011

SQL DATA

Find Top Value In a Table Column

create table ctr
(
num  int
 )

insert into ctr values(1)
insert into ctr values(2)
insert into ctr values(3)
insert into ctr values(4)
insert into ctr values(5)
insert into ctr values(33)
insert into ctr valuse(63)

select top 1 num from(select top 3 num from ctr  order by num desc) ctr order by num desc

OutPut Is 63

Add New Column In the Table

alter table table_name add column_name datatypewithsize


Mdify Column In the Table

alter table table_name alter  column column_name datatypewithsize


Create Sub Table Use The An Other Table

select columns_names into new_Table_Name from Old_Table_Name


Create Table Using Constraints


create table emp
(
id int primary key,
name varchar(10) null,
city varchar(10) check(city in('ORAI','BHOPAL'))
)

create table dept
(
d_id int primary key,
e_id int references emp(id),
d_name varchar(30) not null
)

If You Want To Add Constraint After Create Table



create table emp
(
id int ,
name varchar(10),
city varchar(10) )

create table dept
(
d_id int,
e_id int,
d_name varchar(30)
)

Follow Some Point :::

1- If we Want to add the primary key in the column than first add the not null constraint  in the table tha after add  the primary key.....

Syntax is :::
              alter table emp1 alter column  id  int  not null
              alter table emp1 add constraint  i  primary key(id)i ==  Is Constraint Name


2-Add the foreign key in the table than follow this

Syntax :::
      alter table dept1 add constraint ii  foreign key(e_id)  references emp1(id)

ii ==  Is Constraint Name       

3- add the check consteaint
    table emp1 add constraint iiii check(city in('orai','kanpur'))
iiii ==  Is Constraint Name       

Some Other Quries  


create datebase database_name
use database database_name

drop table table_name
truncae table table_name
delete from  table_name

drop command drop the stracture but truncate command delete the table data


alter

Labels:

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home