SQL 1
SQL 1
System Lab
CBCA109
Continuous Lab Evaluation
About
(30)
IMS Lab
Evaluation
Data and Information
Data is raw, unorganized facts •Data can be something simple, •When data is processed,
that need to be processed. random and useless until it is organized, structured or
organized. presented in a given context to
make it useful, it is called
information.
–Each student's exam marks –The average score of a department or of
the entire college is information
that is derived from the given data.
Information Management System
It is developed, distributed
Supports including Windows,
and supported by Oracle
Linux, UNIX, Mac…
corporation.
Install • Scroll down this page
MySQL
Install MySQL • Click on MySQL Community downloads
Install
MySQL
• Click on the option according to your
environment
Install
MySQL
• For windows, download
from the 1st option
MySQL
Installer
• Choose Developer
and click next and
continue the process
MySQL
Installer
• click next and
continue the process
SQL categorization
SQL commands are mainly categorized into
four categories as:
• DDL – Data Definition Language
• DML – Data Manipulation Language
• DQL – Data Query Language
• DCL – Data Control Language
DDL(Data Definition
TRUNCATE–is used to
Language) : Data Definition
remove all records from a
Language consists of the SQL ALTER-is used to alter the
table, including all spaces
commands that can be used structure of the database.
allocated for the records are
to define the database
removed.
schema.
DDL
It simply deals with
descriptions of the database
COMMENT –is used to add
schema and is used to create DROP – is used to delete
comments to the data
and modify the structure of objects from the database.
dictionary.
database objects in the
database.
Examples of DML:
DQL (Data Query The purpose of DQL Example of DQL: SELECT– is used to retrieve
Language) : Command is to get some data from the a database.
schema relation based on
the query.
DCL
Syntax
• DROP DATABASE databasename;
Note: Deleting a database will result in loss of complete
information stored in the database!
Example
• DROP DATABASE testDB;
Tip: You can check it in the list of databases with the
following SQL command: SHOW DATABASES;
Use Database
To use the database, we need to write following SQL
command:
Syntax
• USE databasename;
Example
• USE testDB;
Creating Table
• The CREATE TABLE statement is used to create a new table in
a database.
Syntax
• CREATE TABLE table_name (
column1 datatype,
column2 datatype,
column3 datatype,
....
);
Example
• CREATE TABLE Persons (
PersonID int,
LastName varchar(255),
FirstName varchar(255),
Address varchar(255),
City varchar(255)
);
Popular Data Types
Syntax
Syntax
• TRUNCATE TABLE table_name;
Example
• TRUNCATE TABLE team;
Alter Table
• The ALTER TABLE statement is used to add,
delete, or modify columns in an existing table.
Example
• ALTER TABLE Persons
ADD Email varchar(255);
ALTER Table -
DROP Column
To delete a column in a table, use the following
syntax (notice that some database systems don't allow
deleting a column):
Example
• ALTER TABLE Persons
DROP COLUMN Email;
ALTER Table - ALTER/MODIFY
Column
To change the data type of a column in a table, use the
following syntax:
ALTER TABLE table_name
MODIFY COLUMN column_name datatype;
Example
Syntax
Example
Syntax
Example
NOT NULL: By default, a column can hold NULL values. The NOT
NULL constraint enforces a column to NOT accept NULL values.
•Eg. Select position, pname, role from team where shirt_no is NOT NULL.
UNIQUE: Ensures that all the values in columns are unique. (Eg.
Roll no, Date of Birth, tshirt no of same team players etc.)
('P2','R. Sharma','Bat' ),
Insert into
Table ('P3','M.Dhoni','Wicket'),
('P4','J.Bumrah','Ball'),
('P5','K.Yadav','Ball');
How to display
• SELECT position,
pname from team;
• select * from team;
Delete a Member
update team
set pname='B.Kumar'
where position='P4';
);
Other tables can be created in the similar process.
Assignment
Create following two tables:
1) Find out the
Branch_name
Branch_name Loan_number amount where amount
>=2000.
Downtown L-170 3000
2) Find out the
Redwood L-230 4000 name of the
Perryridge L-260 1700 customer whose
loan_number is L-
170.
3) Delete the
Customer_name Loan_number customer whose
Jones L-170 loan_number is
155.
Smith L-230 4) Add gender
Hayes L-155 column in the
second table.
THANKYOU