Step by step perform Sql Queries – Part I


Through-out the article we will learn step-by-step: How to perform Perform Sql Queries. We will discuss crud operations with usage of Sql server Queries. I have mention Queries for Some Realtime Basic Queries. This article is Part-I of a series of 'Step by step Perform Sql Queries'

Introduction


This article is Part-I of a series of 'Step by step Perform Sql Queries' In this article, we will discuss step-by-step implementation of sql queries using SQL Server and perform few CRUD operations.

So, what we are waiting for here, lets get started. Just following these step(s) and we will done!

Step1: How to Start Sql Server


Launch SQL Server Management studio from your system. If you do not have SQL Server, get one for this article you can install Sql Express edition.

Sql Connect


Step2:How to Connect Sql Server


From Management Studio Click on 'New Query' and open Sql Query window.

Sql Query Window


Step3:How to Create Database using Sql Query



In Design Steps To create a database
1. In Object Explorer, connect to an instance of the SQL Server Database
Engine and then expand that instance.
2. Right-click Databases, and then click New Database.
3. In New Database, enter a database name

Query
The following SQL statement creates a database called " DBTEST":


CREATE DATABASE DBTEST


Step4:How to Create Table using Sql Query




CREATE TABLE tblTest
(
Id INT PRIMARY KEY IDENTITY(1,1),
EmpNo INT,
EmpName VARCHAR(20),
amount DECIMAL(18,2),
Status BIT,
Dob Datetime
)


Code explanation



Primary Key must contain UNIQUE values. A primary key column cannot contain NULL values.
The table can have only ONE primary key.

Int number fields (Ex: 100,200,1500).

VARCHAR character and string value.

BIT boolean value (Ex: True or False).

DateTime DateTime values.

Step5: How to Create Insert Query




-- How to Insert the Records using Sql Query with snapshots

INSERT INTO tblTest(EmpNo,EmpName ,amount ,Status ,DOB) VALUES(100,'AAA',1859.25,1,'02/02/1984')
INSERT INTO tblTest(EmpNo,EmpName ,amount ,Status ,DOB) VALUES(101,'AAA1',1900.21,1,'02/02/1985')
INSERT INTO tblTest(EmpNo,EmpName ,amount ,Status ,DOB) VALUES(102,'AAA2',2559.24,1,'02/06/1987')
INSERT INTO tblTest(EmpNo,EmpName ,amount ,Status ,DOB) VALUES(103,'AAA3',1590.23,0,'02/08/1981')
INSERT INTO tblTest(EmpNo,EmpName ,amount ,Status ,DOB) VALUES(104,'AAA4',1592.22,1,'02/03/1986')
INSERT INTO tblTest(EmpNo,EmpName ,amount ,Status ,DOB) VALUES(105,'AAA5',1889.21,0,'02/01/1981')

-- How to Select the Records using Sql Query

SELECT * FROM tblTest




Insert & List the Records


Step6: How to use Select Query




-- Select the Records Based on EmpNo

SELECT * FROM Tbltest WHERE EmpNo=103

SELECT * FROM Tbltest WHERE EmpNo IN(101,103)

SELECT * FROM Tbltest WHERE EmpNo >100


Select Records


Step7: How to use Update Query




-- Update the Records Based on EmpNo

Update Tbltest SET Empno = 1947 WHERE EmpNo=103

Update Tbltest SET Empno=1 WHERE EmpNo IN(101,103)

Update Tbltest SET Empno=1001 WHERE EmpNo >104

SELECT * FROM tblTest


Update Query


Step8: How to use Update Query




-- Delete the Records Based on EmpNo

DELETE FROM Tbltest WHERE EmpNo=103

DELETE FROM Tbltest WHERE EmpNo IN(101,103)

SELECT * FROM tblTest


Delete Query


Step9: Perform alter Query in Sql Server




-- Alter New column add Table Fields

ALTER TABLE tblTest ADD RefId INT

UPDATE tblTest SET Refid=8 WHERE Empno=1001

SELECT * FROM tblTest


Alter Query Raise Query Window


MoveAlter Columnfield



Conclusion


In this whole article, we discussed about SQL Query operations using SQL Query Window.
1. Create a new Table
2. Choose Authentication in Sql server window
3. Write DML and DCL Queries
4. View using Query output
5. Add various operations

Hope, you find this article helpful.


Attachments

Comments

No responses found. Be the first to comment...


  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: