The Interledger Community 🌱

Cover image for Here are some SQL hint to help you build database.
Samuel Guxegdsa
Samuel Guxegdsa

Posted on

Here are some SQL hint to help you build database.

SQL (Structured Query Language) is a programming language used to communicate with and manipulate databases. It is a standard language for managing data held in a relational database management system (RDBMS) or for stream processing in a relational data stream management system (RDSMS).

  1. MySQL: Official Website - MySQL
  2. Oracle SQL: Official Website - Oracle SQL
  3. Microsoft SQL Server: Official Website - Microsoft SQL Server
  4. PostgreSQL: Official Website - PostgreSQL
  5. SQLite: Official Website - SQLite
  6. IBM DB2: Official Website - IBM DB2
  7. Amazon Redshift: Official Website - Amazon Redshift
  8. Apache Hive: Official Website - Apache Hive
  9. Apache Cassandra: Official Website - Apache Cassandra
  10. Teradata: Official Website - Teradata

Please note that the websites mentioned above should provide you with more detailed information about each SQL language and its features.

Here are some commonly used SQL commands and their functions:

  1. SELECT: Retrieves data from a database table based on specified conditions.
  2. INSERT: Inserts new data into a database table.
  3. UPDATE: Modifies existing data in a database table.
  4. DELETE: Removes data from a database table based on specified conditions.
  5. CREATE: Creates a new database table, view, or other database objects.
  6. ALTER: Modifies the structure of a database table or other database objects.
  7. DROP: Deletes a database table, view, or other database objects.
  8. JOIN: Combines rows from multiple tables based on related columns.
  9. GROUP BY: Groups rows based on specified columns, often used with aggregate functions.
  10. HAVING: Filters grouped rows based on specified conditions, used with GROUP BY.
  11. ORDER BY: Sorts the result set based on specified columns.
  12. DISTINCT: Returns unique values from a column in the result set.
  13. WHERE: Filters rows based on specified conditions.
  14. LIKE: Performs pattern matching in a WHERE clause.
  15. UNION: Combines the result sets of two or more SELECT statements.
  16. GRANT: Provides specific privileges to database users.
  17. REVOKE: Removes specific privileges from database users.

Here are some hints to get you started

-- SELECT: Retrieves data from a database table based on specified conditions.
SELECT * FROM Employees WHERE Age > 30;

-- INSERT: Inserts new data into a database table.
INSERT INTO Employees (FirstName, LastName, Age) VALUES ('John', 'Doe', 25);

-- UPDATE: Modifies existing data in a database table.
UPDATE Employees SET Age = 26 WHERE FirstName = 'John' AND LastName = 'Doe';

-- DELETE: Removes data from a database table based on specified conditions.
DELETE FROM Employees WHERE FirstName = 'John' AND LastName = 'Doe';

-- CREATE: Creates a new database table, view, or other database objects.
CREATE TABLE Departments (
    DepartmentId INT PRIMARY KEY,
    DepartmentName VARCHAR(50)
);

-- ALTER: Modifies the structure of a database table or other database objects.
ALTER TABLE Employees ADD COLUMN DepartmentId INT;

-- DROP: Deletes a database table, view, or other database objects.
DROP TABLE Departments;

-- JOIN: Combines rows from multiple tables based on related columns.
SELECT Employees.FirstName, Employees.LastName, Departments.DepartmentName
FROM Employees
JOIN Departments ON Employees.DepartmentId = Departments.DepartmentId;

-- GROUP BY: Groups rows based on specified columns, often used with aggregate functions.
SELECT DepartmentId, COUNT(*) as EmployeeCount
FROM Employees
GROUP BY DepartmentId;

-- HAVING: Filters grouped rows based on specified conditions, used with GROUP BY.
SELECT DepartmentId, COUNT(*) as EmployeeCount
FROM Employees
GROUP BY DepartmentId
HAVING COUNT(*) > 5;

-- ORDER BY: Sorts the result set based on specified columns.
SELECT * FROM Employees ORDER BY LastName, FirstName;

-- DISTINCT: Returns unique values from a column in the result set.
SELECT DISTINCT DepartmentId FROM Employees;

-- WHERE: Filters rows based on specified conditions.
SELECT * FROM Employees WHERE Age > 30;

-- LIKE: Performs pattern matching in a WHERE clause.
SELECT * FROM Employees WHERE FirstName LIKE 'J%';

-- UNION: Combines the result sets of two or more SELECT statements.
SELECT FirstName FROM Employees
UNION
SELECT FirstName FROM Customers;

