SQL: The Basic Syntax of SQL (Part IV)

Home /

Table of Contents

SQL DELETE Statement

The DELETE statement is used in SQL to delete existing rows from a table. It allows you to specify the rows to delete using a WHERE clause, and it is used to remove data from the table.

Here is the basic syntax of the DELETE statement:

DELETE FROM table_name WHERE condition;

The DELETE statement is used to delete rows from a table with the specified name. The WHERE clause specifies the conditions to use to filter the rows to delete.

Here is an example of the DELETE statement:

SQL
DELETE FROM customers WHERE name = 'John Doe';

This statement deletes all rows from the “customers” table where the “name” column is ‘John Doe’.

SQL CREATE DATABASE Statement

The CREATE DATABASE statement is used in SQL to create a new database. It allows you to specify the name of the database, and it is used to create the physical storage for the database on the server.

Here is the basic syntax of the CREATE DATABASE statement:

CREATE DATABASE database_name;

The CREATE DATABASE statement is used to create a new database with the specified name.

Here is an example of the CREATE DATABASE statement:

SQL
CREATE DATABASE my_database;

This statement creates a new database named “my_database”. Note that the name of the database must be unique within the server, and it cannot be the same as any other existing database name.

SQL DROP DATABASE Statement

The DROP DATABASE statement is used in SQL to delete a database. It permanently removes the database and all of its data, and it cannot be undone.

Here is the basic syntax of the DROP DATABASE statement:

DROP DATABASE database_name;

The DROP DATABASE statement is used to delete a database with the specified name.

Here is an example of the DROP DATABASE statement:

SQL
DROP DATABASE my_database;

This statement deletes the “my_database” database from the server.

SQL USE Statement

The USE statement is used in SQL to select a database to work with. It allows you to specify the name of the database, and it is used to set the default database for the current session.

Here is the basic syntax of the USE statement:

USE database_name;

The USE statement is used to select a database with the specified name.

Here is an example of the USE statement:

SQL
USE my_database;

This statement selects the “my_database” database as the default database for the current session. Note that the database must exist on the server, and you must have permission to access it.

SQL COMMIT Statement

The COMMIT statement is used in SQL to end a transaction and save the changes to the database. It is used to make the changes made during the transaction permanent, and it is usually followed by the END TRANSACTION statement to end the transaction.

Here is the basic syntax of the COMMIT statement:

COMMIT;

The COMMIT statement is used to end a transaction and save the changes to the database.

Here is an example of the COMMIT statement:

SQL
BEGIN TRANSACTION;

UPDATE customers SET name = 'John Smith' WHERE id = 1;
INSERT INTO orders (customer_id, amount) VALUES (1, 100);

COMMIT;
END TRANSACTION;

This block of code starts a transaction, updates a row in the “customers” table, and inserts a new row into the “orders” table. The COMMIT statement is used to save the changes to the database, and the END TRANSACTION statement is used to end the transaction.

SQL ROLLBACK Statement

The ROLLBACK statement is used in SQL to undo the changes made during a transaction. It is used to discard the changes made during the transaction, and it is usually followed by the END TRANSACTION statement to end the transaction.

Here is the basic syntax of the ROLLBACK statement:

ROLLBACK;

The ROLLBACK statement is used to undo the changes made during a transaction.

Here is an example of the ROLLBACK statement:

SQL
BEGIN TRANSACTION;

UPDATE customers SET name = 'John Smith' WHERE id = 1;
INSERT INTO orders (customer_id, amount) VALUES (1, 100);

ROLLBACK;
END TRANSACTION;

This block of code starts a transaction, updates a row in the “customers” table, and inserts a new row into the “orders” table. The ROLLBACK statement is used to undo the changes made during the transaction, and the END TRANSACTION statement is used to end the transaction.

Share The Tutorial With Your Friends
Twiter
Facebook
LinkedIn
Email
WhatsApp
Skype
Reddit

Check Our Ebook for This Online Course

Advanced topics are covered in this ebook with many practical examples.

Other Recommended Article