Warning: Undefined array key "amp-addthis" in /home/tgagmvup/onlinestudy.guru/wp-content/plugins/addthis/backend/AddThisSharingButtonsFeature.php on line 101
lang="en-US"> Database operations - onlinestudy.guru
Site icon onlinestudy.guru

Database operations

Certainly! Here’s a comprehensive guide to managing a database, starting from its creation and covering table management, data manipulation, querying, data entry forms, and report generation.


Comprehensive Database Management Guide

1. Creating and Managing a Database

1.1 Creating a Database

To create a new database, use the CREATE DATABASE command.

Syntax:

CREATE DATABASE database_name;

Example:

CREATE DATABASE university;

1.2 Selecting a Database

To start working with the newly created database, select it.

Syntax:

USE database_name;

Example:

USE university;

2. Creating and Modifying Tables

2.1 Creating a Table

To create a table in the database, use the CREATE TABLE command.

Syntax:

CREATE TABLE table_name (
    column1 datatype constraints,
    column2 datatype constraints,
    ...
);

Example:

CREATE TABLE Students (
    StudentID INT PRIMARY KEY,
    Name VARCHAR(100),
    Email VARCHAR(100),
    Major VARCHAR(50)
);

CREATE TABLE Courses (
    CourseID INT PRIMARY KEY,
    CourseName VARCHAR(100),
    Instructor VARCHAR(100)
);

CREATE TABLE Enrollments (
    EnrollmentID INT PRIMARY KEY,
    StudentID INT,
    CourseID INT,
    EnrollmentDate DATE,
    FOREIGN KEY (StudentID) REFERENCES Students(StudentID),
    FOREIGN KEY (CourseID) REFERENCES Courses(CourseID)
);

2.2 Modifying Table Structure

Adding a New Column

Syntax:

ALTER TABLE table_name
ADD column_name datatype constraints;

Example:

ALTER TABLE Students
ADD DateOfBirth DATE;

Modifying an Existing Column

Syntax:

ALTER TABLE table_name
MODIFY column_name new_datatype new_constraints;

Example:

ALTER TABLE Students
MODIFY Email VARCHAR(150);

Renaming a Column

Syntax (MySQL):

ALTER TABLE table_name
CHANGE old_column_name new_column_name datatype;

Syntax (SQL Server):

EXEC sp_rename 'table_name.old_column_name', 'new_column_name', 'COLUMN';

Example (MySQL):

ALTER TABLE Students
CHANGE Major MajorField VARCHAR(50);

Dropping a Column

Syntax:

ALTER TABLE table_name
DROP COLUMN column_name;

Example:

ALTER TABLE Students
DROP COLUMN DateOfBirth;

2.3 Dropping a Table

Syntax:

DROP TABLE table_name;

Example:

DROP TABLE Enrollments;

3. Manipulating Data

3.1 Inserting Data

To add new rows to a table, use the INSERT INTO command.

Syntax:

INSERT INTO table_name (column1, column2, ...)
VALUES (value1, value2, ...);

Example:

INSERT INTO Students (StudentID, Name, Email, Major)
VALUES (1, 'Alice Smith', 'alice@example.com', 'Biology');

INSERT INTO Courses (CourseID, CourseName, Instructor)
VALUES (101, 'Introduction to Biology', 'Dr. Green');

INSERT INTO Enrollments (EnrollmentID, StudentID, CourseID, EnrollmentDate)
VALUES (1, 1, 101, '2024-08-15');

3.2 Updating Data

To modify existing records, use the UPDATE command.

Syntax:

UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition;

Example:

UPDATE Students
SET Email = 'alice.smith@example.com'
WHERE StudentID = 1;

3.3 Deleting Data

To remove records from a table, use the DELETE command.

Syntax:

DELETE FROM table_name
WHERE condition;

Example:

DELETE FROM Students
WHERE StudentID = 1;

4. Querying Data

4.1 Selecting Data

To retrieve data from a table, use the SELECT statement.

Syntax:

SELECT column1, column2, ...
FROM table_name
WHERE condition;

Example:

SELECT Name, Email
FROM Students
WHERE Major = 'Biology';

4.2 Joining Tables

To combine data from multiple tables, use JOIN.

Syntax:

SELECT column1, column2, ...
FROM table1
JOIN table2 ON table1.common_column = table2.common_column
WHERE condition;

Example:

SELECT Students.Name, Courses.CourseName
FROM Enrollments
JOIN Students ON Enrollments.StudentID = Students.StudentID
JOIN Courses ON Enrollments.CourseID = Courses.CourseID;

5. Data Entry Forms

Data entry forms are typically part of a user interface application. They allow users to input data, which is then sent to the database.

Steps to Create a Data Entry Form:

  1. Design the Form: Use HTML or a form-building tool to design the form.
  2. Capture Input: Collect user input through form fields.
  3. Submit Data: Use server-side scripts (like PHP or Python) to process the input and execute SQL commands.

Example (Web Form Submission in PHP):

<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $name = $_POST['name'];
    $email = $_POST['email'];
    $major = $_POST['major'];

    $conn = new mysqli('localhost', 'username', 'password', 'university');

    $sql = "INSERT INTO Students (Name, Email, Major)
            VALUES ('$name', '$email', '$major')";

    if ($conn->query($sql) === TRUE) {
        echo "New record created successfully";
    } else {
        echo "Error: " . $sql . "<br>" . $conn->error;
    }

    $conn->close();
}
?>

6. Generating Reports

Reports can be generated using SQL queries and reporting tools.

6.1 Simple SQL Reporting

Syntax:

SELECT column1, column2, ...
FROM table_name
WHERE condition
ORDER BY column1;

Example:

SELECT Name, Salary
FROM Employees
ORDER BY Salary DESC;

6.2 Using Reporting Tools

Reporting tools (such as Microsoft SQL Server Reporting Services, Crystal Reports) can help create more complex reports with features like grouping and charting.

Example Tool Workflow:

  1. Connect to Database: Set up the connection to your database.
  2. Design Report: Use the tool’s interface to layout and format the report.
  3. Generate and Export: Produce the report and export it in formats like PDF, Excel, or HTML.

This guide provides a structured approach to managing a database, from creation and table management to data manipulation, querying, and reporting. Each section includes clear syntax and examples to facilitate understanding and implementation.

Exit mobile version