Professional Documents
Culture Documents
CERTIFICATE
Prof. In-Charge
3 VIEWS
a) Create a view
b) Display the data in view
c) Update the data
d) Retrieve data using SELECT 12 06/09/23
e) Delete a view
f) Delete a record from a view
7 JOIN FUNCTIONS
a) Inner join
b) Left Outer Join 40 04/10/23
c) Right Outer Join
d) Cross Join
MYSQL Practical 1: Basics of MySQL
Program must include Description and Syntax before main code Starts
Code:
/* THE FOLLOWING COMMANDS WERE INPUTED IN THE MYSQL WORKBENCH
*/
C. Make use of ALTER and add new field gender with 1 limit of value.
D. Insert new value to gender.
E. Select and Display Id , name and salary column from above table.
F. Increment the salary of the customer named Ramesh by 10000 by using the WHERE
clause along with the UPDATE statement.
G. Drop the column Gender and display the values.
H. Delete the records of customers who are over 25 years of age.
I. Create another table orders with oid as integer not null, date as varchar not null, custid as
integer not null, amount as decimal with limit of 18 and 2.
J. Delete the table order.
Description:
1. SQL provides the CREATE TABLE statement to create a new table in a given
database. An SQL query to create a table must define the structure of a table. The
structure consists of the name of a table and names of columns in the table with each
column's data type. Note that each table must be uniquely named in a database.
2. SQL provides the INSERT INTO (table name) VALUES() statement to insert table
data in a given database. It must follow the order and restrictions specified in the
CREATE TABLE statement.
3. We use the COMMIT statement to save the data.
4. The ALTER statement is used to modify drop or add columns or data to the database.
5. To add values to the newly generated column GENDER, we used the
UPDATE EMP_DATA
SET GENDER = "M"
WHERE ID = 1;
Here the id is used to specify the location where the data needs to be added.
6. To select only particular column/columns, we use the statement SELECT x,y,z FROM
EMP_DATA(table name); where x,y,z are the columns we would like to see.
7. To delete the column ‘GENDER’ we used
ALTER TABLE EMP_DATA
DROP COLUMN GENDER;
8. To delete only the employees that where older than 25yrs from the table we used
DELETE FROM EMP_DATA WHERE AGE > 25;
The delete statement permanently deletes data from the table.
Syntax:
1. Following is the basic syntax of a SQL CREATE TABLE statement −
CREATE TABLE table_name(
column1 datatype,
column2 datatype,
column3 datatype,
.....
columnN datatype,
PRIMARY KEY( one or more columns )
);
• CREATE TABLE is the keyword telling the database system what you want to do. In
this case, you want to create a new table. The unique name or identifier for the table
follows the CREATE TABLE statement.
• Then in brackets comes the list defining each column in the table and what sort of
data type it is. The syntax becomes clearer with the following example.
2. The syntax of INSERT INTO statement is –
INSERT INTO table_name VALUES();
• Remember that characters in VARCHAR need to be inserted within ‘’ and the
numbers can be inserted without ‘’ .
3. The syntax of COMMIT is COMMIT.
4. The syntax of ALTER statement is –
ALTER TABLE table_name
function name column name column datatype(in some cases);
5. The syntax for update statement is –
UPDATE table_name
SET column_name = "value"
WHERE ID = position;
6. The rest of the syntax is in the Description itself.
Code:
USE EMP1;
CREATE TABLE EMP_DATA (
ID INT PRIMARY KEY,
EMPNAME VARCHAR(15) NOT NULL,
AGE INT(11) NOT NULL,
ADDRESS VARCHAR(25),
SALARY DECIMAL(18,2)
);
INSERT INTO EMP_DATA VALUES(1,'RAMESH',32,'AHMEDABAD',2000.00);
INSERT INTO EMP_DATA VALUES(2,'KHILAN',25,'DELHI',1500.00);
INSERT INTO EMP_DATA VALUES(3,'KAUSHIK',23,'KOTA',2000.00);
INSERT INTO EMP_DATA VALUES(4,'CHAITALI',25,'MUMBAI',6500.00);
INSERT INTO EMP_DATA VALUES(5,'HARDIK',27,'BHOPAL',8500.00);
INSERT INTO EMP_DATA VALUES(6,'KOMAL',22,'HYDERABAD',4500.00);
INSERT INTO EMP_DATA VALUES(7,'MUFFY',24,'INDORE',10000.00);
COMMIT
ALTER TABLE EMP_DATA
ADD GENDER VARCHAR(1);
UPDATE EMP_DATA
SET GENDER = "M"
WHERE ID = 1;
UPDATE EMP_DATA
SET GENDER = "M"
WHERE ID = 2;
UPDATE EMP_DATA
SET GENDER = "M"
WHERE ID = 3;
UPDATE EMP_DATA
SET GENDER = "F"
WHERE ID = 4;
UPDATE EMP_DATA
SET GENDER = "M"
WHERE ID = 5;
UPDATE EMP_DATA
SET GENDER = "F"
WHERE ID = 6;
UPDATE EMP_DATA
SET GENDER = "M"
WHERE ID = 7;
COMMIT
SELECT ID , EMPNAME, SALARY FROM EMP_DATA;
UPDATE EMP_DATA
SET SALARY = SALARY + 10000
WHERE ID = 1;
ALTER TABLE EMP_DATA
DROP COLUMN GENDER;
DELETE FROM EMP_DATA WHERE AGE > 25;
COMMIT
CREATE TABLE ORDERS (
OID INT NOT NULL PRIMARY KEY,
DATE VARCHAR(15) NOT NULL,
CUSTID INT(11) NOT NULL,
AMOUNT DECIMAL(18,2)
);
SELECT * FROM ORDERS;
DROP TABLE ORDERS;
Output:
MySQL Practical 3: Views
Aim:
Write a MySQL program and do the following:-
A. To create following table and describe structure
C.CREATE a VIEW CUSTOMERS_VIEW which must have all values from table.
D. Display all the values of created view.
E. CREATE another VIEW BUYERS_VIEW which must have all values from customer
table where salary is greater than 3000.
F. Updates the age of Ramesh to 35 in the above created CUSTOMERS_VIEW .
G. Retrieve the record with ID value 6 using the SELECT statement
H. updates the NAME and AGE column values in the CUSTOMERS_VIEW of the record
with ID value 3.
I. modify/update the age values of all the records in the CUSTOMERS_VIEW with
increment of 3.
J. CREATE VIEW CUSTOMERS_VIEW1,CUSTOMERS_VIEW2 And
CUSTOMERS_VIEW3 selecting all values from customers table.
K. Delete view Customers view1
L. Delete a record from the third_view created on the CUSTOMERS table created above
where age is equal to 22.
Description:
1. SQL provides the CREATE TABLE statement to create a new table in a given
database. An SQL query to create a table must define the structure of a table. The
structure consists of the name of a table and names of columns in the table with each
column's data type. Note that each table must be uniquely named in a database.
2. SQL provides the INSERT INTO (table name) VALUES() statement to insert table
data in a given database. It must follow the order and restrictions specified in the
CREATE TABLE statement.
4. The ALTER statement is used to modify drop or add columns or data to the database.
9. To delete only the record that had age = 22 yrs from the table we used DELETE
FROM CUSTOMERS_VIEW3 WHERE AGE = 22;
The delete statement permanently deletes data from the table.
However this did not change the VIEW as we had previously incremented the age,
making all the ages greater than 22.
Syntax:
1. Following is the basic syntax of a SQL CREATE TABLE statement −
CREATE TABLE table_name(
column1 datatype,
column2 datatype,
column3 datatype,
.....
columnN datatype,
PRIMARY KEY( one or more columns )
);
• CREATE TABLE is the keyword telling the database system what
you want to do. In this case, you want to create a new table. The
unique name or identifier for the table follows the CREATE
TABLE statement.
• Then in brackets comes the list defining each column in the table
and what sort of data type it is. The syntax becomes clearer with
the following example.
2. The syntax of INSERT INTO statement is –
INSERT INTO table_name VALUES();
• Remember that characters in VARCHAR need to be inserted within ‘’
and the numbers can be inserted without ‘’ .
3. The syntax of COMMIT is COMMIT.
4. The syntax of creating a VIEW is –
Q1. Perform following ASCII() function on string ‘SQL stands for Structured Query
Language.’
Description:
ASCII stands for American Standard Code for Information Interchange.
It assigns unique values for 128 alphabetic, numeric or special additional characters.
Here, the function returns the ASCII value of only the first character of the string. The
command used is –
SELECT STRINGS, ASCII(STRINGS) AS ASCII_Value
FROM STRINGCONTAINER
WHERE id = 1;
Syntax:
SELECT (column_name), ASCII(column_name) AS ASCII_Value
FROM (table_(name)
WHERE id = (id_number);
Code:
CREATE DATABASE PRACTICAL4;
USE PRACTICAL4;
CREATE TABLE STRINGCONTAINER (
ID INT PRIMARY KEY,
STRINGS VARCHAR(255)
);
INSERT INTO STRINGCONTAINER
VALUES ('SQL stands for Structured Query Language.');
SELECT STRINGS, ASCII(STRINGS) AS ASCII_Value
FROM STRINGCONTAINER
WHERE ID = 1;
Output:
Q2. Return the character for each integer.
Description:
The CHAR() function returns the ASCII code of the specified integer from 0 to 255. You can
also specify an expression that returns an integer value. The command used is –
SELECT CHAR(ASCII_Value) AS CHARACTERS
FROM STRINGCONTAINER
WHERE ID=1;
Syntax:
SELECT CHAR(column_name) AS CHARACTERS
FROM (table_name)
WHERE ID=(id_number);
Code:
CREATE DATABASE PRACTICAL4;
USE PRACTICAL4;
CREATE TABLE STRINGCONTAINER (
ID INT PRIMARY KEY,
STRINGS VARCHAR(255)
);
INSERT INTO STRINGCONTAINER
VALUES ('SQL stands for Structured Query Language.');
ALTER TABLE STRINGCONTAINER
ADD ASCII_VALUE INT;
UPDATE STRINGCONTAINER
SET ASCII_VALUE = ASCII(STRINGS)
WHERE ID = 1;
SELECT CHAR(ASCII_VALUE) AS CHARACTERS
FROM STRINGCONTAINER
WHERE ID=1;
SELECT CHAR(83) AS CHAR83
Output:
Q3. Return concatenate with separator for string ‘TCS and Company with separator’
Description:
Concatenate function is used to join two or more strings together. A separator is a type of
parameter which decides what should be between two or more strings.
Syntax:
CONCAT_WS(separator,input_string1,input_string2,[...input_stringN]);
Code:
SELECT CONCAT_WS(' AND ','TCS','COMPANY');
Output:
Q4. Return concatenated string SMART and POINT.
Description:
Concatenate function is used to join two or more strings together. A separator is a type of
parameter which decides what should be between two or more strings.
Syntax:
CONCAT(input_string1,input_string2,[...input_stringN]);
Code:
SELECT CONCAT('SMART','POINT');
Output:
Q5. Return the left 5 character from string SMARTPOINT.
Description:
The LEFT() function is used to SELECT a sub string from the left of given size or
characters.
Syntax:
SELECT LEFT(‘string’, location);
Code:
SELECT LEFT(‘SMARTPOINT’, 5);
Output:
Description:
The LENGTH() function is used to find out the length of a string.
Syntax:
SELECT LENGTH ('string');
Code:
SELECT LENGTH (' PHILLIPINES');
Output:
Description:
The LCASE() function is used to find out the lowercase version of a string.
Syntax:
SELECT LCASE ('string');
Code:
SELECT LCASE ('VARIABLES');
Output:
Q.8 Return the uppercase of the given string ALPHANIA.
Description:
The UCASE() function is used to find out the lowercase version of a string.
Syntax:
SELECT UCASE ('string');
Code:
SELECT UCASE ('alphania');
Output:
Description:
To replace the word "HELLO" with "HI" in the string "HELLO WORLD", you
can use the REPLACE() function.
In this query, the REPLACE() function takes three arguments:
• The original string ('HELLO WORLD').
• The string you want to replace ('HELLO').
• The string you want to replace it with ('HI').
This query replaces all occurrences of "HELLO" with "HI" in the input string, and the result
is "HI WORLD".
Syntax:
SELECT REPLACE(input_string,old_substring,newsubstring);
Code:
SELECT REPLACE('HELLO WORLD','HELLO','HI') AS REPLACED_STRING;
Output:
3. Write a MySQL command to use date format for 2025-05-21 with all options
available.
Description:
The DATE FORMAT() function returns the date in the specified format.
Formats specified to give the respective outcome are:
• W% -> gives the day of the week ( in words)
• M% -> gives the month of the specified date ( in words)
• Y% -> gives the year of the specified date (full year ex: 2023)
• %m -> gives the month in number
• %d -> gives the day in number
• %y -> gives the year (ex: 23)
• %j -> gives the days passed in the year
Code:
SELECT DATE_FORMAT('2025-05-21', '%W %M %Y') AS 'Formatted Date';
SELECT DATE_FORMAT('2025-05-21', '%m/%d/%Y') AS 'Formatted Date';
SELECT DATE_FORMAT('2025-05-21', '%d-%m-%y') AS 'Formatted Date';
SELECT DATE_FORMAT('2025-05-21', '%j') AS 'Formatted Date';
Output:
4. Write a MySQL command to add date with 10 day , 5 month and 6 year on 2022-06-
12.
Description:
Here we used DATE_ADD function which can be used to increase the day, month, or
year respectively.
Code:
SELECT DATE_ADD(DATE_ADD(DATE_ADD('2022-06-12', INTERVAL 10
DAY), INTERVAL 5 MONTH), INTERVAL 6 YEAR) AS 'Date after 6 yrs 5
months and 10 days';
Output:
5. Write a MySQL command to display name of day, day of month , day of week, day of
year for 2021-05-21.
Description:
Here we used the following functions:
• DAYNAME() – It returns the name of the given day.
• DAYOFMONTH() – It returns the day of the month.
• DAYOFWEEK() – It returns the weekday index of the argument.
• DAYOFYEAR() – It returns the day of the year.
Code:
SELECT DAYNAME('2021-05-21') AS 'Day Name',
DAYOFMONTH('2021-05-21') AS 'Day of Month',
DAYOFWEEK('2021-05-21') AS 'Day of Week',
DAYOFYEAR('2021-05-21') AS 'Day of Year';
Output:
6. Write a MySQL command to display sysdate and utc date
Description:
The SYSDATE() function returns the system date when the function gets executed
whereas the UTC_DATE() function returns the current UTC date
Code:
SELECT SYSDATE() AS 'System Date',
UTC_DATE() AS 'UTC Date';
Output:
Output:
2. ACOS() function:
Description:
It returns the arc cosine
Code:
SELECT ACOS(0) AS 'OUTPUT';
Output:
3. ASIN() function:
Description:
It returns the arc sine
Code:
SELECT ASIN(0) AS 'OUTPUT';
Output:
4. ATAN() function:
Description:
It returns the arc tangent
Code:
SELECT ATAN(1) AS 'OUTPUT';
Output:
5. ATAN2(),ATAN() function:
Description:
It returns the arc tangent of the two arguments
Code:
SELECT ATAN2(0),ATAN(1) AS 'OUTPUT';
Output:
6. CEIL() function:
Description:
It returns the smallest integer value not less than the argument
Code:
SELECT CEIL(10.9) AS 'OUTPUT';
Output:
7. CEILING() function:
Description:
It returns the smallest integer value not less than the argument
Code:
SELECT CEILING(10.9) AS 'OUTPUT';
Output:
8. CONV() function:
Description:
It convert numbers between different number bases.
It’s syntax is CONV(number, from_base, to_base)
Code:
SELECT CONV(10,2,5) AS 'OUTPUT';
Output:
9. SIN() function:
Description:
It returns the sine
Code:
SELECT SIN(90) AS 'OUTPUT';
Output:
USE PRACTICALASS;
Name VARCHAR(255),
Department VARCHAR(20),
Salary INT,
Gender VARCHAR(10),
Age INT,
City VARCHAR(255)
);
Title VARCHAR(255),
ClientId INT,
EmployeeId INT,
StartDate DATETIME,
EndDate DATETIME
);
1. Inner Join
DESCRIPTION:
SQL provides the INNER JOIN statement to join value into give Table
Syntax:
Following is the basic syntax of a SQL INNER JOIN statement −
SELECT column_name(s)
FROM table1
INNER JOIN table2
ON table1.column_name = table2.column_name
Code:
SELECT EmployeeId,Name,Department,City,Title,ClientId FROM EMPDATA
INNER JOIN PROJECTS ON EMPDATA.Id=PROJECTS.EmployeeId ORDER BY
EmployeeId;
Output:
2. Outer Join (left and right):
Syntax:
Following is the basic syntax of a SQL LEFT JOIN statement −
SELECT column_name(s)
FROM table1
LEFT JOIN table2
ON table1.column_name = table2.column_name;
Code:
Syntax
Following is the basic syntax of Right Join in SQL −
SELECT table1.column1, table2.column2...
FROM table1
RIGHT JOIN table2
ON table1.common_field = table2.common_field;
Code:
Output:
3. For Full join:
DESCRIPTION:
We need to use the UNION of the RIGHT JOIN and LEFT JOIN statement to Full
join value into give Table.
Syntax:
SELECT * FROM table1 LEFT JOIN PROJECTS ON
table1.columnX=table2.columnY
UNION
SELECT * FROM table1 RIGHT JOIN PROJECTS ON
table1.columnX=table2.columnY;
Code:
SELECT * FROM EMPDATA LEFT JOIN PROJECTS ON
EMPDATA.Id=PROJECTS.EmployeeId
UNION
SELECT * FROM EMPDATA RIGHT JOIN PROJECTS ON
EMPDATA.Id=PROJECTS.EmployeeId;
Output:
4. For Cross join:
DESCRIPTION: SQL provides the CROSS JOIN statement to join value into give
Table
Syntax
Following is the basic syntax of the Cross Join query in SQL −
SELECT column_name(s)
FROM table1
CROSS JOIN table
Code:
Output: