SQL- Create Database

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 :


👉 Select Database

The SQL USE statement is used to select any of its databases in SQL.

Syntax:-    
USE DBName;    
Example:  I will show how to USE database in SQL.

Query :
USE 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 :  
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:- 
INSERT INTO table_name(column1, column2,.......columnN)
VALUES (value1, value2,........valueN);
ExampleI will show how to insert data in a table.

Query :

INSERT INTO my_tb (Name, Roll, address)
VALUES ('RAM', '01', 'Delhi'); 
Output :                        

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:
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');
Output :


👉 Update Table

In SQL, UPDATE Query is used to modifying the existing records in a table.

Syntax:- 
UPDATE table_name
SET column1 = value name, column2 = value name,..........columnN = valueN
WHERE 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";
Output:-

For ALL Records:-

Syntax:-  
DELETE FROM TBName;
Example: Let's take an example to delete a table in the database.

Query:
 DELETE  FROM my_tb;
Output :  
               

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;
Output  : 
                


Drop Database

In SQL, the DROP DATABASE statement is used to leave an existing table or database in a database.

Syntax:- 

DROP DATABASE DBName;

Example: Let's take an example to drop a database name "students".
Output:-

 Query:

DROP DATABASE students;

Now you can check that your database is removed by executing the following query.

Output:-


Post a Comment

0 Comments