SQL Server Queries

 // Creating a new Database

CREATE DATABASE DummyDB


// Selecting OR entring in the Database

USE DummyDB


// Creating a table in our database

CREATE TABLE DummyTBL (

ID INT PRIMARY KEY IDENTITY, 

Name VarChar(50),

Address VarChar(100),

Age INT,

Gender VarChar(6)

);


//Seeing the table which we just have created

SELECT * FROM DummyTBL


//Inserting record in the table

INSERT INTO DummyTBL (Name, Address, Age, Gender) VALUES ('Yogender Joshi', 'Lakhnow', 45, 'Male')


//Updating record from the table

UPDATE DummyTBL SET Address='Agra' WHERE ID=2


//Deleting record from the table

DELETE FROM DummyTBL WHERE ID=11;


//Adding a column in the excisting table

ALTER TABLE DummyTBL ADD Mobile INT NOT NULL


//Deleting a column in any table

ALTER TABLE DummyTBL DROP COLUMN Mobile


//Rename the table DummyTBL to NewDummyTBL

SP_RENAME 'DummyTBL', 'NewDummyTBL'

SP_RENAME 'NewDummyTBL', 'DummyTBL'


//Rename the column Name to Full Name

SP_RENAME 'DummyTBL.Name','Full Name'

SP_RENAME 'DummyTBL.Full Name','Name'


//Changing the type of Column INT to VarChar(2)

ALTER TABLE DummyTBL ALTER COLUMN Age VarChar(2)

ALTER TABLE DummyTBL ALTER COLUMN Age INT


//Changing the name of Database - DummyDB to NewDummyDB

SP_RENAMEDB 'DummyDB','NewDummyDB'

SP_RENAMEDB 'NewDummyDB','DummyDB'


//Seeing selected columns only

Select Name, Age FROM DummyTBL


//Seeing the record using OR [Will show the result even if one condition is true]

Select * FROM DummyTBL WHERE ID=2 OR ID=22


//Seeing the record using AND [Will show the result only when all the conditions gets true]

Select * FROM DummyTBL WHERE ID=2 OR Name='Mohan'


//Seeing the record using IN

Select * FROM DummyTBL WHERE ID IN(2,4,6,8,10)


//Seeing the record using BETWEEN [In this case 2 & 8 will be included]

Select * FROM DummyTBL WHERE ID BETWEEN 2 AND 8


//More ways to see the record

Select * FROM DummyTBL WHERE ID <4

Select * FROM DummyTBL WHERE ID >4

Select * FROM DummyTBL WHERE ID <=4

Select * FROM DummyTBL WHERE ID >=4

Comments

Popular posts from this blog

String class in C# (Methods and properties)

Simple calculator (Console application/Java)