0% found this document useful (0 votes)
4 views26 pages

Step-by-Step Instructions in Linux

Uploaded by

2024ca73d
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views26 pages

Step-by-Step Instructions in Linux

Uploaded by

2024ca73d
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 26

🖥️ Step-by-Step Instructions in Linux

✅ Step 1: Start MySQL


bash
CopyEdit
sudo systemctl start mysql

✅ Step 2: Log in to MySQL


bash
CopyEdit
mysql -u root -p

(Enter your MySQL root password when prompted)

🗃️ Step 3: Create a Database and Use It


sql
CopyEdit
CREATE DATABASE company;

Untitled 1
USE company;

📄 Step 4: Create Tables


sql
CopyEdit
CREATE TABLE EMP (
E_No INT PRIMARY KEY,
ename VARCHAR(50),
Address VARCHAR(100)
);

CREATE TABLE DEPT (


Dept_no VARCHAR(10),
D_name VARCHAR(50),
E_no INT,
FOREIGN KEY (E_no) REFERENCES EMP(E_No)
);

✏️ Step 5: Insert Data


sql
CopyEdit
INSERT INTO EMP VALUES
(1, 'RAM', 'PUNE'),
(2, 'HARI', 'NAVIMUMBAI'),
(3, 'GOPAL', 'NAVIMUMBAI'),
(4, 'VARUN', 'DELHI'),
(5, 'NITIN', 'DELHI');

INSERT INTO DEPT VALUES


('D1', 'HR', 1),
('D2', 'IT', 2),

Untitled 2
('D3', 'MRKT', 4);

🔍Department
Step 6: SQL Query to Find Employees Working in HR

sql
CopyEdit
SELECT EMP.ename
FROM EMP
INNER JOIN DEPT ON EMP.E_No = DEPT.E_no
WHERE DEPT.D_name = 'HR';

🟢 Output Expected:
markdown
CopyEdit
ename
------
RAM

🔁 1. INNER JOIN
Get employees who are assigned to a department:

sql
CopyEdit
SELECT EMP.E_No, EMP.ename, DEPT.D_name
FROM EMP
INNER JOIN DEPT ON EMP.E_No = DEPT.E_no;

🟢 Expected Output:
markdown
CopyEdit
E_No | ename | D_name

Untitled 3
-----------------------
1 | RAM | HR
2 | HARI | IT
4 | VARUN | MRKT

⬅️ 2. LEFT JOIN
Get all employees, including those who are not assigned to any department:

sql
CopyEdit
SELECT EMP.E_No, EMP.ename, DEPT.D_name
FROM EMP
LEFT JOIN DEPT ON EMP.E_No = DEPT.E_no;

🟢 Expected Output:
markdown
CopyEdit
E_No | ename | D_name
-----------------------
1 | RAM | HR
2 | HARI | IT
3 | GOPAL | NULL
4 | VARUN | MRKT
5 | NITIN | NULL

➡️ 3. RIGHT JOIN
Get all departments, including those that are not assigned to any employee
(though not applicable here since all DEPT entries have valid E_no ):

sql
CopyEdit
SELECT EMP.E_No, EMP.ename, DEPT.D_name

Untitled 4
FROM EMP
RIGHT JOIN DEPT ON EMP.E_No = DEPT.E_no;

🟢 Expected Output:
markdown
CopyEdit
E_No | ename | D_name
-----------------------
1 | RAM | HR
2 | HARI | IT
4 | VARUN | MRKT

🔄 4. FULL OUTER JOIN


Note: MySQL does not support FULL OUTER JOIN directly, but you can
emulate it using UNION .

sql
CopyEdit
SELECT EMP.E_No, EMP.ename, DEPT.D_name
FROM EMP
LEFT JOIN DEPT ON EMP.E_No = DEPT.E_no

UNION

SELECT EMP.E_No, EMP.ename, DEPT.D_name


FROM EMP
RIGHT JOIN DEPT ON EMP.E_No = DEPT.E_no;

🟢 Expected Output:
markdown
CopyEdit
E_No | ename | D_name

Untitled 5
-----------------------
1 | RAM | HR
2 | HARI | IT
3 | GOPAL | NULL
4 | VARUN | MRKT
5 | NITIN | NULL

✅Commands
Part 1: DDL (Data Definition Language)
with Constraints
🔹Step 1: Start MySQL
bash
CopyEdit
sudo systemctl start mysql
mysql -u root -p

🔹Step 2: Create a database


sql
CopyEdit
CREATE DATABASE college;

Untitled 6
USE college;

🔹Step 3: Create a table with all constraints


