In databases, an entity is the representation of a real-world object or concept that is described in a database.
Examples of entity names: Student, Employee, Article, News, etc.
Entities are described in the database structure using a data model.
Each entity is made up of one or more attributes.
For example, the entity «Student» could have the attributes: first name, last name, year of birth, etc.
The «News» entity could have the attributes: headline, description, news text, date, etc.
Two types of entities are used in the entity-relationship model: strong entity and weak entity.
Furthermore, in this modeling the entities are related to each other through Relationships.
Strong entities have key attributes, while weak entities do not have key attributes of their own.
The image shows several well-known and widely used methods for representing the same relationship. This particular relationship is a one-to-many relationship. Each diagram shows the relationship between a Person entity and its City (of birth) entity: each person must have been born in one and only one city, while each city can have zero or more people born in it.
implementation of the entities
Once all the Entities and the relationships between them are determined, in addition to their cardinalities and their attributes, they can be implemented or carried out.
In SQL, entities and relationships are implemented in the form of Tables (table is the name used in SQL).
Not all relationships and not all entities will end up becoming tables. It will depend on whether they are weak or strong entities and on the cardinality of the relationships.
Entity Implementation Example
In this example there are two related entities.
PERSON entity with an ID that is a number that identifies it and is a key, and the attributes Name Y surname.
Entity CITY with a Code (postal) that identifies it and a city name.
There is also the relationship that unites both entities. In this case a person was born in a single city, but a single city can have multiple people born in it (this is cardinality).
So to implement this diagram two tables will be created, Person table and Entity table.
For the relationship between these, it will not be necessary to create a table because it is a (1,n) relationship, that is, a person has a single city of birth, while a city has multiple people born.
In this case the Person table will have the attributes: id, name, surname, code (from the city)
Meanwhile, the City table will have its original attributes: code, name.
If the relation had been (n,m), then that relation would necessarily become a table. This table should contain the keys of both related tables.
For example, a Student entity can have many teachers with whom it takes courses.
Whereas a Teacher entity can have multiple students.
In this case the relation is (n,m) and therefore three tables will be implemented.
Student with the attributes: student_id, firstname, lastname
Teacher with the attributes: teacher_id, firstname, lastname
Student_teacher_relationship: student_id, teacher_id
Tables in SQL
As we stated earlier, entities in SQL are called Tables. There are currently countless programs that allow you to easily and visually create tables and their attributes.
Behind this facility, the basic SQL commands for creating tables are executed, which are the following:
CREATE TABLE: used to create a new table.
ALTER TABLE: used to modify a table already created previously.
DROP TABLE: used to drop an existing table.
In order to manipulate (access, insert, modify or delete) the data contained in the tables, commands such as:
– INSERT: to add new data to the tables.
– UPDATE: to update existing data in the tables.
– DELETE: to remove data from the tables.
– REPLACE: to add or change new or existing data in the tables.
– TRUNCATE: to empty the template data.
– SELECT-FROM-WHERE: to retrieve data from a table.
Escape this article will provide SQL examples for each of these commands, but you can go to those commands that have a link (in blue) and there you will find examples of these commands.
Related terms
• Database query
• SQL query
• Table (database)
Doubts? needs more information? Write and we will respond to your email: click here