Introduction to Database Playing With Tables and Database

In this digital era, data is the heartbeat of our modern existence, impacting everything from how we shop and communicate to how we make informed decisions. Little do we realize that behind the scenes, databases and tables work tirelessly, organizing and structuring our data to keep our lives running smoothly.

From tracking our expenses and managing our personal schedules to powering the systems that drive our favorite apps and websites, the influence of databases and tables is all around us. In this blog, we'll explore the art of crafting databases and tables that streamline our digital interactions and enhance the quality of our lives. Discover how these seemingly technical concepts are woven into the fabric of our everyday routines, shaping our experiences in ways we might not have imagined.

So, let's dive in and unveil the secrets of creating databases and tables that are as meaningful and impactful as the data they hold. Get ready to unravel the intricate web that connects our daily lives to the remarkable world of data organization and management. Welcome to a blog where data meets life in the most extraordinary ways!

What are Databases?

A database is a structured collection of data stored electronically. It is a software system that manages and organizes data, allowing users to store, retrieve, and manipulate information. Databases typically consist of multiple tables and objects, such as views, indexes, and stored procedures. They provide a centralized and efficient way to store and manage large amounts of data.

Introduction To Database: Unboxing GROUP BY And JOIN Queries

What are Tables?

Tables are structured collections of data organized in rows and columns. It is one of the fundamental components of a relational database management system (RDBMS). It serves as a way to store and organize data in a tabular format.

Why is it Important to Create Databases and Tables?

Creating datasets and tables is important for several reasons:

  • Organization: Databases and tables provide a structured way to organize and store data, making managing and accessing information easier.
  • Data Integrity: Tables with constraints ensure data accuracy and prevent invalid or inconsistent data insertion.
  • Efficiency: Well-designed tables enable efficient data retrieval, analysis, and performance optimization.
  • Security: Separate databases and access controls protect sensitive data and maintain data confidentiality.
  • Scalability: Databases and tables can scale to accommodate growing data needs, allowing for easy expansion and modification.
  • Collaboration: Databases and tables facilitate consistent data sharing and collaboration among multiple users or applications.

Data Types

Datasets and tables support various data types to accommodate different kinds of information. Here are some common data types used in datasets and tables:

  • Numeric Data Types:​​
  1. Integer: Represents whole numbers without decimal places.
  2. Float/Double: Represents numbers with decimal places and floating-point precision.
  3. Decimal: Represents numbers with fixed-point precision, suitable for financial or precise calculations.
  • Textual Data Types:
  1. Characters/String: Represents alphanumeric characters or text.​
  2. Varchar: Variable-length character string.
  3. Text: Represents large textual data.
  • Date and Time Data Types:
  1. Date: Represents a specific date without time.
  2. Time: Represents a specific time without a date.
  3. DateTime/TimeStamp: Represents a specific date and time.
  • Boolean Data Type:
  1. Boolean: Represents true or false values.​
  • Binary Data Types:
  1. Binary: Represents binary data, such as images, audio files or documents.​
  • Enumerated Data Types:
  1. Enum: Represents a predefined list of values that a column can take.​
  • Other Data Types:
  1. JSON: Represents structured or semi-structured data in JSON format.​
  2. Array/List: Represents a collection of values of the same type.

Different database management systems may provide additional data types or variations of the ones mentioned above. It is essential to choose the appropriate data type based on the nature of the data being stored to ensure efficient storage and accurate data representation.

Primary Key and Foreign Key

Primary Key

A primary key is a column or a combination of columns in a table that uniquely identifies each record or row. It serves as a unique identifier for each row and ensures data integrity and uniqueness within the table. Each primary key value must be unique and cannot be null. Primary keys enforce data integrity, establish relationships with other tables, and enable efficient data retrieval.

Foreign Key

A foreign key is a column or a combination of columns in a table that establishes a link or relationship with the primary key of another table. It represents a reference to a primary key value in another table, creating a relationship between the two tables. Foreign keys enforce referential integrity and maintain consistency between related tables. They ensure that the values in the foreign key column(s) correspond to existing values in the referenced table's primary key column(s). Foreign keys allow for establishing relationships, such as one-to-one, one-to-many, or many-to-many, between tables in a relational database.

