SQL Tables

HTML
CSS
C#
SQL

SQL Table

Table is a collection of data, organized in terms of rows and columns. Table is the simple form of data storage. A table is also considered a convenient representation of relations.

Note: A table has a specified number of columns, but can have any number of rows.

Example:

Employee table

Create Table:

Explanation: SQL CREATE TABLE statement is used to create a table in a database. If you want to create a table, you should name the table and define its column and each column’s data type.

Syntax:

create table “tablename” 

(“column1” “data type”, 

“column2” “data type”, 

“column3” “data type”, 

… 

“column” “data type”);

Example :

CREATE TABLE Persons (

    PersonID int,

    LastName varchar(255),

    FirstName varchar(255),

    Address varchar(255),

    City varchar(255)

);

The PersonID column is of type int and will hold an integer.

The LastName, FirstName, Address, and City columns are of type varchar and will hold characters, and the maximum length for these fields is 255 characters.

The empty “Persons” table will now look like this:

SQL Rename table:

SQL Server does not have any statement that directly renames a table. However, it does provide you with a stored procedure named sp_rename that allows you to change the name of a table.

The following shows the syntax of using the sp_rename stored procedure for changing the name of a table:

EXEC sp_rename ‘old_table_name’, ‘new_table_name’

SQL DROP TABLE:

The DROP TABLE statement is used to drop an existing table in a database.

Syntax

DROP TABLE table_name;

SQL DELETE TABLE:

The DELETE statement is used to delete rows from a table. If you want to remove a specific row from a table you should use WHERE condition.

DELETE FROM table_name [WHERE condition];  

But if you do not specify the WHERE condition it will remove all the rows from the table.

DELETE FROM table_name

SQL TRUNCATE TABLE:

A truncate SQL statement is used to remove all rows (complete data) from a table. It is similar to the DELETE statement with no WHERE clause.

TRUNCATE TABLE Employee;  

SQL ALTER TABLE:

The ALTER TABLE statement in Structured Query Language allows you to add, modify, and delete columns of an existing table. This statement also allows database users to add and remove various SQL constraints on the existing tables.

ALTER TABLE ADD Column statement in SQL:

In many situations, you may require to add the columns in the existing table. Instead of creating a whole table or database again you can easily add single and multiple columns using the ADD keyword.

Syntax of ALTER TABLE ADD Column statement in SQL

ALTER TABLE table_name ADD column_name column-definition;  

 

ALTER TABLE table_name     

ADD column_Name1 column-definition,    

column_Name2 column-definition,    

…..    

column_NameN column-definition;    

ALTER TABLE MODIFY Column statement in SQL:

The MODIFY keyword is used for changing the column definition of the existing table.

Syntax of ALTER TABLE MODIFY Column statement in SQL

ALTER TABLE table_name ALTER COLUMN column_name column-definition;  

ALTER TABLE DROP Column statement in SQL:

In many situations, you may require to delete the columns from the existing table. Instead of deleting the whole table or database you can use DROP keyword for deleting the columns.

Syntax of ALTER TABLE DROP Column statement in SQL

ALTER TABLE table_name DROP Column column_name ;  

ALTER TABLE RENAME Column statement in SQL:

Syntax of ALTER TABLE RENAME Column statement in SQL

EXEC sp_rename ‘tablename.old_columnName’,’new_column_name’,’column’;    

Course Video

NOTE: Practice below practice questions on MSSQL SERVER, it will not execute on site editor.

1.Write a sql query to create a simple table with 5 columns

2.Write a sql query to create a table with 5 columns of varchar type

3.Write sql query to add column in above table

4.Create table with 5 column having only int data type

5. Write a sql query to drop above table