-- GRANT: Provides specific privileges to database users.
GRANT SELECT, INSERT, UPDATE ON Employees TO 'user1';

-- REVOKE: Removes specific privileges from database users.
REVOKE INSERT, UPDATE ON Employees FROM 'user1';
Enter fullscreen mode Exit fullscreen mode

These are just a few examples of SQL commands and their functions. SQL is a rich and versatile language with many more commands and functionalities available for managing and manipulating databases.

Database structure overview

A database structure, also known as a database schema, is the logical configuration of all or part of a relational database. It can include the creation of tables, the types of data allowed in each column, and the relationships between different tables.

Here's a brief overview of the main components of a database structure and the SQL commands to work with them:**

A; Database: This is the container that holds the tables and other SQL structures related to your app. It's the main container for the data in a relational database management system.

-- Create a database
CREATE DATABASE database_name;

-- Delete a database
DROP DATABASE database_name;
Enter fullscreen mode Exit fullscreen mode

B; Tables: These are used to store data within the database. A table is a collection of related data entries and it consists of columns and rows. Each table has a name and is organized into rows. Each row represents a single record.

-- Create a table
CREATE TABLE table_name (
    column1 datatype,
    column2 datatype,
    ...
);

-- Delete a table
DROP TABLE table_name;
Enter fullscreen mode Exit fullscreen mode

C; Columns (Fields): Each table is made up of one or more columns. Each column is designed to store a certain type of information, for example, dates, names, or numbers.

-- Add a column to a table
ALTER TABLE table_name ADD column_name datatype;

-- Delete a column from a table
ALTER TABLE table_name DROP COLUMN column_name;
Enter fullscreen mode Exit fullscreen mode

D; Rows (Records): Each row in a table represents a set of related data, and every row in the table has the same structure.

-- Insert a row into a table
INSERT INTO table_name (column1, column2, ...) VALUES (value1, value2, ...);

-- Delete a row from a table
DELETE FROM table_name WHERE condition;
Enter fullscreen mode Exit fullscreen mode

E; Primary Key: This is a column (or set of columns) whose values uniquely identify every row in a table. Primary keys provide a way of distinguishing each record in a table from every other record in the table.

-- Add a primary key to a table
ALTER TABLE table_name ADD PRIMARY KEY (column_name);
Enter fullscreen mode Exit fullscreen mode

F; Foreign Key: This is a column or set of columns in one table, that is used to link to a Primary Key in another table. Foreign keys are used to enforce referential integrity.

-- Add a foreign key to a table
ALTER TABLE table_name ADD FOREIGN KEY (column_name) REFERENCES other_table(other_column);
Enter fullscreen mode Exit fullscreen mode

G; Index: This is used to speed up the retrieval of rows from the table or view. An index is a data structure (most commonly a B- tree) that improves the speed of data retrieval operations on a database table.

-- Create an index on a table
CREATE INDEX index_name ON table_name (column_name);

-- Delete an index
DROP INDEX index_name;
Enter fullscreen mode Exit fullscreen mode

H; Views: These are virtual tables based on the result-set of an SQL statement. A view contains rows and columns, just like a real table. The fields in a view are fields from one or more real tables in the database.

-- Create a view
CREATE VIEW view_name AS SELECT column1, column2, ... FROM table_name WHERE condition;

-- Delete a view
DROP VIEW view_name;
Enter fullscreen mode Exit fullscreen mode

I; Stored Procedures: These are prepared SQL codes that you can save, so the code can be reused over and over again.

-- Create a stored procedure
CREATE PROCEDURE procedure_name AS SQL_statement;

-- Delete a stored procedure
DROP PROCEDURE procedure_name;
Enter fullscreen mode Exit fullscreen mode

J; Triggers: These are SQL commands that are automatically executed by the database system when certain events occur, like data modification (INSERT, DELETE, UPDATE).

-- Create a trigger
CREATE TRIGGER trigger_name TRIGGER_TIME TRIGGER_EVENT ON table_name FOR EACH ROW BEGIN SQL_statements; END;

-- Delete a trigger
DROP TRIGGER trigger_name;
Enter fullscreen mode Exit fullscreen mode

Conclusion: Remember, the structure of a database is very important because it determines how data is stored, organized, and manipulated. It's crucial to plan the structure of the database carefully during the design phase to ensure that it fits the needs of your application.

Top comments (1)

Collapse
 
techburner profile image
Steve Smith

A very great article, thank you for sharing.