What is Database
• A database is a structured collection of data that is organized and stored
in a way that enables efficient retrieval, management, and manipulation
of that data.
• In the context of computing and information technology, a database
serves as a centralized repository for storing and managing various types
of data.
• Databases are used in various applications and industries to store and
manage different types of data, such as customer information, financial
records, inventory data, medical records, and more.
• They serve as the backbone of many software systems, enabling efficient
data management and retrieval for applications ranging from simple
websites to complex enterprise solutions.
4/4/2024 Prepared By : Basanta Chapagain 109
What is database table
• In a relational database, a table is a fundamental and
structured representation of data.
• It consists of rows (also known as records or tuples) and columns
(also known as fields) that organize and store specific types of
information.
• Each row represents a single instance of data, and each column
represents a specific attribute or characteristic of that data.
• Tables are the primary means of organizing and storing data in a
relational database.
4/4/2024 Prepared By : Basanta Chapagain 110
Database Table Example
4/4/2024 Prepared By : Basanta Chapagain 111
Database Table – Multiple with Relation
4/4/2024 Prepared By : Basanta Chapagain 112
Some terms of database
• Rows (Records): Each row in a table corresponds to a specific
piece of data or a specific entity. For example, in a table
representing customers, each row might represent an individual
customer.
• Columns (Fields): Columns define the attributes or properties of
the data stored in the table. Each column has a specific data
type that determines the kind of data it can hold. For example, a
"Name" column might hold character strings, while a "Date of
Birth" column might hold date values.
4/4/2024 Prepared By : Basanta Chapagain 113
Some terms of database
• Primary Key: A primary key is a column or a set of columns in a
table that uniquely identifies each row. It ensures that each row
in the table has a unique identifier. Common examples of
primary keys are ID numbers or usernames.
• Foreign Key: A foreign key is a column in a table that establishes
a relationship between that table and another table. It refers to
the primary key of another table, creating a connection
between related data.
4/4/2024 Prepared By : Basanta Chapagain 114
Some terms of database
• Data Types: Each column in a table is assigned a specific data
type, such as INTEGER, VARCHAR (variable-length character
string), DATE, BOOLEAN, etc. Data types define the kind of values
that can be stored in each column.
• Queries: SQL queries are used to interact with database tables.
You can perform various operations, such as retrieving data,
inserting new records, updating existing records, and deleting
records, using SQL statements.
4/4/2024 Prepared By : Basanta Chapagain 115
SQL-Structured query language
• SQL, or Structured Query Language, is a domain-specific language used for
managing and manipulating relational databases.
• It provides a standardized way to interact with databases, perform various
operations like querying, inserting, updating, and deleting data, and defining
the structure of the data itself
Types:
• Data Definition Language (DDL):
• Data Manipulation Language (DML):
• Data Control Language (DCL):
• Transaction Control Language (TCL):
4/4/2024 Prepared By : Basanta Chapagain 116
Data Definition Language (DDL)
• DDL deals with defining and managing the structure of the database. It
includes creating, altering, and dropping database objects like tables,
indexes, views, and more.
• Common DDL commands include:
– CREATE TABLE: Defines a new table and its columns.
– ALTER TABLE: Modifies an existing table's structure.
– DROP TABLE: Removes a table from the database.
– CREATE INDEX: Creates an index on one or more columns to improve
query performance.
– CREATE VIEW: Defines a virtual table based on a query's result set.
4/4/2024 Prepared By : Basanta Chapagain 117
Data Manipulation Language (DML)
• DML is used to manipulate the data stored within the database.
It includes operations like inserting, updating, and deleting data,
as well as querying the data.
• Common DML commands include:
– SELECT: Retrieves data from one or more tables based on specified
conditions.
– INSERT INTO: Adds new rows of data into a table.
– UPDATE: Modifies existing data in a table.
– DELETE FROM: Removes rows of data from a table.
4/4/2024 Prepared By : Basanta Chapagain 118
Data Control Language (DCL)
• DCL focuses on controlling access to the database and its
objects. It deals with permissions, security, and user roles.
• Common DCL commands include:
– GRANT: Provides specific privileges to users or roles.
– REVOKE: Removes specific privileges from users or roles.
4/4/2024 Prepared By : Basanta Chapagain 119
Transaction Control Language (TCL)
• TCL commands are used to manage transactions in a database.
Transactions ensure the integrity and consistency of the data
during multiple operations.
• Common TCL commands include:
– COMMIT: Confirms the changes made within a transaction.
– ROLLBACK: Reverts the changes made within a transaction.
– SAVEPOINT: Creates a point within a transaction to which you can later roll
back.
4/4/2024 Prepared By : Basanta Chapagain 120
Some Common Data types supported by SQL
• Numeric types (e.g., INTEGER, FLOAT)
• Character string types (e.g., VARCHAR, CHAR)
• Date and time types (e.g., DATE, TIME, TIMESTAMP)
• Boolean type (e.g., BOOLEAN)
• Binary data types (e.g., BLOB, BINARY)
• Enumerated types (e.g., ENUM)
• JSON data type (e.g., JSON)
• Array types (e.g., ARRAY)
Each relational database management system (RDBMS), like MySQL, PostgreSQL, SQL
Server, Oracle, etc., may have specific data types and syntax variations, but the core
concepts of SQL remain consistent.
4/4/2024 Prepared By : Basanta Chapagain 121
Create table using SQL
• To create a table using SQL in MySQL, you would use the
`CREATE TABLE` statement.
CREATE TABLE table_name (
column1 datatype,
column2 datatype,
column3 datatype,
...
);
Replace `table_name` with the name you want to give to your table. Then, for each column you
want to add, provide the column name and its corresponding data type.
4/4/2024 Prepared By : Basanta Chapagain 122
Create table using SQL-Example
• Here's an example of creating a simple table named `employees` with a few columns:
CREATE TABLE employees (
employee_id INT PRIMARY KEY,
first_name VARCHAR(50),
last_name VARCHAR(50),
birth_date DATE,
hire_date DATE
In this example:
); - `employee_id` is an integer column and is designated as the primary key.
- `first_name` and `last_name` are variable-length character string columns.
- `birth_date` and `hire_date` are date columns.
4/4/2024 Prepared By : Basanta Chapagain 123
Add new record – Insert Query
• To insert records into the `employees` table that you created earlier, you can use the
`INSERT INTO` statement. Here's the basic syntax along with an example:
INSERT INTO table_name (column1, column2, column3, ...) VALUES (value1, value2,
value3, ...);
• Replace `table_name` with the name of your table, and for each column, provide the
corresponding values you want to insert.
• Here's an example of inserting a record into the `employees` table:
INSERT INTO employees (employee_id, first_name, last_name, birth_date, hire_date)
VALUES (1, ‘Rajesh', ‘Dai', ‘1000-1-5', ‘2000-05-12');
• This example inserts a new employee record with the provided values into the
`employees` table. You can continue using similar `INSERT INTO` statements to add
more records as needed.
4/4/2024 Prepared By : Basanta Chapagain 124
Select Record- SQL syntax
• To retrieve data from the `employees` table using a `SELECT`
query in MySQL, you can use the following syntax:
SELECT column1, column2, column3, ...
FROM table_name
WHERE condition;
• Replace `column1, column2, column3, ...` with the names of the
columns you want to retrieve, `table_name` with the name of
your table, and `condition` with any optional filtering conditions
you want to apply.
4/4/2024 Prepared By : Basanta Chapagain 125
Select Record- SQL Example
• SELECT query to retrieve all columns for all employees from the `employees` table:
SELECT * FROM employees;
Above query will return all rows and all columns from the `employees` table.
• If you want to retrieve specific columns or apply a condition, you can modify the
query accordingly. For example, to retrieve only the first and last names of employees:
SELECT first_name, last_name FROM employees;
• To retrieve employees who were hired after a certain date:
SELECT * FROM employees WHERE hire_date > '2020-01-01';
• These are just a few examples of `SELECT` queries. You can customize your queries to
retrieve the specific data you need from your table.
4/4/2024 Prepared By : Basanta Chapagain 126
Update Record- SQL Syntax
• To update data in the `employees` table using an `UPDATE` query in MySQL,
you can use the following syntax:
UPDATE table_name SET column1 = value1, column2 = value2, ... WHERE
condition;
• Replace `table_name` with the name of your table, `column1 = value1,
column2 = value2, ...` with the columns you want to update along with their
new values, and `condition` with the criteria to identify which rows to update.
4/4/2024 Prepared By : Basanta Chapagain 127
Update Record- SQL Example
• Here's an example of an `UPDATE` query to change the last name of an
employee with a specific `employee_id`:
UPDATE employees SET last_name = 'Smith‘ WHERE employee_id = 1;
• This query will update the `last_name` column of the employee with an
`employee_id` of 1 to 'Smith'.
• You can also update multiple columns at once:
UPDATE employees SET first_name = 'Jane', last_name = 'Johnson‘ WHERE
employee_id = 2;
• This query will update the `first_name` and `last_name` columns of the
employee with an `employee_id` of 2 to 'Jane' and 'Johnson', respectively.
4/4/2024 Prepared By : Basanta Chapagain 128
Delete Record- SQL Syntax
• To delete data from the `employees` table using a `DELETE`
query in MySQL, you can use the following syntax:
DELETE FROM table_name WHERE condition;
• Replace `table_name` with the name of your table and
`condition` with the criteria to identify which rows to delete.
4/4/2024 Prepared By : Basanta Chapagain 129
Delete Record- SQL Example
• Here's an example of a `DELETE` query to remove an employee
with a specific `employee_id` from the `employees` table:
DELETE FROM employees WHERE employee_id = 1;
• This query will delete the employee with an `employee_id` of 1
from the `employees` table.
4/4/2024 Prepared By : Basanta Chapagain 130
Create Table Example for Students
• Assume a simple `students` table with columns for student
information:
CREATE TABLE students (
student_id INT PRIMARY KEY,
first_name VARCHAR(50),
last_name VARCHAR(50),
age INT,
grade CHAR(1)
);
4/4/2024 Prepared By : Basanta Chapagain 131
Insert Record Example for Students
Single:
INSERT INTO students (student_id, first_name, last_name, age, grade) VALUES (1,
'John', 'Doe', 18, 'A'),
Multiple:
INSERT INTO students (student_id, first_name, last_name, age, grade)
VALUES
(1, 'John', 'Doe', 18, 'A'),
(2, 'Jane', 'Smith', 17, 'B'),
(3, 'Michael', 'Johnson', 19, 'A');
4/4/2024 Prepared By : Basanta Chapagain 132
Update Record Example for Students
Update Record:
UPDATE students SET grade = 'A‘ WHERE student_id = 2;
This query updates the grade of the student with `student_id` 2 to 'A'.
Delete Record:
DELETE FROM students WHERE student_id = 1;
This query deletes the student with `student_id` 1 from the table
4/4/2024 Prepared By : Basanta Chapagain 133
Select Record Example for Students
Retrieve All Records:
SELECT * FROM students;
This query retrieves all records from the `students` table.
Retrieve Specific Columns:
SELECT first_name, last_name FROM students;
This query retrieves only the `first_name` and `last_name` columns from the `students`
table.
Retrieve with Condition:
SELECT * FROM students WHERE age >= 18;
This query retrieves students who are 18 years old or older.
4/4/2024 Prepared By : Basanta Chapagain 134
Select Record Example for Students
Retrieve Ordered Data:
SELECT * FROM students ORDER BY last_name, first_name;
This query retrieves all students and orders the result by last name and then first
name.
Aggregate Functions (Count and Average):
SELECT COUNT(*) AS total_students, AVG(age) AS average_age FROM students;
This query calculates the total number of students and the average age of all
students.
4/4/2024 Prepared By : Basanta Chapagain 135
Database connection into ASP.NET with MySQL
• To establish a database connection between an ASP.NET
application and a MySQL database, you need to provide a
connection string in your ASP.NET application's configuration file.
• The connection string contains information about the database
server, credentials, and other settings needed to connect to the
MySQL database.
• Here's how we can create a MySQL connection string for an
ASP.NET application:
– Install MySQL Connector
– Create Connection String
– Use Connection String in Code
4/4/2024 Prepared By : Basanta Chapagain 136
1. Database connection into ASP.NET with MySQL
• Download Install MySQL Connector: Before you start, make sure
you have the MySQL Connector for .NET installed in your project.
4/4/2024 Prepared By : Basanta Chapagain 137