sql
CopyEdit
CREATE TABLE Student (
StudentID INT PRIMARY KEY,
Name VARCHAR(50) NOT NULL,
Age INT CHECK (Age >= 18),
Email VARCHAR(100) UNIQUE
);

🔹Step 4: Alter table – Add a column


sql
CopyEdit
ALTER TABLE Student ADD Gender VARCHAR(10);

🔹Step 5: Rename table


sql
CopyEdit
RENAME TABLE Student TO Student_Info;

🔹Step 6: Truncate table (deletes all data)


sql
CopyEdit
TRUNCATE TABLE Student_Info;

Untitled 7
🔹Step 7: Delete table
sql
CopyEdit
DROP TABLE Student_Info;

✅ Part 2: DML, DCL, and TCL Commands


📌 Create the Student Table Again

sql
CopyEdit
CREATE TABLE Student (
StudentID INT PRIMARY KEY,
Name VARCHAR(50),
Age INT
);

📌 Insert Data
sql
CopyEdit
INSERT INTO Student VALUES (1, 'Ravi', 20);
INSERT INTO Student VALUES (2, 'Sneha', 21);
INSERT INTO Student VALUES (3, 'Arjun', 22);

Untitled 8
📌 Select Data
SELECT * FROM Student;

📌 Update a Record
UPDATE Student SET Age = 23 WHERE StudentID = 3;

📌 Delete a Record
sql
CopyEdit
DELETE FROM Student WHERE StudentID = 2;

🔐 DCL (Data Control Language)


📌 Create a New User
sql
CopyEdit
CREATE USER 'testuser'@'localhost' IDENTIFIED BY 'testpass';

📌 Grant Privileges
sql
CopyEdit
GRANT SELECT, INSERT ON college.Student TO 'testuser'@'localhost';

Untitled 9
📌 Revoke Privileges
sql
CopyEdit
REVOKE INSERT ON college.Student FROM 'testuser'@'localhost';

🔁 TCL (Transaction Control Language)


📌 Using Transactions
sql
CopyEdit
START TRANSACTION;

INSERT INTO Student VALUES (4, 'Kiran', 19);


UPDATE Student SET Name = 'Ravi Kumar' WHERE StudentID = 1;

ROLLBACK; -- or use COMMIT to save

Perfect! Based on the instruction in the image, here is the step-by-step guide
to:

Create tables: Student , College , Library

Create a view on them

Use SHOW , EXEC commands

Take backup of the database

Untitled 10
Use Linux with MySQL (CLI)

✅ Step-by-Step Instructions (Linux + MySQL)


🔹 Step 1: Start MySQL in Linux
bash
CopyEdit
sudo systemctl start mysql
mysql -u root -p

🔹 Step 2: Create a Database and Use It


sql
CopyEdit
CREATE DATABASE my_institute;
USE my_institute;

🔹 Step 3: Create Tables


sql
CopyEdit
CREATE TABLE Student (
StudentID INT PRIMARY KEY,
Name VARCHAR(50),
Age INT
);

CREATE TABLE College (


CollegeID INT PRIMARY KEY,
CollegeName VARCHAR(100),
Location VARCHAR(100)
);

Untitled 11
CREATE TABLE Library (
LibraryID INT PRIMARY KEY,
BookName VARCHAR(100),
StudentID INT,
FOREIGN KEY (StudentID) REFERENCES Student(StudentID)
);

🔹 Step 4: Insert Sample Data


sql
CopyEdit
INSERT INTO Student VALUES (1, 'Ravi', 20), (2, 'Sneha', 21);
INSERT INTO College VALUES (101, 'IGCE', 'Ghansoli'), (102, 'XYZ College',
'Mumbai');
INSERT INTO Library VALUES (1, 'DBMS Concepts', 1), (2, 'OS Made Easy',
2);

🔹 Step 5: Create a View


sql
CopyEdit
CREATE VIEW StudentLibraryView AS
SELECT s.StudentID, s.Name, l.BookName
FROM Student s
JOIN Library l ON s.StudentID = l.StudentID;

🔹 Step 6: Use SHOW and EXEC commands

sql
CopyEdit
-- Show all databases
SHOW DATABASES;

Untitled 12
-- Show all tables in current database
SHOW TABLES;

-- Show structure of a table


DESC Student;

-- Show views
SHOW FULL TABLES WHERE Table_type = 'VIEW';

-- Execute a view
SELECT * FROM StudentLibraryView;

(Note: EXEC is not a MySQL command; it's used in SQL Server. In MySQL, you
just run queries directly.)

🔹 Step 7: Take Backup of Database (Linux Shell)


Exit MySQL with exit , then run in Linux shell:

bash
CopyEdit
mysqldump -u root -p my_institute > /home/yourusername/database_back
up.sql

