Table operations in MS SQL Server
Last Updated :
17 Sep, 2020
In a relational database, the data is stored in the form of tables and each table is referred to as a relation. A table can store a maximum of 1000 rows. Tables are a preferred choice as:
- Tables are arranged in an organized manner.
- We can segregate the data according to our preferences in the form of rows and columns.
- Data retrieval and manipulation become easier.
- You can identify if there are any repetitive data.
- You can also add new columns without interrupting the previous data in the table.
A table has a combination of rows and columns. Rows are called records and columns are called fields. In MS SQL Server, the tables are being designated within the database and schema names.
The syntax for naming the table –
create table[db_name] [schema_name] table_name col1 datatype,......);
Example –
create table student
(
name varchar2(30),
rollnumber int primary key,
course varchar2(10)
);
A table with the name student will be created in the database. A few values can be inserted as follows.
insert into student values('Aisha', 111, 'CSE');
insert into student values('Naina', 112, 'ECE');
Output –
Name |
Rollnumber |
Course |
Aisha |
111 |
CSE |
Naina |
112 |
ECE |
The values are now inserted using the insert keyword. There are other operations that can be performed on the table:
Alter table add column :
A student table has so many attributes in it. In case a user wants to add a new column, it can be done as follows.
alter table table_name add column_name datatype column_constraint;
Suppose a user wants to add age of student in the student table, it can be done as follows.
alter table student add age int;
The age column will be created. The user can insert age in the table as follows:
insert into student values('Aisha', 111, 'CSE', 18);
insert into student values('Naina', 112, 'ECE', 19);
Output –
Name |
Rollnumber |
Course |
Age |
Aisha |
111 |
CSE |
18 |
Naina |
112 |
ECE |
19 |
Alter table drop column :
There are some columns that remain unused or rarely during the life cycle of a database. In order to delete them from the table, It has to be done as follows.
alter table table_name drop column column_name;
In the student table, age is one attribute that is unused. To delete the column, the query must be given as follows:
alter table student drop column age;
Output –
Name |
Rollnumber |
Course |
Aisha |
111 |
CSE |
Naina |
112 |
ECE |
It removes the data from database records. It means the data is permanently removed and cannot be retrieved again.
Similar Reads
Select Statement in MS SQL Server
The SELECT statement in SQL Server is a foundational SQL command used for querying and retrieving data from one or more tables within a database. This command allows users to specify which columns and rows to retrieve and apply filters to focus on specific data and perform various operations to mani
4 min read
Insert Statement in MS SQL Server
The SQL Server INSERT statement is a fundamental command used to add new rows of data to a table. Whether we are inserting specific values, utilizing default values or copying data from another table. In this guide, weâll explore various ways to use the Insert statement in MS SQL Server with the hel
4 min read
Order by in MS SQL Server
In this article, order by and terms related to order by will be discussed. Introduction - There are some instances where the tables are to be arranged in a chronological order. While the users use the select statement to retrieve rows, one cannot guarantee that the rows are arranged in an order. To
2 min read
Primary key in MS SQL Server
A table has a particular number of columns and each column has n number of rows. At times, there might be a probability of repeated rows in a column. For example, a column named identification number has repeated rows. To avoid the duplication of rows, the concept of a key has been introduced. A key
2 min read
Joins in MS SQL Server
A database comprises tables and each table in case of RDBMS is called a relation. Let us consider a sample database named University and it has two tables named Student and Marks. If a user wants to transfer a certain set of rows, insert into select statement is used along with the query. But if a u
2 min read
How to Limit Rows in a SQL Server?
To limit rows in SQL Server, use the TOP clause in the SELECT statement. Using the TOP clause in SQL Server, users can limit the number of rows in the results set. Here, we will understand how to limit rows in SQL Server with the help of different examples. Steps to Limit Rows in SQL ServerLet's che
3 min read
Insert Into Select statement in MS SQL Server
The INSERT INTO SELECT statement in SQL Server is a versatile feature that enables you to efficiently copy data from one or more tables into another table. This functionality is essential for tasks such as data transfer, backup creation, and data merging. In this article, We will learn to Insert Int
4 min read
SQL INSERT INTO SELECT Statement
In SQL, the INSERT INTO statement is used to add or insert records into the specified table. We use this statement to insert data directly into a table by specifying column names in a specific order. The SELECT statement is used to retrieve data from the table, and it can be used in conjunction with
6 min read
Introduction of MS SQL Server
Data is a collection of facts and figures and we have humungous data available to the users via the internet and other sources. To manipulate the data, Structured Query Language (SQL) in short has been introduced years ago. There are different versions of SQL available in the market provided by diff
2 min read
DELETE Statement in MS SQL Server
The DELETE statement in MS SQL Server deletes specified records from the table. SyntaxMS SQL Server DELETE statement syntax is: DELETE FROM table_name WHERE condition; Note: Always use the DELETE statement with WHERE clause. The WHERE clause specifies which record(s) need to be deleted. If you exclu
1 min read