Quantcast
Viewing all articles
Browse latest Browse all 3160

CRUD operations in SQL Server

CRUD operations are foundation operations every database developer and administrator needs to understand. Let’s take a look at how they work with this guide.

Introduction

According to Wikipedia …

“In computer programming ,create, read, update, and delete(CRUD) are the four basic functions of persistent storage .Alternate words are sometimes used when defining the four basic functions ofCRUD, such asretrieveinstead ofread,modifyinstead ofupdate, ordestroyinstead ofdelete.CRUDis also sometimes used to describe user interface conventions that facilitate viewing, searching, and changing information ; often using computer-based forms and reports . The term was likely first popularized by James Martin in his 1983 bookmanaging the Data-base Environment. The acronym may be extended to CRUDL to coverlistingof large data sets which bring additional complexity such as pagination when the data sets are too large to hold easily in memory.”

CRUD is an acronym that stands for C reate, R ead, U pdate, and D elete.

These are the four most basic operationsthat can be performed with most traditional database systems and they are the backbone for interacting with any database.

Getting started

Let’s get started to understand the concepts of CRUD operations in SQL Server


Image may be NSFW.
Clik here to view.
CRUD operations in SQL Server

Create

The first letter of CRUD in CRUD operations, ‘C’, refers to CREATE aka add, insert. In this operation, it is expected to insert a new record using theSQL insert statement. SQL uses INSERT INTO statement to create new records within the table.

Let us create a simple table named Demo for this example.

USE AdventureWorks2016; GO DROP TABLE IF EXISTS Demo; CREATE TABLE dbo.Demo (id INT, name VARCHAR(100) );

SQL Insert starts with the keyword INSERT INTO then specify the table name and the columns that we want to insert. The columns go inside of the parentheses and then we specify a VALUES clause.

INSERT INTO< tablename>

(column1,column2,….)

VALUES (value1,value2,….)

We put in the table name demo after the insert into command. Now, supply the values to the listed columns id and name in the VALUES clause.

INSERT INTO dbo.Demo VALUES (1, 'Prashanth' );

To insert multiple rows, follow the below syntax

INSERT INTO< tablename>

(column1,column2,….)

VALUES(value1,value2,…. ),( value1,value2,…. ), (value1,value2,…. )…

In the following example, the multiple values are listed within in the parenthesis and each list is separated by a comma delimiter

INSERT INTO dbo.Demo (id, name ) VALUES (2, 'Jayaram' ), (3, 'Pravitha' );

To insert rows from SQL Union clause, follow the below syntax

INSERT INTO< tablename> (column1,column2,….)

SELECT value1,value2,… UNION ALL

SELECT value1, value2,…

In the following example, the multiple values are listed using SELECT statement and then these values combined and fed to the table using SQL UNION ALL set operator.

INSERT INTO dbo.demo SELECT 4, 'Prarthana' UNION ALL SELECT 5, 'Ambika';

The output lists all the inserted rows from the above samples.


Image may be NSFW.
Clik here to view.
CRUD operations in SQL Server

Notes: It is mandatory to insert at least all of the required columns, but you don’t have to update a column if those values are not required, or if there is a default value for that column A detailed explanation of SQL insert can be found in the following article: Overview of the SQL Insert statement SQL Insert statement only works against a single table unlike select which can work against multiple tables A detailed explanation of SQL Union clause can be found in the following article: SQL Union overview, usage and examples Read

The second letter of CRUD in CRUD operations, ‘R’, refers to SELECT (data retrieval) operation. The word ‘read’ retrieves data or record-set from a listed table(s).SQL uses the SELECT command to retrieve the data. When it comes to executing queries, you can use SQL Server Management Studio or SQL Server Data Tools or sqlcmd, based on your preference.

For example, to read related data from the specified table, refer to the below syntax.

SELECT * FROM< TableName>

The SQL select statement allows you to query the tables. It allows you to retrieve specific data, one or more rows from one or more tables.

The SQL SELECT statement in a vast majority of the time going to contain names of columns from the table(s) that you would like to get data from. Once you have column names, the table name is required in the FROM clause. Now, in a SELECT list, after every column of data, you’re going to need a comma. So you separate each column with a comma, except, no comma after the last column. We’re going to have the SELECT keyword, column name followed by a comma, column name, and the last column name, no comma, FROM clause followed by table name.

In this case, one that will return every row in the Address table. And it will return just the AddressID, AddressLine1,AddressLine2,City, StateProvinceID and PostalCode columns.


Image may be NSFW.
Clik here to view.
CRUD operations in SQL Server

The SQL SELECT statement uses a wildcard character (*) or asterisk to populate all the columns of the table(s). It provides a way to not have to list every column table(s). That’s by using the asterisk or ‘*’.

The output lists all the columns of the Address table. The following SQL going to give me all of the columns

USE [AdventureWorks2016]; GO SELECT * FROM [Person].[Address];
Image may be NSFW.
Clik here to view.
CRUD operations in SQL Server

Next, the FROM Clause is going to have at least a table or it is possible to have multiple tables usingSQL Join.

In the following example, the Product and SalesOrderDetail tables are listed in the FROM Clause of the Select statement.

SELECT * FROM Production.Product AS p JOIN Sales.SalesOrderDetail AS s ON s.ProductID = p.ProductID;
Image may be NSFW.
Clik here to view.
CRUD operations in SQL Server

Notes: A detailed explanation of a few more SQL SELECT statement scenarios is discussed in the article: Overview of the SQL Order by clause Update

The third letter of CRUD in CRUD operations, ‘U’, refers to Update operation. Using the Update keyword, SQL brings a change to an existing record(s) of the table.

You can refer to the article

Viewing all articles
Browse latest Browse all 3160

Trending Articles