📁 Replace /home/yourusername/ with your actual Linux path or the one given in
your question like /sigce/database .

✅ Final: Folder Path Example


bash
CopyEdit
mkdir -p /sigce/database
mv /home/yourusername/database_backup.sql /sigce/database/

Untitled 13
🚀 Step-by-Step in Linux (MySQL)
🔹 Step 1: Start MySQL Server in Linux Terminal
bash
CopyEdit
sudo systemctl start mysql
mysql -u root -p

🔹 Step 2: Create Database and Table


sql
CopyEdit
CREATE DATABASE company;
USE company;

CREATE TABLE Employee (


EmpID INT PRIMARY KEY,
EmpName VARCHAR(50),
Salary DECIMAL(10,2),
DepartmentID INT
);

INSERT INTO Employee VALUES

Untitled 14
(1, 'Ravi', 50000, 10),
(2, 'Sneha', 60000, 10),
(3, 'Aman', 45000, 20),
(4, 'Priya', 55000, 20);

🔹 Step 3: Create Stored Function – GetTotalSalary

sql
CopyEdit
DELIMITER $$

CREATE FUNCTION GetTotalSalary(deptID INT)


RETURNS DECIMAL(10,2)
DETERMINISTIC
BEGIN
DECLARE total DECIMAL(10,2);
SELECT SUM(Salary) INTO total
FROM Employee
WHERE DepartmentID = deptID;
RETURN total;
END$$

DELIMITER ;

🔹 Step 4: Call the Function


sql
CopyEdit
SELECT GetTotalSalary(10) AS TotalSalaryForDept10;
SELECT GetTotalSalary(20) AS TotalSalaryForDept20;

Untitled 15
.

🔧 Step-by-Step in Linux Terminal (MySQL)


🔹 Step 1: Start MySQL and Login
bash
CopyEdit
sudo systemctl start mysql
mysql -u root -p

🔹 Step 2: Create Database and Table


sql
CopyEdit
CREATE DATABASE company;
USE company;

CREATE TABLE Employee (


empId INT PRIMARY KEY,
ename VARCHAR(50),
esalary DECIMAL(10,2)
);

Untitled 16
🔹 Step 3: Insert 5 Sample Rows
sql
CopyEdit
INSERT INTO Employee VALUES
(1, 'Ravi Kumar', 50000),
(2, 'Sneha Mehta', 65000),
(3, 'Amit Roy', 45000),
(4, 'Priya Singh', 72000),
(5, 'Karan Patil', 55000);

🔹 Step 4: Aggregate Functions


sql
CopyEdit
-- Count total employees
SELECT COUNT(*) AS TotalEmployees FROM Employee;

-- Sum of salaries
SELECT SUM(esalary) AS TotalSalary FROM Employee;

-- Maximum salary
SELECT MAX(esalary) AS HighestSalary FROM Employee;

-- Minimum salary
SELECT MIN(esalary) AS LowestSalary FROM Employee;

-- Average salary
SELECT AVG(esalary) AS AverageSalary FROM Employee;

🔹 Step 5: Simple Queries


sql
CopyEdit
-- Get all employees with salary > 50000

Untitled 17
SELECT * FROM Employee WHERE esalary > 50000;

-- Get names of employees in alphabetical order


SELECT ename FROM Employee ORDER BY ename ASC;

🔹 Step 6: String Manipulation Examples


sql
CopyEdit
-- Convert names to uppercase
SELECT UPPER(ename) AS UpperName FROM Employee;

-- Display length of each name


SELECT ename, LENGTH(ename) AS NameLength FROM Employee;

-- Concatenate name and salary


SELECT CONCAT(ename, ' earns Rs. ', esalary) AS SalaryInfo FROM Emplo
yee;

-- Extract first name (before space)


SELECT SUBSTRING_INDEX(ename, ' ', 1) AS FirstName FROM Employee;

✅ Aim
Write a program to implement Triggers using PL/SQL.
When data is deleted from the MAIN table, the deleted row should be
automatically inserted into the BACKUP table using an AFTER DELETE Trigger.

🛠️ Step-by-step MySQL in Linux Terminal


🔹 Step 1: Start MySQL and Login
Untitled 18
bash
CopyEdit
sudo systemctl start mysql
mysql -u root -p

🔹 Step 2: Create Database and Use It


sql
CopyEdit
CREATE DATABASE company;
USE company;

🔹 Step 3: Create MAIN Table and Insert Data

sql
CopyEdit
CREATE TABLE MAIN (
ID INT PRIMARY KEY,
Salary DECIMAL(10,2)
);

INSERT INTO MAIN VALUES


(1, 10000),
(2, 20000),
(3, 30000),
(4, 40000);

