RDBMS Unit-3
RDBMS Unit-3
  SQL
          Introduction to SQL
          History of SQL
          Benefits of SQL
          Features of SQL
          DBMS v/s RDBMS
          Introduction to SQL: Data types, Constraints
          Commands in SQL: Create table, Drop command, Alter Queries in SQL
          Statements in SQL (Insert, delete and update)
          Manipulation of data
          Conclusion of Unit
  # Introduction to SQL
       SQL, stands for Structured Query Language, is a domain-specific programming language used
       for managing and manipulating relational databases. SQL is essential for both database
       administrators and developers who work with relational databases. The main purpose of SQL is
       to provide a structured and standardized way to communicate with databases. It allows users to
       define, manipulate, and query data, as well as manage the structure of the database itself. SQL is
       essential for tasks such as creating and modifying tables, inserting, updating, and deleting
       records, and retrieving data based on specific criteria.
       SQL is not tied to any specific database management system but rather serves as a standardized
       way to interact with various DBMS, such as MySQL, PostgreSQL, Oracle, Microsoft SQL Server,
       and many others. While the core concepts of SQL remain relatively consistent across systems,
       there might be some variations in syntax and features between different DBMS implementations.
       SQL is not a single, monolithic language, but rather a collection of sub-languages that cater to
       different aspects of database management. Some of the most commonly used sub-languages
       within SQL include:
           1. Data Definition Language (DDL): DDL is used for defining and managing the structure
               of the database. Statements like CREATE, ALTER, and DROP are part of DDL and are
               used to create and modify tables, indexes, and other database objects.
           2. Data Manipulation Language (DML): DML allows you to manipulate the data within the
               database. The main statements in DML are INSERT, UPDATE, and DELETE, which
               respectively add new records, modify existing records, and remove records from tables.
           3. Data Control Language (DCL): DCL is concerned with managing user access and
               permissions within the database. Statements like GRANT and REVOKE are used to grant
               or revoke privileges to users and roles.
           4. Transaction Control Language (TCL): TCL statements are used to manage transactions,
               which are sequences of one or more database operations that are executed as a single
               unit. The main TCL statements are COMMIT, ROLLBACK, and SAVEPOINT.
Faculty of : FCE.        Program: BTech        Class/Section: 4 F/J                  Date: ……………………….
Name of Faculty: Dr. S.K.Kapoor                Name of Course: RDBMS                 Code: BCECCE4103
#History of SQL
       # Benefits of SQL
       Some of the key benefits of SQL are:
       1. Ease of Use: SQL uses a simple and intuitive syntax that resembles natural language,
           making it relatively easy to learn and use, especially for querying and managing data.
       2. Data Retrieval and Manipulation: SQL provides powerful and flexible tools for retrieving,
           filtering, sorting, and aggregating data from databases. This makes it easy to extract specific
           information from large datasets.
       3. Data Integrity and Consistency: SQL enforces data integrity by allowing the definition of
           constraints, such as primary keys, foreign keys, and unique constraints. This ensures that
           data remains accurate and consistent over time.
       4. Data Definition and Modeling: SQL supports the creation, modification, and deletion of
           database objects such as tables, indexes, and views. It also enables the definition of
           relationships between tables, facilitating data modeling.
       5. Database Normalization: SQL supports the principles of database normalization, which
           helps organize data efficiently, reduces data duplication, and improves overall data integrity.
       6. Transaction Management: SQL supports transactions, allowing users to group multiple SQL
           statements into a single unit of work. This ensures that changes to the database are either
           fully completed or fully rolled back in case of errors or failures.
       7. Concurrency Control: SQL provides mechanisms for managing concurrent access to data by
           multiple users or applications, ensuring that transactions are isolated and consistent.
       8. Security and Access Control: SQL enables the definition of access permissions, roles, and
           privileges at both the database and object levels. This helps protect sensitive data and
           restricts unauthorized access.
       9. Scalability: SQL databases can handle large amounts of data and scale horizontally (adding
           more servers) or vertically (upgrading server hardware) to accommodate increasing workloads.
Faculty of : FCE.        Program: BTech        Class/Section: 4 F/J                   Date: ……………………….
Name of Faculty: Dr. S.K.Kapoor                Name of Course: RDBMS                  Code: BCECCE4103
  # Features of SQL
       1. High Performance
       SQL includes features like high-performance programming skills for highly transactional
       database systems. SQL has several ways to represent the data more analytically.
       2. High Availability
       Structured Query Language is cooperative with databases like Microsoft SQL Server, MS Access,
       SAP Adaptive Server, MySQL, Oracle Database, SAP HANA, etc. All of these relational database
       management systems support SQL. It is easy to create an application extension for procedural
       programming and various other functions, which are additional features, thus converting SQL
       into a powerful tool.
       5. High Security
       It is straightforward to grant permissions on tables, procedures, and views; hence SQL gives
       security to your data.
       7. Management Ease
       SQL programming language is utilized in each and every relational database management
       system. Standard commands like Drop, Insert, Delete, Select, Create, Update is easily accessible
       by a developer to manage large amounts of data from the database.
       8. Open Source
       Structures Query Language is an open-source programming language feature for developing
       relational database management systems.
