Database 2
Database 2
We're going to explain the basics of what a database actually is, look at the history,
understand relational databases, get into some fundamental concepts from columns
and rows on up, touch on other types of databases
II.Theory
1. Database
− The purpose of a database is to collect, store and manage data. Databases exist
as files in computer operating systems or database management systems. They
are stored on memory devices such as hard drives, USB drives, CDs, etc.
- Secure all data: Total security and complete data is the key point of surrender in data
storage impact. And the database deserves a “10” on the entire data set. If you want to
save the text file to data, you should use the .xml file and the .csv file is best. When you
save to two file types, you can easily and securely convert to a database. This is normal
is used in the external database backup to readable.
- Ensure simultaneous access of multiple users' capabilities on data: Lots of people can
use the database at the same time without going through complicated points by accessing
from different ways. Therefore, you will have many advantages in using, managing,
accessing data, etc.
- Flexibility to change according to user needs: You have the flexibility to change the
complexity and size of a database. There are databases that consist of only a few hundred
records (a list of students in a class) and there are databases that have a very large
capacity (such as the inventory management database of a supermarket). At the same
time, the database storage format is also quite diverse. Databases can be stored in various
formats such as hard drives, USB sticks or CDs.
2. Relational database
2.1 What is a relational database?
- A relational database is a type of database that stores and provides access to data
points that are related to one another. Relational databases are based on the relational
model, an intuitive, straightforward way of representing data in tables. In a relational
database, each row in the table is a record with a unique ID called the key. The columns
of the table hold attributes of the data, and each record usually has a value for each
attribute, making it easy to establish the relationships among data points.
- Table: A data table is a key component in a relational database. The table contains
the data.
Field: The fields represent attributes of the data table such as name, address, etc.
Cell: Intersection between rows and columns and where data is stored
Primary Key: Is a field or fields combined used to define a record. Primary key has
02 attributes that cannot be duplicated and cannot be null. For example, the value 1
of that Customer ID field will infer all the data of the first row. In other words, the
values of the first row are the values of the attributes of the table with customer ID =
1.
- Relationship:
One-to-one relationship: In this relation, each table has only one and only one
corresponding record. For example husband-wife relationship, relationship Basic
information – Details…
n-to-n relationship: in this relation one table in one table corresponds to many
records in the other and vice versa.
- ERD: Entity-relationship diagrams help you understand more quickly about structured
database systems, making it easier for you to work with them.
Database Management System – DBMS : DBMS is the software used to manage
and operate the Database. Popular DBMSs like MySQL, SQL Server, Oracle
Database, DB2...
- ERD allows to convey the logical structure of the database to the user.
- Entities: A thing in the real world, whether it exists or not, that may or may not be
recognizable. It can be anything in an organization that can be represented in a database.
It can be an object, a business piece of information, or a real-world event. An entity can
be a place, person, object, event or concept that is stored data in a database. Entities
characteristics must have an attribute and have a key attribute, called a key, to easily
identify the entity as unique. Every entity is made up of some attribute that represents
that entity.
- Properties: A trait containing a value for an entity type or a relation type. Example:
A lecture can have attributes like time, date, duration, location.
-> Attributes are represented by an ellipse
Attribute Types Describe
Single attribute Attributes cannot be subdivided anymore. A
student's contact number, for example,
is called an atomic value.
Synthetic properties Aggregate properties can be subdivided. For
example, a student's full name can be divided
into last name, first name, and last
name.
Derived Attribute This attribute type is not included in the
physical database. However, their values are
derived from other attributes present in the
database. For example, a student's age can be
calculated from the date of
birth.
Multivalued attribute Multivalued attributes can have multiple values.
For example, a student may have more than one
cell phone number or email
address.
- Symbols of components in an entity-relationship diagram
4. Relational schema
4.1 What is a relational schema?
- A relational schema is a set of relational tables and associated items that are related to
one another. All of the base tables, views, indexes, domains, user roles, stored modules,
and other items that a user creates to fulfill the data needs of a particular enterprise or
set of applications belong to one schema.
4.2 What is the purpose of relational schema?
- The goal of the E-R model in database design is to analyze the data, identify the basic
units of information needed by the organization, and describe their structure and
relationships.
4.3 How to convert ERD to relational schema
- Converting files can: Each entity is converted to a relation with the same name and
attribute list. For regular entity type: The key of the relation is the key of the entity type.
The attribute of the relation is also the property of the entity type. The relation does not
contain multivalued attributes, but only the member attributes of the composite
attributes.
- Switch 3 star relationship: Convert to a new relationship, with a primary key consisting
of 3 key attributes of the 3 entities participating in the association. The association
attribute (if any) will become the attribute of the new relationship.
- Transformation relationship 1 - 1: One side's key attribute will be the other party's
foreign key or vice versa.
- Transform 1 – n relationship: Attribute 1 side key as a many-party foreign key.
- Convert n - n relationship: Convert to a new relationship with a primary key consisting
of 2 key attributes of 2 or more relations, the association attribute (if any) becomes an
attribute of the new relation.
- Convert multivalued relationship: Convert to a new relationship with a primary key
consisting of a multivalued attribute and an entity key attribute. After converting to a
new relationship, the multivalued attribute will automatically disappear from the old
entity.
5. Relational database management system
5.1 What is a relational database management system?
Provide tools to control and control access to the database to ensure the
fulfillment of some basic requirements of the database system. Including: (1)
Ensuring security, detecting and preventing illegal access. (2) Maintain data
consistency. (3) Organize and control access. (4) Restore the database in the
event of a hardware or software failure. (5) Manage data descriptions.
5.3 Some database management systems
6. Standardized data
6.1 Database Normalization
After normalization, the normalized tables Dept and Employee looks like below:
Second Normal Form (2NF): A relation is in 2NF normal form if the relation as 1NF,
Non-key attributes must be fully functionally dependent on the primary key.
In this example, the Title column is functionally dependent on Name and Date columns.
These two keys form a composite key. In this case, it only depends on Name and
partially dependent on the Date column. Let’s remove the course details and form a
separate table. Now, the course details are based on the entire key. We are not going to
use a composite key.
7. Query language
7.1 What is query language?
- It is a common name for computer languages used to create queries in databases and
information systems.
7.2 Commands in query language
a. Create
b. Select
c. Where
d. Insert into
e. Update
f. Delete
g. Joins
8. System testing
8.1 What is testing?
- It is an activity to find and detect software errors, to ensure that the software is accurate,
correct and complete according to customer requirements and product requirements.
8.2 The purpose of testing
- Software testing to assess whether the software meets the expectations or has any
flaws?
- Is the software working as expected?
- Does the software meet the customer's requirements? Does it do what the user expects?
- Do users like it?
- Is it compatible with our other systems?
II. Practice
1. Analysis the real problem
- Building a system database for a phone retail store named “Thinhstore”. The system
includes a store that sells smartphone products to consumers. In addition, the store will
have staff with the task of managing orders and smartphone products. The products will
be divided according to many types of product categories of the store. For online orders,
it will be delivered to the shipping department for direct delivery to the customer.
2. Entities
- Customer
- Invoice
- InvoiceDetail
- Product
- Sale
- Shipper
- Supplies
3. Entity Attributes
- Customer (idCus,contactname,addres,phone)
- Invoice (idOrder , orderdate , shipdate , shipname , shipaddress)
- InvoiceDetail(idInvoice, price , quantity , discount )
- Product (IMEI , name , releaseDate , capicity , size)
- Sale(idSale , name , phone , addres , mgid)
- Shipper ( idShip , phone , company)
- Supplies ( idSup , companyname , country , phone )
4. Relationships of entities
- Product give information about produc.
- supplies provide more product
- customer buy more product
- sale sale product
- shipper ship product
- all information about them are writen down in invoice and invoice detail
5. ERD
1. Relational schema
2. Constraints of DB
3. Detail settings
- Customer
- Supplies
- Shipper
- Sale
- Product
- Invoice
- InvoiceDetail
4. DB table
5. Query statements in the created database
* Insert
* Select
* Delete
• multi-condition query
*group by:
-- take out the customer ‘name and how many Invoice thay have
*Union
Take out the list of customer and shipper
Database protection has different levels to it. Here are some of the standards for
protecting a database:
• Data-Level Security: This is the process of securing data from getting tampered with
or stolen within the server.
• User-Level Security: When they attack a server, they do so from the user level. This
is why organizations look towards real-time protection software for monitoring
transactions and restricting users from visiting unauthorized websites or downloading
from untrusted sources.
• System-Level Security: This is to protect network servers, hardware, and other
outbound/inbound communications from working as the channel for distributing
malicious software.
There are different types of database attacks, but the ones that most threaten the
security of the database are:
• DoS attacks
• Privilege escalation
• Buffer overflow vulnerability
• SQL injection threats
• Weak authentication
Data Security Best Practices: There are some best practices recommended by experts
to ensure data security. Some methods that are most trusted and widely used to ensure
data security are:
Physical Security: This is one of the first methods to secure physical servers. It
mainly requires digital locks and the use of CCTV cameras to continuously monitor
the data and secure the premises 24/7, preventing unauthorized entry.
Establish Policies for Security and Compliance: You have to define your standards
and security policies clearly. This will ensure that your enterprise won’t have to
struggle with assessing compliance or measuring the progress against its benchmarks.
Most times, organizations develop strong security policies for their data because it
moves through the network. However, they fail to map these policies back to their
database by themselves.
When weaknesses in security are remediated, this is mostly a reaction to the incident
instead of being a proactive response as a result of policies. Businesses always have to
review their policies after patching the vulnerabilities or installing a newer version of
the software. This is so that they can account for settings and configurations that they
have updated. When establishing standards and policies, the data security team has to
ensure that they address how they update their policy regularly, who is in charge of the
updates, what is meant to trigger a change of policy, and the process of approving a
policy change.
Use a Strong Database Software: You can use an open-source database software or a
version paid for and supported by a vendor. This software will run on only active
devices, but on inactive devices, it will be unplugged, and the default accounts that are
not in use will be periodically removed. Security patches should be applied timely.
You also have to look closely at the vendor of your software to determine how they are
keeping the code very secure.
Implementing Application Code: The DBAs will ensure that the source codes and
configuration files are accessible through authorized accounts of the operating system.
Application codes should be reviewed regularly to ensure that they are not vulnerable
to the injection of SQL.
Establishing Server Firewalls: You can set all connections to your database to ensure
it denies all incoming traffic using a firewall. This means that it will block access for
direct clients and give access to only authorized applications. The database
administrators (DBAs) and system administrators (SAs) strictly maintain and monitor
firewall rules. They are also responsible for performing ISP scans and network scans
for strengthening the server machines.
Role of DBAs and SAs: The role of SAs and DBAs are vital as they play a significant
role as strict administrators. Only selected people have proper authorization, and they
all sign an NDA after a background check.
The DBA staff are allowed to use personal accounts to access the server and not share
accounts. These people have to build and maintain a very strong password to meet all
the necessary safety and security guidelines.
The Three Concepts of Database Security
Three main concepts are essential for database security. These are:
2. Integrity: The control system for the user system is to make sure that only
authorized personnel are allowed to access the data, and the log is maintained so
everybody can access it. This will ensure that there is no breach of data caused by
errors.
3. Availability: The data stored within the system is available to both the
administrators and users, reducing downtime. The servers are updated periodically to
ensure maximum security while also boosting the job through continuous service.
Conclusion
When the administrator of the database is aware of the most common threats, they
implement these different methodologies for data protection and make sure that they
store the data in servers that are extremely protected. These best practices will ensure
that the data recovery and backup are able to safeguard the data against ransomware.
2. Test case
3. Trigger:
Trigger will be called when there is a change to the table :
Inserted : contains the information that has been inserted
Deleted : contains the information that has been deleted
This way we will export theconfiguration file.sql from one machine and run this .sqlfile
onanothermachine to get the "Database" to be restored.
Step 1: Right-clickonDatabaseselectTasks>Generate Scripts...
Right-click on Database -> Tasks -> Generate Scripts
Step 2: ClickNexttocontinue
Click Next...
2 . if- else
Create function to take out ‘ContracName’ in table Customer with parameter @id
Call function :
Output:
1. Cursor
---------cursor is using to point out the value at position that you want to look at
Create cursor:
◼ This cursor is using to point at table that have 2 column are ‘’ContractName’’
and ‘’ Phone’’ in table dbo.Customer
Out put :
VII. JDBC:
Step 1: Connect
Use the connection class to connect to SQL Database
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
prepsInsertProduct.execute();
// Retrieve the generated key from the insert.
resultSet = prepsInsertProduct.getGeneratedKeys();
Step 3:
Next to we optimise 6 option : insert, reaload , reset , delete, search , close
1: RELOAD :
It is used to fill up data in table :
Program in class connection:
Program in jframe form:
Result:
Delete function:
Insert function:
Search function :
Close function :