Tuesday, 25 June 2013

SQL Server Basic Queries

--copy all data from another table and the format of the table(columns)
select * into new_table from old_table

--delete all rows in a table
truncate table table_name

--delete table from the db
drop table table_name

--copy onlyy the format of the table(columns) and its data types
select top 0 * into new_table from old_table

--to create a new table
create table persons
(
personid int,
lastname varchar(255),
firstname varchar(255),
address varchar(255),
city varchar(255)
);

--to insert a row or value in a table
insert into persons (personid, lastname, firstname, address, city,)
values (1,'tom b. erichsen','skagen ‘,'stavanger 4006 st','norway');

--to update already existing row or value in a table
update persons set lastname ='alfred schmidt', city='london' where personid = 1

--to delete a particular row in a table (use where condition)
delete from persons where where personid = 1and city ='norway'

--to delete all row in a table (use where condition)
delete from persons
or

delete * from persons

No comments:

Post a Comment