Professional Documents
Culture Documents
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
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
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;
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) );
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
21