🔹 Step 4: Create BACKUP Table

sql
CopyEdit
CREATE TABLE BACKUP (

Untitled 19
ID INT,
Salary DECIMAL(10,2),
Deleted_At TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

🔹 Step 5: Create Trigger for AFTER DELETE


sql
CopyEdit
DELIMITER //

CREATE TRIGGER after_delete_backup


AFTER DELETE ON MAIN
FOR EACH ROW
BEGIN
INSERT INTO BACKUP (ID, Salary) VALUES (OLD.ID, OLD.Salary);
END;
//

DELIMITER ;

🔹 Step 6: Test the Trigger


sql
CopyEdit
-- Delete a record from MAIN
DELETE FROM MAIN WHERE ID = 2;

-- Check BACKUP table


SELECT * FROM BACKUP;

Untitled 20
🧾 What the question asks:
Write a PHP program to connect to a MySQL database using:

servername = "localhost"

username = "username"

password = "password"

It should display:

"DBMS connection connected"

✅ Step-by-Step Guide on Ubuntu


Step 1: Install Required Software
Open your terminal (Ctrl + Alt + T) and run:

bash
CopyEdit
sudo apt update
sudo apt install apache2 php libapache2-mod-php mysql-server php-mysq
l

This installs:

Apache (web server)

PHP

MySQL (backend)

PHP extension to talk to MySQL

Step 2: Start Apache and MySQL Services

Untitled 21
bash
CopyEdit
sudo systemctl start apache2
sudo systemctl start mysql
sudo systemctl enable apache2
sudo systemctl enable mysql

Step 3: Secure MySQL and Create a User (Optional but


Recommended)

bash
CopyEdit
sudo mysql_secure_installation

When prompted:

Press Enter (set root password? No)

Remove anonymous users? Yes

Disallow root remote login? Yes

Remove test DB? Yes

Reload privilege tables? Yes

Now create the user username and give access:

bash
CopyEdit
sudo mysql -u root -p

In MySQL prompt:

sql
CopyEdit
CREATE USER 'username'@'localhost' IDENTIFIED BY 'password';

Untitled 22
GRANT ALL PRIVILEGES ON *.* TO 'username'@'localhost';
FLUSH PRIVILEGES;
EXIT;

Step 4: Write the PHP Code


Create a file named connect.php in your Apache server directory:

bash
CopyEdit
sudo nano /var/www/html/connect.php

Paste this code:

php
CopyEdit
<?php
$servername = "localhost";
$username = "username";
$password = "password";

// Create connection
$conn = new mysqli($servername, $username, $password);

// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "DBMS connection connected";
?>

Save and close ( Ctrl + O , Enter, then Ctrl + X )

Step 5: Run the PHP File in Your Browser


Open your browser and go to:

Untitled 23
arduino
CopyEdit
http://localhost/connect.php

You should see:

DBMS connection connected

✅ Summary Checklist
Step Task Command/Action

1 Install Apache, PHP, MySQL sudo apt install ...

2 Start services systemctl start

3 Secure MySQL + create user mysql_secure_installation + SQL queries

4 Write connect.php Use nano /var/www/html/connect.php

5 Run in browser Visit http://localhost/connect.php

If you want, I can also help you:

Create a sample table and insert data

Build a frontend form (HTML) to insert data into MySQL

Fetch/display MySQL data using PHP

Just let me know what you're curious about next!


4o

You said:
sudo nano /var/www/html/connect.php this command and its code to be run in
the linux terminal only?

ChatGPT said:
Great question! Let's break it down so you clearly understand:

🟡 sudo nano /var/www/html/connect.php

Untitled 24
✅ Yes, this command is meant to be run in the Linux terminal.
Here's what it's doing:

sudo : Run as superuser (needed to write in the /var/www/html/ directory, which


is protected).

nano : Opens the Nano text editor (terminal-based).

/var/www/html/connect.php : This is the full path to the PHP file you'll create.
Apache serves files from /var/www/html , so if you save your PHP file here, you
can access it via http://localhost/connect.php .

🧾 After running that command:


You’ll be inside the nano editor. Paste the code below inside nano (you can
right-click to paste if you're using a terminal):

php
CopyEdit
<?php
$servername = "localhost";
$username = "username";
$password = "password";

// Create connection
$conn = new mysqli($servername, $username, $password);

// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "DBMS connection connected";
?>

✅ How to Save and Exit Nano:


Press Ctrl + O (letter O) → then hit Enter to save.

Press Ctrl + X to exit nano.

Untitled 25
📍 Then, in your browser (not terminal), go to:
arduino
CopyEdit
http://localhost/connect.php

And you'll see the output.

Untitled 26

You might also like