Faculty of : FCE.          Program: BTech      Class/Section: 4 F/J                   Date: ……………………….
Name of Faculty: Dr. S.K.Kapoor                Name of Course: RDBMS                  Code: BCECCE4103
                               It is a normal size floating point number. Its size parameter specifies the
            DOUBLE(size, d)    total number of digits. The number of digits after the decimal is specified
                               by d parameter.
                               It is used to specify a fixed point number. Its size parameter specifies the
                               total number of digits. The number of digits after the decimal parameter
            DECIMAL(size, d)   is specified by d parameter. The maximum value for the size is 65, and
                               the default value is 10. The maximum value for d is 30, and the default
                               value is 0.
                              It is used to specify the time format. Its format is hh:mm:ss. Its supported
            TIME(fsp)
                              range is from '-838:59:59' to '838:59:59'
  # SQL Constraints
  Constraints are rules that define the properties and relationships of the data in a database.
  Constraints are the rules applied to a data columns or the complete table to limit the type of data
  that can go into a table. They help enforce data integrity and maintain consistency.
  Here are some common constraints:
       1.   NOT NULL
       2.   CHECK
       3.   DEFAULT
       4.   PRIMARY KEY
       5.   AUTO_INCREMENT
       6.   UNIQUE
       7.   INDEX
       8.   ENUM
       9.   FOREIGN KEY
       1. NOT NULL
          This constraint specifies that the column cannot have NULL or empty values. The below
          statement creates a table with NOT NULL constraint.
                 Ex- mysql> CREATE TABLE STUDENT(ID INTEGER, LASTNAME TEXT NOT NULL,
                                FIRSTNAME TEXT NOT NULL, CITY VARCHAR(35));
       2. CHECK(expr):
          It controls the value in a particular column. It ensures that the inserted value in a column
          must be satisfied with the given condition. In other words, it determines whether the value
          associated with the column is valid or not with the given condition.
                    Ex- mysql> CREATE TABLE Persons (ID int NOT NULL,Name varchar(45) NOT NULL,
                                  Age int CHECK (Age>=18) );
       3. DEFAULT
          This constraint is used to set the default value for the particular column where we have not
          specified any value. It means the column must contain a value, including NULL.
                    Ex- mysql> CREATE TABLE Persons ( ID int NOT NULL,Name varchar(45) NOT NULL,
                                  Age int, City varchar(25) DEFAULT 'New York');
       4. PRIMARY KEY
          This constraint is used to identify each record in a table uniquely. If the column contains
          primary key constraints, then it cannot be null or empty. A table may have duplicate
          columns, but it can contain only one primary key. It always contains unique value into a
          column.
                 Ex-mysql>CREATE TABLE Persons ( ID int NOT NULL PRIMARY KEY, Name
                               varchar(45) NOT NULL, Age int, City varchar(25));
Faculty of : FCE.          Program: BTech         Class/Section: 4 F/J                      Date: ……………………….
Name of Faculty: Dr. S.K.Kapoor                   Name of Course: RDBMS                     Code: BCECCE4103
       5. AUTO_INCREMENT
          This constraint automatically generates a unique number whenever we insert a new record
          into the table. Generally, we use this constraint for the primary key field in a table.
                  Ex- mysql> CREATE TABLE Animals( id int NOT NULL AUTO_INCREMENT,
                                 name CHAR(30) NOT NULL, PRIMARY KEY (id));
       6. UNIQUE
          This constraint ensures that all values inserted into the column will be unique. It means a
          column cannot stores duplicate values. MySQL allows us to use more than one column with
          UNIQUE constraint in a table.
                 Ex- mysql> CREATE TABLE ShirtBrands(Id INTEGER, BrandName VARCHAR(40)
                     UNIQUE, Size VARCHAR(30));
       7. INDEX
          This constraint allows us to create and retrieve values from the table very quickly and easily.
          An index can be created using one or more than one column. It assigns a ROWID for each
          row in that way they were inserted into the table.
                    Ex- mysql> CREATE TABLE Shirts (id INT PRIMARY KEY AUTO_INCREMENT,
                                  name VARCHAR(35), size ENUM('small', 'medium', 'large', 'x-large') );
       8. ENUM
          The ENUM data type in MySQL is a string object. It allows us to limit the value chosen from a
          list of permitted values in the column specification at the time of table creation. It is short for
          enumeration, which means that each column may have one of the specified possible values. It
          uses numeric indexes (1, 2, 3…) to represent string values.
                   Ex- mysql> CREATE TABLE Shirts (id INT PRIMARY KEY AUTO_INCREMENT,
                                name VARCHAR(35), size ENUM('small', 'medium', 'large', 'x-large') );
       9. FOREIGN KEY
           This constraint is used to link two tables together. It is also known as the referencing
           key. A foreign key column matches the primary key field of another table. It means a
           foreign key field in one table refers to the primary key field of another table.
           Ex- Let us consider the structure of these tables: Persons and Orders.
                    Table: Persons
                           mysql>CREATE TABLE Persons ( Person_ID int NOT NULL PRIMARY
                                  KEY,Name varchar(45) NOT NULL, Age int, City varchar(25) );
                    Table: Orders
                           Mysql>CREATE TABLE Orders ( Order_ID int NOT NULL PRIMARY KEY,
                                  Order_Num int NOT NULL, Person_ID int,FOREIGN KEY (Person_ID)
                                  REFERENCES Persons(Person_ID) );
