Table
Table
and contrasting information easier. As you can see in the following example, the data is easier to read
in a table format.
Name,Date of Birth,Phone
Bob Smith,01-05-65,555-123-4567
Jane Doe,07-20-70,555-232-1818
Tables in a database
In a database, a table consists of columns and rows of data, much like an Excel spreadsheet. It is often
referenced by software programs and web pages, to store and retrieve data for users. There are
multiple types of databases, but the structure of a table in each database type is mostly the same.
SQL Table
o SQL Table is a collection of data which is organized in terms of rows and columns. In DBMS, the
table is known as relation and row as a tuple.
o Table is a simple form of data storage. A table is also considered as a convenient representation
of relations.
In the above table, "EMPLOYEE" is the table name, "EMP_ID", "EMP_NAME", "CITY", "PHONE_NO" are the
column names. The combination of data of multiple columns forms a row, e.g., 1, "Kristen",
"Washington" and 7289201223 are the data of one row.
Operation on Table
1. Create table
2. Drop table
3. Delete table
4. Rename table
SQL create table is used to create a table in the database. To define the table, you should define the
name of the table and also define its columns and column's data type.
Syntax
1. create table "table_name"
5. ...
Example
7. );
If you create the table successfully, you can verify the table by looking at the message by the SQL
server. Else you can use DESC command as follows:
Now you have an EMPLOYEE table in the database, and you can use the stored information related to
the employees.
Drop table
A SQL drop table is used to delete a table definition and all the data from a table. When this command
is executed, all the information available in the table is lost forever, so you have to very careful while
using this command.
Syntax
Firstly, you need to verify the EMPLOYEE table using the following command:
This table shows that EMPLOYEE table is available in the database, so we can drop it as follows:
Now, we can check whether the table exists or not using the following command:
In SQL, DELETE statement is used to delete rows from a table. We can use WHERE condition to delete a
specific row from a table. If you want to delete all the records from the table, then you don't need to
use the WHERE clause.
Syntax
Example
2. WHERE EMP_ID = 3;
If you don't specify the WHERE condition, it will remove all the rows from the table.
An Entity Relationship Diagram (ERD) is a snapshot of data structures. An Entity Relationship Diagram
shows entities (tables) in a database and relationships between tables within that database. For a good
database design it is essential to have an Entity Relationship Diagram.
There are three basic elements in ER-Diagrams:
Entities are the "things" for which we want to store information. An entity is a person, place,
thing or event.
Lines: It links attributes to entity types and entity types with other relationship types
Entities
Attributes
Relationships
ER Diagram Examples
For example, in a University database, we might have entities for Students, Courses, and
Lecturers. Students entity can have attributes like Rollno, Name, and DeptID. They might
have relationships with Courses and Lecturers.
WHAT IS ENTITY?
A real-world thing either living or non-living that is easily recognizable and nonrecognizable. It is
anything in the enterprise that is to be represented in our database. It may be a physical thing or
simply a fact about the enterprise or an event that happens in the real world.
An entity can be place, person, object, event or a concept, which stores data in the database. The
characteristics of entities are must have an attribute, and a unique key. Every entity is made up of
some 'attributes' which represent that entity.
Examples of entities:
Notation of an Entity
Entity set:
Student
An entity set is a group of similar kind of entities. It may contain entities with attribute sharing similar
values. Entities are represented by their properties, which also called attributes. All attributes have
their separate values. For example, a student entity may have a name, age, class, as attributes.
Example of Entities:
A university may have some departments. All these departments employ various lecturers and offer
several programs.
Some courses make up each program. Students register in a particular program and enroll in various
courses. A lecturer from the specific department takes each course, and each lecturer teaches a
various group of students.
Relationship
Relationship is nothing but an association among two or more entities. E.g., Tom works in the
Chemistry department.
Entities take part in relationships. We can often identify relationships with verbs or verb phrases.
For example:
Weak Entities
A weak entity is a type of entity which doesn't have its key attribute. It can be identified uniquely by
considering the primary key of another entity. For that, weak entity sets need to have participation.
In above ER Diagram examples, "Trans No" is a discriminator within a group of transactions in an ATM.
Let's learn more about a weak entity by comparing it with a Strong Entity
Strong entity set always has a primary key. It does not have enough attributes to build a
primary key.
It contains a Primary key represented by the It contains a Partial Key which is represented by
underline symbol. a dashed underline symbol.
The member of a strong entity set is called as The member of a weak entity set called as a
dominant entity set. subordinate entity set.
Primary Key is one of its attributes which helps to In a weak entity set, it is a combination of
identify its member. primary key and partial key of the strong entity
set.
In the ER diagram the relationship between two The relationship between one strong and a weak
strong entity set shown by using a diamond entity set shown by using the double diamond
symbol. symbol.
The connecting line of the strong entity set with The line connecting the weak entity set for
the relationship is single. identifying relationship is double.
Attributes
For example, a lecture might have attributes: time, date, duration, place, etc.