What is the original purpose of SQL?

190    Asked by DavidEDWARDS in SQL Server , Asked on Jul 16, 2024

Which of the following is the original purpose of SQL? Here are the options given:

Web development ,Graphic design, Network security ,Managing relational database,


Answered by David EDWARDS

 Among the options given the correct option is option 4 which refers to managing a relational database. The primary work of SQL is to manage and query with relational databases. It is used for tasks such as retrieving, inserting, and also defining, and managing the schema of the database and access controls.

The SQL was developed in the early 1970s by IBM. It was made to facilitate to performance of a variety of operations on the data stored in the relation database management system. Its key functions are:-

Data querying

It is mainly used in retrieving the specific data from the database. The queries are written by using the SELECT statement to specify which columns and rows are to be retrieved. Here is the example given below:-

SELECT first_name, last_name 
FROM employees
WHERE department = ‘Sales’;

Data manipulation

It also supports inserting, updating, and deleting the data within a particular database. These particular operations are performed by using INSERT, UPDATE, AL, and DELETE statements. Here is the example given below:-

INSERT INTO employees (first_name, last_name, department, salary)

        VALUES (‘John’, ‘Doe’, ‘Marketing’, 60000);

Schema definition

It is also used in creating and modifying the structure of database objects such as tables, indexes, and also views. This can be done by using data definition language commands like CREATE, ALTER, and DROP. Here Is the example given below:-

CREATE TABLE employees (
    Employee_id INT PRIMARY KEY,
    First_name VARCHAR(50),
    Last_name VARCHAR(50),
    Department VARCHAR(50),
    Salary DECIMAL(10, 2)
);

Transaction control

It can also support transaction management to ensure the integrity of the data. Commands such as BEGIN TRANSACTION, COMMIT, and ROLLBACK are used to control transactions. Here is the example given below:-

BEGIN TRANSACTION;
UPDATE accounts
SET balance = balance – 100
WHERE account_id = 123;
UPDATE accounts
SET balance = balance + 100
WHERE account_id = 456;
COMMIT;

Here is the example given of making a relational database management:-

CREATE DATABASE StudentDB;
USE StudentDB;
CREATE TABLE students (
    Student_id INT AUTO_INCREMENT PRIMARY KEY,
    First_name VARCHAR(50) NOT NULL,
    Last_name VARCHAR(50) NOT NULL,
    Major VARCHAR(50),
    Gpa DECIMAL(3, 2)
);

The following class would help you in handling the above database connection and CRUDE( CREATE, READ, UPDATE, DELETION) by using Database Manager.java:-

Import java.sql.Connection;
Import java.sql.DriverManager;
Import java.sql.PreparedStatement;
Import java.sql.ResultSet;
Import java.sql.SQLException;
Import java.sql.Statement;
Public class DatabaseManager {
    Private static final String JDBC_URL = “jdbc:mysql://localhost:3306/StudentDB”;
    Private static final String JDBC_USER = “root”; // Replace with your MySQL username
    Private static final String JDBC_PASSWORD = “”; // Replace with your MySQL password
    Private Connection connection;
    Public DatabaseManager() throws SQLException {
        Connect();
    }
    Private void connect() throws SQLException {
        If (connection == null || connection.isClosed()) {
            Connection = DriverManager.getConnection(JDBC_URL, JDBC_USER, JDBC_PASSWORD);
        }
    }
    Public void createTable() throws SQLException {
        String createTableSQL = “CREATE TABLE IF NOT EXISTS students (“ +
                                “student_id INT AUTO_INCREMENT PRIMARY KEY, “ +
                                “first_name VARCHAR(50) NOT NULL, “ +
                                “last_name VARCHAR(50) NOT NULL, “ +
                                “major VARCHAR(50), “ +
                                “gpa DECIMAL(3, 2))”;
        Statement statement = connection.createStatement();
        Statement.execute(createTableSQL);
    }
    Public void addStudent(String firstName, String lastName, String major, double gpa) throws SQLException {
        String insertSQL = “INSERT INTO students (first_name, last_name, major, gpa) VALUES (?, ?, ?, ?)”;
        PreparedStatement preparedStatement = connection.prepareStatement(insertSQL);
        preparedStatement.setString(1, firstName);
        preparedStatement.setString(2, lastName);
        preparedStatement.setString(3, major);
        preparedStatement.setDouble(4, gpa);
        preparedStatement.executeUpdate();
    }
    Public ResultSet getAllStudents() throws SQLException {
        String querySQL =


Your Answer

Interviews

Parent Categories