Hello, readers in today’s article I am going to describe the top basic SQL queries. I think this article must be useful for beginners. I write about some basic queries like Create, Drop, Alter, Select, Insert and Update, etc.
So, without wasting any time let’s start…
Create database:
For Creating a new database.
create database mydb;
Here “mydb” is the name of your database.
Drop Database:
To delete an existing database.
drop database mydb;
Use Query:
Select and use an existing database.
use mydb;
Create Table:
To create a new table within a database.
create table mytable(
id int identity(1,1) primary key,
name varchar(50) not null,
city varchar(50) not null,
);
Here “mytable” is the name of your table. “id“,”name“, and “city” are the columns of the table. Here id is a primary key constraint that defines that id is not null and unique.
Drop Table:
To delete a table from the current database.
drop table mytable;
Alter Table:
Alter table is used to modify our table. The “Alter Table” command is used to add a new column, delete an existing column, or modify an existing column of our table.
To add a new column in an existing table:
alter table mytable
add pin int not null;
The above command adds a new column for “pin” of type “integer“.
To delete an existing column from the table:
alter table mytable
drop column pin;
To change the datatype of a column in the table:
alter table mytable
alter column mobile varchar not null;
If your mobile no. is an integer type and you want to change it into a varchar type this command will work.
INSERT INTO command:
To fill the records into your created table.
insert into mytable (name, city, pin)
values ('ankit','new delhi','110059');
Use the following command if you insert more than one records in a table.
insert into mytable (name, city, pin)
values ('raj','ballia','11111'),('chirag','noida','231123');
Select Query:
Select Query used to show all the records of an existing table.
select * from mytable;
Update Query:
To update or modify an existing record in the table.
update mytable
set pin='110061'
where name='ankit';
Delete Query:
To delete the inserted wrong records from the table.
delete from mytable where id=1;
To delete all records from the table using the following command:
delete from mytable;
That’s all readers this article is only dedicated to beginners. These are some basic queries in SQL server that is necessary to know.
MUST READ: CRUD OPERATION IN C#