Page Content

Tutorials

What is the Role of Tables in RDBMS such as MYSQL

What is Role of Tables?

A table serves as the primary storage location for data or information in relational database management systems (RDBMS), such as MySQL. Because data is arranged using rows and columns, a table and a spreadsheet are comparable. It’s regarded as a two-dimensional data representation. The table is the primary building element of a database.

Tables are made to organize and store data. By organizing data into rows and columns, they offer structure. One or more tables make up any database. A table can also be referred to as a relation in the relational paradigm.

Relationship between a Database and a Table

An assortment of connected data is called a database. To be more precise, a database in a relational database system is a group of related tables. Consider databases to be table containers. A database represents the complete collection of data and tables, while tables reflect the divisions of data inside a database. A database object may not be a table (for example, a view or index, though these are not specifically addressed in this context as non-table data), but the opposite is not always true for all data within a database.

Data can be categorized and stored in databases. For instance, distinct tables for Customers, Products, Employees, and Orders may be present in a firm database. Although a database can have only one table, relational databases usually contain numerous tables connected by shared values. Relational databases are those that use a common column to store data in distinct, connected tables.

Tables Store Related Data and Consist of Columns and Rows

The purpose of tables is to store linked data. This data is arranged into vertical groupings called columns and horizontal subsets called rows. Information may be stored and retrieved in an organized manner because to this framework.

Table rows include data about a person, place, item, or event. An Employee table’s rows contain each employee’s ID, name, city, and compensation. A single row’s data items may all be of distinct types.

Every column in a table has a distinct name that indicates the kind of data it will hold. Every data item in a single column needs to belong to the same data type. INTEGER, CHAR, VARCHAR, DECIMAL, and DATE are a few examples of data types.

Understanding Basic Table Parts: Row (Record) and Column (Field)

Let’s define the terms used to describe the various components of a table:

Row: A table’s horizontal subset. Other names for a row include a record, entry, or tuple. A record is a collection of information regarding a certain thing or entity, and each row represents one record.

Column: A table’s vertical subset. A column may also be referred to as a domain, field, or attribute. Every column contains data of the same type and has a unique name. The kind of data that is kept in each vertical section of the table is specified by the columns.

Like a spreadsheet cell, a single data value is kept at the intersection of a row and a column.

Create Tables

In a database, the first step in organizing data is to create a table. Data is arranged in tables using rows and columns. Each column, also called an attribute or field, defines a type of data, while each row, sometimes called a record or tuple, represents a single entry or collection of data on a person, place, or object.

Create a table by naming it and defining each column’s name, data type, and optional characteristics. Using NOT NULL prevents columns from being empty.

Here is an example:

CREATE TABLE MyGuests (
    id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    firstname VARCHAR(30) NOT NULL,
    lastname VARCHAR(30) NOT NULL,
    email VARCHAR(50),
    reg_date TIMESTAMP
);

The MyGuests table has five columns and requires firstname and lastname.

AUTO_INCREMENT

The AUTO_INCREMENT feature generates a unique number identification for new records in a database, usually the main key. A unique ID is assigned to each row without manual input. When the AUTO_INCREMENT column is NULL, 0 or the default, values start at 1 and increase by 1 for each new entry (unless a SQL option prevents 0).

Due to transaction rollbacks or INSERT IGNORE or REPLACE operations, MySQL may “burn” AUTO_INCREMENT IDs, skipping or pre-allocating values.

Example of table creation with AUTO_INCREMENT:

CREATE TABLE Persons (
    Personid INT NOT NULL AUTO_INCREMENT,
    LastName VARCHAR(255) NOT NULL,
    FirstName VARCHAR(255),
    Age INT,
    PRIMARY KEY (Personid)
);

You can also set the starting AUTO_INCREMENT value using ALTER TABLE. For example:

ALTER TABLE your_table_name AUTO_INCREMENT = 101;

To fill in the gaps left by significant deletions, this can be helpful.

Rename Tables

Change the name of a MySQL table using the RENAME TABLE command or ALTER TABLE with the RENAME TO clause.

A RENAME TABLE example would be:

TABLE existing_table TO new_table_name RENAME;

Using an ALTER TABLE example:

TABLE existing_table RENAME TO new_table_name ALTER TABLE;

Additionally, you can use a single command to rename several tables.

Add Columns

The ALTER TABLE command and ADD COLUMN clause add a column to an existing table. For the new column, you may define the data type and any restrictions. You can choose to place the new column either AFTER an existing column or FIRST (to make it the first column).

Example:

ALTER TABLE Employee ADD TelNo Integer;

To add multiple columns:

ALTER TABLE Student ADD (TelNo Integer, Age Integer DEFAULT 10);

To add a column after an existing one:

ALTER TABLE cats ADD gender CHAR(1) AFTER name;

Drop Columns

With the DROP COLUMN option, the ALTER TABLE command removes columns from a table. The column and data are deleted permanently.

Example:

ALTER TABLE Student DROP TelNo;

To drop multiple columns:

ALTER TABLE Emp DROP JOB, DROP Pay;

In the event that the column is the only one remaining in the table, a DROP operation will fail.

Drop Tables

A database table is permanently deleted with the DROP TABLE command. This procedure deletes all data, table structure, and definition — permanently. It should therefore be used very carefully.

Example:

DROP TABLE Student;

To avoid an error if the table doesn’t exist, you may also use DROP TABLE IF EXISTS.

Temporary Tables

Temporary tables are unique tables created to hold data that is only available during the current client session. They immediately delete when the session or connection ends. Their usefulness includes intermediate calculations and short-term data storage. Individual temporary tables are segregated to the client that created them, allowing several connections to share a temporary table name.

Example of creating a temporary table:

CREATE TEMPORARY TABLE tempTable1 (
    id INT NOT NULL AUTO_INCREMENT,
    title VARCHAR(100) NOT NULL,
    PRIMARY KEY (id)
);

Additionally, a SELECT query can be used to generate temporary tables. Before the session ends, you can use the DROP TABLE command to specifically destroy a temporary table.

Example of dropping a temporary table:

DROP TABLE tempTable1;

Generated Columns

There is no specific information regarding “Generated Columns” as a unique MySQL feature—where column values are automatically calculated from other columns in the same table and stored with the row—in the sources that are supplied. However, the sources cover related topics like obtaining computed values from SELECT queries by using functions and expressions. Aliases, for instance, can be used to choose an expression as a new column.

An example of an alias-based computed column in a SELECT statement is as follows:

SELECT Name, Sal*12 AS 'Annual Salary' FROM EMP;

This demonstrates how to use a query to get a “result” or “derived data”. Although this results in a calculated value in the query output, the table definition in the sources does not include this as a stored produced field. Direct data storage and manipulation within tables, as well as the usage of functions and expressions for data retrieval or modification at query time, are the main topics of the sources.

Index