You are on page 1of 21

Relational Data Base Management System

SQL

1
SQL
(Structured Query Language)
used for accessing and modifying information in one or
more data tables and rows of a database..
Keyword-based Language
Act as an interface language between the user and the
database

1. Data Definition Language (DDL)


2. Data Controlled Language (DCL)
3. Data Manipulation Language (DML)
4. Transaction Control Language (TCL)
5. Queries
2
1.Data Definition Language
Create, alter or to drop database objects from the system
E.g.:
• CREATE TABLE
• CREATE VIEW
• DROP TABLE
• DROP VIEW
These commands cannot use to modify data.

3
2.Data Control Language
Grant or Control administer privileges granted to
users.
Determines how, when, and whom can
manipulate data.
Provide commands to specify access rights to
tables and views.
GRANT, REVOKE

4
3.Data Manipulation Language
These statements change the data in the
database.
E.g.:
• INSERT
• UPDATE
• DELETE

5
4.Transaction Control Language

•Control if and where any changes made to


data are made permanent.

COMMIT
ROLLBACK
SAVEPOINT

6
Queries
• Helps users to retrieve data in the database.

7
8
The overall syntax is very simple:
SELECT [ ALL / DISTINCT ] column_list
FROM table_list
[ WHERE conditional expression ]
[ GROUP BY group_by_column_list ]
[ HAVING conditional_expression ]
[ ORDER BY order_by_column_list ]

9
DATABASE Operations
• CREATE DATABASE databasename;
• DROP DATABASE databasename;
• SHOW DATABASES;

10
MySQL Data Types
• String data

11
Numeric Datatypes

12
13
Creating a Table
My Sql >CREATE TABLE tbl_student(
stdId int,
lastName varchar(10),
firstName varchar(10),
city varchar(10)
);
View tables in the database :
- show tables;

View table structure :


- DESC tbl_student ;

When you create a table you specify the


* Table name
* Column names
* Type of values in the column (Data types)
* Maximum width of the column
14
Constraints
• Used to specify rules data in a table.
• This ensures the accuracy and reliability of the data in the table.
• NOT NULL - Ensures that a column cannot have a NULL value
• UNIQUE - Ensures that all values in a column are different
• PRIMARY KEY - A combination of a NOT NULL and UNIQUE. Uniquely
identifies each row in a table
• FOREIGN KEY - Uniquely identifies a row/record in another table
• CHECK - Ensures that all values in a column satisfies a specific
condition
• DEFAULT - Sets a default value for a column when no value is
specified
• INDEX - Used to create and retrieve data from the database very
quickly

15
Create a table with constrains
• Primary key is the user id column.
CREATE TABLE users(
user_id INT AUTO_INCREMENT PRIMARY KEY,
username VARCHAR(40),
password VARCHAR(255),
email VARCHAR(255) );

• Primary key as the table constraint.


CREATE TABLE roles(
role_id INT AUTO_INCREMENT,
role_name VARCHAR(50),
PRIMARY KEY(role_id) );

16
Primary key with multiple columns and the
foreign key.
CREATE TABLE user_roles(
user_id INT,
role_id INT,
PRIMARY KEY(user_id,role_id),
FOREIGN KEY(user_id) REFERENCES users(user_id),
FOREIGN KEY(role_id) REFERENCES roles(role_id) );

17
Check constraint

18
Alter table
• Is used to modify a table by using add, drop and
modify commands
1) Add a column to a table
ALTER TABLE parts
ADD name VARCHAR(20) NOT NULL;
2) Modify a column
ALTER TABLE parts
MODIFY description VARCHAR(100) NOT NULL;

19
Alter table…contd.
3) Rename a column in a table
ALTER TABLE parts
CHANGE COLUMN description disc VARCHAR(100)
NOT NULL;

4) Drop a column
ALTER TABLE parts DROP COLUMN name;

5. Rename table
ALTER TABLE parts RENAME TO VehicleParts;

20
Dropping TABLES

• The SQL command is:

DROP TABLE table_name

• If you have data in the table, it will delete permanently


• Once the Table is deleted, you cannot get it back

21

You might also like