Faculty of : FCE.        Program: BTech       Class/Section: 4 F/J                   Date: ……………………….
Name of Faculty: Dr. S.K.Kapoor               Name of Course: RDBMS                  Code: BCECCE4103
  DELETE STATEMENT
  DELETE statement can be used to delete multiple rows of a single table and records across multiple
  tables. In order to filter the records to be deleted, you can use the WHERE clause along with the
  DELETE statement.
  UPDATE STATEMENT
  UPDATE statement is used to modify the existing records in a table.
  Syntax: UPDATE table_name SET column1 = value1, column2 = value2...., columnN = valueN
              WHERE [condition];
Faculty of : FCE.             Program: BTech         Class/Section: 4 F/J                       Date: ……………………….
Name of Faculty: Dr. S.K.Kapoor                      Name of Course: RDBMS                      Code: BCECCE4103
# Manipulation of data
                           String Functions
                                                                                    Logical Operators
    Name              Description
    LIKE              Simple pattern matching                                  Name          Description
    NOT LIKE          Negation of simple pattern matching                    AND, && Logical AND
Aggregate Functions
                      Name                   Description
                      AVG()                  Return the average value of the argument
                      COUNT()                Return a count of the number of rows returned
                      COUNT(DISTINCT) Return the count of a number of different values
                      MAX()                  Return the maximum value
                      MIN()                  Return the minimum value
                      SUM()                  Return the sum
Faculty of : FCE.        Program: BTech         Class/Section: 4 F/J                    Date: ……………………….
Name of Faculty: Dr. S.K.Kapoor                 Name of Course: RDBMS                   Code: BCECCE4103
        Name                                  Description
        ADDDATE()                             Add time values (intervals) to a date value
        ADDTIME()                             Add time
        CURDATE()                             Return the current date
        CURRENT_DATE(), CURRENT_DATE Synonyms for CURDATE()
        CURRENT_TIME(), CURRENT_TIME Synonyms for CURTIME()
        CURTIME()                             Return the current time
        DATE()                                Extract the date part of a date or datetime expression
        DATEDIFF()                            Subtract two dates
        DAY()                                 Synonym for DAYOFMONTH()
        DAYNAME()                             Return the name of the weekday
        DAYOFMONTH()                          Return the day of the month (0-31)
        DAYOFWEEK()                           Return the weekday index of the argument
        DAYOFYEAR()                           Return the day of the year (1-366)
        HOUR()                                Extract the hour
        LAST_DAY                              Return the last day of the month for the argument
        MINUTE()                              Return the minute from the argument
        MONTH()                               Return the month from the date passed
        MONTHNAME()                           Return the name of the month
        NOW()                                 Return the current date and time
        QUARTER()                             Return the quarter from a date argument
        SECOND()                              Return the second (0-59)
        SYSDATE()                             Return the time at which the function executes
        TIME()                                Extract the time portion of the expression passed
        TIMEDIFF()                            Subtract time
        WEEK()                                Return the week number
        YEAR()                                Return the year
Faculty of : FCE.        Program: BTech         Class/Section: 4 F/J                   Date: ……………………….
Name of Faculty: Dr. S.K.Kapoor                 Name of Course: RDBMS                  Code: BCECCE4103
Comparison Operators
       Name                       Description
       >                          Greater than operator
       >=                         Greater than or equal operator
       <                          Less than operator
       <>, !=                     Not equal operator
       <=                         Less than or equal operator
       <=>                        NULL-safe equal to operator
       =                          Equal operator
       BETWEEN ... AND ...        Whether a value is within a range of values
       COALESCE()                 Return the first non-NULL argument
       GREATEST()                 Return the largest argument
       IN()                       Whether a value is within a set of values
                                  Return the index of the argument that is less than the first
       INTERVAL()                 argument
       IS                         Test a value against a boolean
       IS NOT                     Test a value against a boolean
       IS NOT NULL                NOT NULL value test
       IS NULL                    NULL value test
       ISNULL()                   Test whether the argument is NULL
       LEAST()                    Return the smallest argument
       LIKE                       Simple pattern matching
       NOT BETWEEN ... AND ... Whether a value is not within a range of values
       NOT IN()                   Whether a value is not within a set of values
       NOT LIKE                   Negation of simple pattern matching
       STRCMP()                   Compare two strings