In summary, a primary key uniquely identifies each row in a table, while a foreign key establishes a relationship between tables by referencing the primary key of another table. Primary keys ensure data integrity within a table, while foreign keys maintain consistency and enforce relationships between tables.

primary key
PK indicates primary key

Constraints Used

Constraints are rules or conditions applied to columns or tables in a database to maintain data integrity and enforce certain rules. Here are some common constraints used in datasets and tables:

Primary Key Constraint

Ensures that a column or a combination of columns in a table uniquely identifies each row. It enforces uniqueness and allows for efficient data retrieval.

Foreign Key Constraint

Establishes a relationship between tables by referencing the primary key of another table. It ensures referential integrity and maintains consistency between related tables.

Unique Constraint

Ensures that the values in a column or a combination of columns are unique within a table. It prevents duplicate values from being entered in the specified column(s).

Not Null Constraint

Ensures that a column does not contain null values. A value must be provided for the specified column(s) during data insertion.

Check Constraint

Specifies a condition that values in a column must satisfy. It allows for defining custom rules or conditions that restrict the values allowed in the column.

Default Constraint

Specifies a default value for a column if no value is explicitly provided during data insertion. It ensures that a predefined default value is used when no specific value is provided.

Cascade Constraint

Applied to foreign keys, it specifies the action to be taken when referenced data is updated or deleted. Cascade constraints allow for automatic updates or deletions in related tables when the referenced data changes.

These constraints play a vital role in maintaining data integrity, enforcing business rules, and establishing relationships between tables in a database. They help ensure data consistency, prevent inconsistencies or violations, and provide data quality and reliability.

Introduction To Database: Unboxing SELECT Queries One At A Time

Create Table

Let's check the syntax for creating a table in a relational database, along with explanations for each component and an example of it: 

SYNTAX:

CREATE TABLE table_name (
column1 datatype constraint,
column2 datatype constraint,
...
);

Explanation:

  • CREATE TABLE is the SQL statement that signals the intention to create a new table.
  • table_name is the name you choose for your table. It should be unique within the database.
  • Inside the parentheses, you define the columns of the table, separated by commas.
  • You specify each column's name, followed by the data type and any constraints.

Let's break down the components further:

  • column1, column2, ... represent the names of the columns you want to create in the table.
  • datatype refers to the specific data type you assign to each column.
  • constraint represents optional constraints that you can apply to each column.

Example: 

create table
Table named 'account_01' has been created.

Insert

Let's check the syntax for inserting data into a table in a relational database, along with explanations for each component:

SYNTAX:

INSERT INTO table_name (column1, column2, ...)
VALUES (value1, value2, ...);

Explanation:

  • INSERT INTO is the SQL statement that indicates that you want to insert data into a table.
  • table_name refers to the table name into which you want to insert the data.
  • Inside the parentheses after the table name, you specify the column names where you want to insert data. These column names should match the columns in the table.
  • After specifying the column names, you use the VALUES keyword to provide the corresponding values for those columns.
  • Inside another set of parentheses following VALUES, you provide the values you want to insert into the table, separated by commas.​
  • value1, value2, ... represents the corresponding values you want to insert into the specified columns.

Example: After creating  the table 'account_01', we now insert values.

Values to be inserted in the table
insert
After successful execution of the 'insert' query

Update

Let's check the syntax for updating data in a table in a relational database, along with explanations for each component:

SYNTAX:

UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition;

Explanation:

  • UPDATE is the SQL statement used to indicate that you want to update data in a table.
  • table_name refers to the name of the table from which you want to update the data.
  • After the SET keyword, you specify the column names you want to update and their corresponding new values.
  • Each column name is followed by the assignment operator (=) and the new value you want to set.
  • If you want to update multiple columns, you separate each column assignment with a comma.
  • The WHERE clause is optional but highly recommended to specify the condition that determines which rows should be updated.
  • condition is an optional component that specifies the condition that determines which rows should be updated.

Example:

update
Updating the 'last_login' column

Delete

Let's check the syntax for deleting data from a table in a relational database, along with explanations for each component:

SYNTAX:

DELETE FROM table_name
WHERE condition;

