Some Important SQL Command
1 Create Database
To create a new database:
Syntax:
sql
CREATE DATABASE database_name;
Example:
sql
CREATE DATABASE school;
2. Delete (Drop) Database
To delete an existing database:
Syntax:
sql
DROP DATABASE database_name;
Example:
sql
DROP DATABASE school;
3. Create Table
To create a table within a specific database:
Syntax:
sql
CREATE TABLE table_name (
column1 datatype constraints,
column2 datatype constraints,
...
columnN datatype constraints
);
Example:
Create a table ipstudents in the school database with the columns roll_no,
name, and marks
sql
CREATE TABLE ipstudents (
roll_no INT PRIMARY KEY,
name VARCHAR(100),
marks INT
);
Steps:
First, create or select the database:
sql
USE school;
Then, create the table with the above CREATE TABLE command.
Here are the basic SQL commands for CRUD operations (Create, Read, Update,
Delete) in MySQL:
1. Create (Inserting data into a table)
The INSERT INTO command is used to add data into a table.
Syntax:
sql
Copy code
INSERT INTO table_name (column1, column2, column3, ...)
VALUES (value1, value2, value3, ...);
Example:
sql
INSERT INTO ipstudents (roll_no, name, marks)
VALUES (1, 'John Doe', 85);
Or
INSERT INTO ipstudents VALUES (1, 'John Doe', 85);
If you want to Insert multiple value
INSERT INTO ipstudents VALUES (1, 'John Doe', 85), (2, ‘ram’, 95), (3, ‘suraj’,
55);
2. Read (Retrieving data from a table)
The SELECT command is used to fetch data from a table.
Syntax:
sql
SELECT column1, column2, ...
FROM table_name
WHERE condition;
Example:
Retrieve all columns:
Sql:
SELECT * FROM ipstudents;
Retrieve specific columns:
Sql:
SELECT name, marks FROM ipstudents WHERE roll_no = 1;
3. Update (Modifying existing data in a table)
The UPDATE command is used to modify existing data.
Syntax:
sql
UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition;
Example:
Update a student’s marks:
Sql
UPDATE ipstudents
SET marks = 90
WHERE roll_no = 1;
4. Delete (Removing data from a table)
The DELETE command is used to remove rows from a table.
Syntax:
sql
DELETE FROM table_name
WHERE condition;
Example:
Delete a student record with roll_no = 1:
sql
DELETE FROM ipstudents
WHERE roll_no = 1;
Deleting Rows with NULL values:
If you want to delete rows where a column has NULL values:
sql
DELETE FROM ipstudents
WHERE marks IS NULL;
These are the basic CRUD operations that help manage data in a MySQL
database
5. Add a New Column
The ALTER TABLE statement is used to add a new column to an existing table.
Syntax:
sql
ALTER TABLE table_name
ADD column_name datatype;
Example:
Add a new column address of type VARCHAR(255) to the ipstudents table:
sql
ALTER TABLE ipstudents
ADD address VARCHAR(255);
6. Delete a Column
To remove a column from a table, you use the ALTER TABLE command with the
DROP keyword.
Syntax:
sql
ALTER TABLE table_name
DROP COLUMN column_name;
example
ALTER TABLE ipstudents
DROP COLUMN address;
7. Rename column
Syntax:
sql
ALTER TABLE table_name RENAME COLUMN old_column_name TO
new_column_name;
Example:
sql
ALTER TABLE ipstudents RENAME COLUMN marks TO total_marks;