SQL stands for Structured Query Language. SQL is a language which allow us to access data or modify data from the database. Before proceeding towards the SQL we should also know about database. DataBase can be defined as the collection of multiple tables which consist data in the form of rows and columns. Different task can be done on database with the help of Structured Query Language like create database, create table, insert data, delete data, update data . Here we are going to discuss some commands used to do the above mentioned tasks. For doing these commands you should have SQL Server installed in your system. Now one by one we will discuss the basic commands of SQL.
Now one by one we will discuss above commands in detail:-
• SQL CREATE DATABASE
With the help of this command we can create database in SQL SERVER. As database is a collection of tables that is why we have to create database before creating table. Syntax of this command is given below:-
create database databasename;
for example:- create database EmployeeDataBase;
Above mentioned command will create a database in the SQL SERVER with name Employee.
• SQL CREATE TABLE
After database creation now we want to create table inside the database we created so to use the database we need a command. Command to use a particular database is given below:-
As As here we want to use Employee database so we will use that database in the given way:-
Now EmployeeDataBase is in use so if we will create table so that table will be visible within EmployeeDataBase only. As within a database we can have multiple tables simultaneously within a table we can have multiple columns so create table command is given below:-
for example:- Suppose we are creating a table with name EmployeeBasicDetail and columns will be Employee_Name, Employee_Address, Employee_Contact, Employee_EmailId. Command will be written in the following way:-
We can insert the data within the table in two forms. First form includes direct values as we want to insert in all columns of the table but if we want to insert only in few columns so we have to write column names also. First form of insert command will be as given:-
insert into table_name values (value1,value2,value3,...);
Second form will be :-
insert into table_name (column1,column2,column3,...) values (value1,value2,value3,...);
First form of insert command:-
insert into EmployeeBasicDetail values(’Tanuja’,’Dehradun’,’9632589632’,’Tanu@yahoo.com’);
Second form of insert command(insert data in specific columns):-
insert into EmployeeBasicDetail (Employee_Name, Employee_Address) values(’Tanuja’,’Dehradun’);
• SQL DELETE COMMAND
The delete command is used to delete rows in a table. So when we want to delete some rows from a table we use delete command. We will also provide condition to delete the row with the help of where condition within the command.
delete from table_name where some_column=some_value;
Suppose we want to delete data from EmployeeBasicDetail where employee name is ‘Tanuja’ so the command will be used in the given way:-
delete from EmployeeBasicDetail where Employee_Name =‘Tanuja’;
• SQL UPDATE COMMAND
The update command is used to update existing records in a table. Suppose we want to update contact number of Employee from EmployeeBasicDetail table for this command will be given below:-
update table_name set column1=value1,column2=value2,... where some_column=some_value;
Suppose we wish to update the employee name from ‘Tanuja’ with ’Tanuja Bhatt’ this can be done with the help of update command which is given below:-
update EmployeeBasicDetail set Employee_Name =‘Tanuja Bhatt’ where Employee_Name =‘Tanuja’;
• ALTER TABLE COMMAND
The alter table statement is used to add, delete, or modify columns in an existing table. Below is the syntax to change the datatype of a particular column.
alter table table_name alter column column_name datatype
alter table EmployeeBasicDetail alter Employee_Name int;
Now after this command datatype of Employee_Name column in EmployeeBasicDetail will change from varchar(255) to int.
• TRUNCATE TABLE COMMAND
If we want to delete the whole data from a table than we will use truncate command.
truncate table table_name
truncate table EmployeeBasicDetail;
• DROP TABLE COMMAND
If we want to delete the whole table than drop command is useful for us. It will drop the whole structure of the table from the database.
drop table table_name
drop table EmployeeBasicDetail;
• DROP DATABASE COMMAND
If we want to delete the database from SQL SERVER than this command is useful which is given below:-
drop database database_name
drop database EmployeeDataBase;
So this blog includes the very basic commands used in SQL SERVER for different tasks.