Explanation:

  • DELETE FROM is the SQL statement used to indicate that you want to delete data from a table.
  • table_name refers to the name of the table from which you want to delete data.
  • The WHERE clause is optional but highly recommended to specify the condition that determines which rows should be deleted.​

Example:

existing data
Current existing dataset
data to be deleted
SQL to delete 'cowboy'
deleted
Dataset after deleting 'cowboy'

Alter

Let's check the syntax for altering a table in a relational database, along with explanations for each component:

SYNTAX

ALTER TABLE table_name
[ALTER COLUMN column_name {SET DATA TYPE datatype | RENAME TO new_column_name | ADD CONSTRAINT constraint_name constraint_definition | DROP CONSTRAINT constraint_name}]

Explanation:

  • ALTER TABLE is the SQL statement used to indicate that you want to change an existing table.
  • table_name refers to the name of the table you want to alter.

The ALTER statement allows you to perform various modifications on the table. Let's break down the different options:

  • Altering a Column:
  1. ALTER COLUMN column_name SET DATA TYPE datatype is used to modify the data type of a column.​
  2. ALTER COLUMN column_name RENAME TO new_column_name is used to rename a column.
  • Adding Constraints:
  1. ADD CONSTRAINT constraint_name constraint_definition adds a new constraint to the table.​
  2. constraint_name is the name you choose for the constraint.
  3. constraint_definition specifies the type of constraint you want to add, such as PRIMARY KEY, UNIQUE, CHECK, or FOREIGN KEY.
  • Dropping Constraints:
  1. DROP CONSTRAINT constraint_name is used to remove an existing constraint from the table.​
  2. constraint_name is the name of the constraint you want to drop.

Please note that the specific options available and the syntax for altering a table may vary depending on your database management system. The provided syntax represents a general structure for the ALTER statement.

Example:

current table
Current table 'information'
name change
Command to alter table name
name changed
Table name altered to 'new_infor'
rename column
Command to alter column name
rename column
Column name 'person' changed to 'people'
current database
How the database looks currently

Drop

Let's check the syntax for dropping a table or a database object in a relational database, along with explanations for each component:

To drop a table:

SYNTAX:

DROP TABLE table_name;

Explanation:

  • DROP TABLE is the SQL statement that indicates that you want to remove a table from the database.
  • table_name refers to the name of the table you want to drop. This is the table that will be deleted.

To drop a database object (such as a view, index, or constraint):

SYNTAX:

DROP OBJECT object_name;

Explanation:

  • DROP OBJECT is a generic syntax that can drop different types of database objects.
  • object_name refers to the name of the specific object you want to drop. This can be a view, index, constraint, or other supported database object.

It's important to note that the specific syntax and options for dropping objects may vary depending on your database management system. Some databases may have more specific syntax for dropping certain types of objects, such as DROP VIEW, DROP INDEX, or DROP CONSTRAINT.

Example:

dropping a column
Command to drop a column
after dropping
Once the column has been dropped

Check Constraints

Let's check the syntax for creating a check constraint on a table in a relational database, along with explanations for each component:

SYNTAX:

ALTER TABLE table_name
ADD CONSTRAINT constraint_name CHECK (condition);

Explanation:

  • ALTER TABLE is the SQL statement used to modify an existing table.
  • table_name refers to the name of the table on which you want to add the check constraint.
  • ADD CONSTRAINT specifies that you want to add a constraint to the table.
  • constraint_name is the name you choose for the check constraint. It should be unique within the table.
  • CHECK is a keyword that indicates that you want to create a check constraint.
  • (condition) represents the condition or expression that defines the check constraint. The condition must evaluate to true for the constraint to be satisfied.
  • table_name is the name of the table on which you want to add the check constraint.
  • constraint_name is a user-defined name for the check constraint.
  • condition represents the logical expression that defines the check constraint.
key points

Conclusion

In summary, datasets and tables are foundational database structures that organize and store data. They allow for efficient data management and retrieval. Constraints are crucial for maintaining data integrity and defining relationships. Primary and foreign keys establish connections between tables. SQL statements like INSERT, UPDATE, DELETE, ALTER, and DROP enable data manipulation and table modifications. Understanding and effectively using these concepts ensures well-organized and reliable databases for optimal data usage and analysis.