Today we will read about the SQL database, and see how the database is created in SQL and how the database is selected and how we create tables in the database and how to store data in that created table. We will read through this tutorial. And will understand every SQL query with an example.
SQL Query:
👉 Create Database
The Create Database is used to create a new Database.
Syntax:-
CREATE DATABASE DBName;
Example: I will show how to create a database in SQL.
Query :
create database my_db;
Output :
👉 Create Table
In SQL, the CREATE TABLE statement is used to create a new table in a database.
Syntax:-
CREATE TABLE table_name(column1 datatype,column2 datatype,column3 datatype);
Example: I will show how to create a table in the database.
Query :
The table is created and you can use the DESC command and see the table.
CREATE TABLE my_tb(Name varchar(50),Roll int(4),address varchar(40));
Output :
The table is created and you can use the DESC command and see the table.
Syntax :
desc my_tb;
Output :👉 Insert Table
In SQL INSERT INTO statement is used to insert new records in a table.
Syntax:-
Query :
Example: I will show how to insert data in a table.INSERT INTO table_name(column1, column2,.......columnN)VALUES (value1, value2,........valueN);
Query :
INSERT INTO my_tb (Name, Roll, address)
VALUES ('RAM', '01', 'Delhi');
Now, you can see inserted data in the table and also used SELECT Statement to see table data.
Syntax:-
SELECT * FROM table_name;
Query :
SELECT * FROM my_tb;
Output :
You can insert multiple records in a table at one time.
Example:
Output :INSERT INTO my_tb (Name, Roll, address)
VALUES ('Mohan', '02', 'Mumbai'),('Amit', '03', 'Kanpur'),('Madhur', '04', 'Meerut'),('Pradeep', '05', 'Nepal'),('Vikas', '06', 'Meerut'),('Deepak', '07', 'Delhi'),('Kapil', '08', 'Goa'),('Ankur', '09', 'Mumbai'),('Vibhu', '10', 'Banglore');
👉 Update Table
In SQL, UPDATE Query is used to modifying the existing records in a table.
Syntax:-
UPDATE table_nameSET column1 = value name, column2 = value name,..........columnN = valueNWHERE conditions;
Example: I will show how to update data in the table.
Query :
UPDATE my_tb
SET address = 'Meerut'
WHERE Roll =9;
Output :
Delete Table
DELETE Statement is used to delete/remove existing records or records of the entire table from the table.
Syntax:-
For Specific Record:-
DELETE FROM table_name WHERE condition;
Example: I will show how to delete a specific record in the table.
Query :
DELETE FROM my_tab WHERE Name="Vikas";
Drop-Table
DROP table statement is used to delete a table and all the data like- indexes, triggers, etc.
Syntax:-
DROP TABLE TBName;
Example: Let us first verify the my_tb table name and then we will delete it from the database as shown below.
Query :
DROP TABLE my_tb;
Drop Database
In SQL, the DROP DATABASE statement is used to leave an existing table or database in a database.
0 Comments