From c9fcbfccb0b2e8f1c4140776ccbcbfaa3b38a0c9 Mon Sep 17 00:00:00 2001 From: Shantanu Shukla Date: Thu, 26 Sep 2024 19:47:20 +0530 Subject: [PATCH 1/5] added postgreSQL identifier --- .../concepts/identifers/identifers.md | 289 ++++++++++++++++++ 1 file changed, 289 insertions(+) create mode 100644 content/postgresql/concepts/identifers/identifers.md diff --git a/content/postgresql/concepts/identifers/identifers.md b/content/postgresql/concepts/identifers/identifers.md new file mode 100644 index 00000000000..7c4dc755a34 --- /dev/null +++ b/content/postgresql/concepts/identifers/identifers.md @@ -0,0 +1,289 @@ +--- +Title: 'Identifier' +Description: 'Refers name given to databases object such as tables, columns, indexes etc.' +Subjects: + - 'Data Science' + - 'Computer Science' +Tags: + - 'PostgreSQL' + - 'Collations' +CatalogContent: + - 'learn-sql' + - 'paths/analyze-data-with-sql' +--- + +In PostgreSQL, identifiers refer to the names given to database objects such as tables, columns, indexes, views, sequences, and other database components. + +## Identifier Types + +`Identifier Types` refers to names given to objects in a database like tables, columns, indexes and other components of a database. These names help **PostgreSQL** to interact with database. The are different types of Identifier that are usedL + +- Table Identifier +- Column Identifiers +- Index Identifiers +- View Identifiers +- Constraint Identifiers +- Schema Identifiers +- Sequence Identifiers +- Function Identifiers +- Trigger Identifiers +- Role Identifiers +- Foreign Key Identifiers +- Aggregate Type Identifiers +- Tablespace Identifiers + +### Table Identifier + +Table Identifier refers to name given to a table in the database. These name are used for interacting with the tables, they must follow certain rules like each table must have a unique name, using only character in the name that are allowed etc. + +#### Example + +In the below example `employees` is the table identifier: + +```pseudo +-- Creating a Table with Standard identifiers +CREATE TABLE employees ( + id SERIAL PRIMARY KEY, + name VARCHAR(100) +); + +-- Creating a Table with case-sensitive identifiers +CREATE TABLE "Employees" ( + "ID" SERIAL PRIMARY KEY, + "Name" VARCHAR(100) +); + +-- Creating a table with special character identifiers +CREATE TABLE "Employee Details" ( + "Emp ID" SERIAL PRIMARY KEY, + "First Name" VARCHAR(100) +); +``` + +### Column Identifiers + +Column Identifier refers to name given to a colums in the database. These name are used for interacting with the columns, they must follow certain rules like each column in the same table must have a unique name but they do not need to be unique across different tables. For cases where using different tables with same column name using tables aliases is important to differentiate between them. + +#### Example + +In the below example two different tables `employees` and `departments` with same `name` column to access both tables aliases are used. + +```sql +CREATE TABLE employees ( + id SERIAL PRIMARY KEY, + name VARCHAR(100), + department_id INT +); + +CREATE TABLE departments ( + id SERIAL PRIMARY KEY, + name VARCHAR(100) +); + +SELECT emp.name AS employee_name, dep.name AS department_name +FROM employees emp +JOIN departments dep ON emp.department_id = dep.id; +``` + +### Index Identifiers + +Index Identifier refers to name given to index created on tables or columns. Indexes are database objects that help improve the speed of query operations by providing quick access paths to data. + +#### Example + +In the below example `employee_name_idx` index is created on `employees` table: + +```sql +CREATE INDEX employee_name_idx ON employees (name); +``` + +### View Identifiers + +View Identifier refers to name given to views which are virtual tables defined by a query it allows user to encapsulate complex queries under a single name and interact with the query results as if they were actual tables. + +#### Example + +In the below example `employee_summary` view is created which works as a shortcut to select from the `employees` table. + +```sql +CREATE VIEW employee_summary AS +SELECT name, department, salary +FROM employees; + +SELECT * FROM employee_summary; +``` + +### Constraint Identifiers + +Constraint Identifiers are name assgined to constraint that enforce rules on the data in the table. They ensure data integrity and include types such as primary keys, foreign keys, unique constraints, check constraints, and not-null constraints. + +#### Example + +In the below example we use different kinds of constraint + +```sql +CREATE TABLE employees ( + id SERIAL PRIMARY KEY, + department_id INT, + FOREIGN KEY (department_id) REFERENCES departments(id), + email VARCHAR(255) UNIQUE, + price NUMERIC CHECK (price > 0) +); +``` + +### Schema Identifiers + +Schema Identifiers refers to name given to schemas, which are logical container for building relation between tables, views, indexes and other database objects. It helps in organizing database object and avoid conflicts. + +#### Example + +In the below example we use different kinds of constraint + +```sql +CREATE TABLE employees ( + id SERIAL PRIMARY KEY, + department_id INT, + FOREIGN KEY (department_id) REFERENCES departments(id), + email VARCHAR(255) UNIQUE, + price NUMERIC CHECK (price > 0) +); +``` + +### Sequence Identifiers + +Sequence Identifiers refers to name assigned to sequence which is used to generate unique integer values and can be used in scenario when the purpose is to make sure each row has a distinct value, typically used for auto-incrementing primary key columns. + +#### Example + +In the below example a seqeunce is create and its current value is returned: + +```sql +-- Naming sequence using +CREATE SEQUENCE employee_id_seq; + +SELECT currval('employee_id_seq'); +``` + +### Function Identifiers + +Function Identifiers are name assigned to custom function, these function are made for certain purpose and they return a result based on the logic inside them. + +#### Example + +In the below example a custom function `add_numbers` used to add numbers passed to it: + +```sql +CREATE FUNCTION add_numbers (a INT, b INT) RETURNS INT AS $$ +BEGIN + RETURN a + b; +END; +$$ LANGUAGE plpgsql; + +SELECT add_numbers(5, 10); +``` + +### Trigger Identifiers + +Trigger Identifiers are names assgined to triggers, which automatically execute a specific procedure in response to certain events on the table. First we need to create trigger by specifying when to execute and the action to be performed. + +#### Example + +In the below example we have a trigger `update_timestamp` that will be trigger when a user information is updated and create a timestamp for when the update happened. + +```sql +CREATE TABLE employees ( + id SERIAL PRIMARY KEY, + name VARCHAR(100), + position VARCHAR(100), + last_updated TIMESTAMP +); + +CREATE OR REPLACE FUNCTION update_last_updated() +RETURNS TRIGGER AS $$ +BEGIN + -- Set the last_updated column to the current time + NEW.last_updated = NOW(); + RETURN NEW; +END; +$$ LANGUAGE plpgsql; + +CREATE TRIGGER update_timestamp +BEFORE UPDATE ON employees +FOR EACH ROW +EXECUTE FUNCTION update_last_updated(); +``` + +### Role Identifiers + +Role Identifiers refers to name given to role which are used to manage roles within the database. They are fundamental part for security and permission system it can be used to give certain access to a user like read all the tables, write to all tables etc. + +#### Example + +In the below example how to create roles and assigned it to a user is shown: + +```sql +CREATE ROLE admin; + +GRANT admin TO alice; +``` + +### Foreign Key Identifiers + +Foreign Key Identifiers is used to give names to foreign key constraint that help in establishes a relationship between two tables, it helps in building relation between columns in different tables. It ensures that values in one column (or a set of columns) match values in another column, typically the primary key of another table. + +### Example + +In the below example two tables are created `departments` and `employees` and a foreign key relationship between them is established. + +```sql +CREATE TABLE departments ( + id SERIAL PRIMARY KEY, + name VARCHAR(100) +); + +CREATE TABLE employees ( + id SERIAL PRIMARY KEY, + name VARCHAR(100), + department_id INT, + CONSTRAINT fk_department FOREIGN KEY (department_id) + REFERENCES departments(id) + ON DELETE CASCADE +); +``` + +### Aggregate Type Identifiers + +Aggregate Type Identifiers refers to name given to aggregate function, these functions operate on set of values and return a single value. There are can build-in aggregate functions in like `COUNT`, `SUM` etc and they can also be custom aggregate function. + +#### Example + +In the below example build-in `SUM` aggregate function is used and a custom aggregate function `my_sum` is created and used. + +```sql +SELECT SUM(salary) FROM employees; + +CREATE AGGREGATE my_sum(int) ( + SFUNC = sum_state, + STYPE = int, + INITCOND = 0 +); + +SELECT my_sum(salary) FROM employees; +``` + +### Tablespace Identifiers + +Tablespace Identifiers refers to name given to tablespace, which is used to give storage location where the data will be stored. It is used control where the data is stored that can help optimise performance by distributing I/O across different storage devices. + +#### Example + +In the below example `data/my_tablespace` is the path in the filesystem where data will reside and we are specifying which tablespace to use. + +```sql +CREATE TABLESPACE my_tablespace LOCATION '/data/my_tablespace'; + +CREATE TABLE employees ( + id SERIAL PRIMARY KEY, + name VARCHAR(100) +) TABLESPACE my_tablespace; +``` From 7f688763f3c9c2d9495efdb80912d3c4442e034b Mon Sep 17 00:00:00 2001 From: shantanu <56212958+cigar-galaxy82@users.noreply.github.com> Date: Thu, 26 Sep 2024 19:53:32 +0530 Subject: [PATCH 2/5] Update identifers.md --- .../concepts/identifers/identifers.md | 46 +++++++++---------- 1 file changed, 23 insertions(+), 23 deletions(-) diff --git a/content/postgresql/concepts/identifers/identifers.md b/content/postgresql/concepts/identifers/identifers.md index 7c4dc755a34..e38c5a32cb4 100644 --- a/content/postgresql/concepts/identifers/identifers.md +++ b/content/postgresql/concepts/identifers/identifers.md @@ -1,6 +1,6 @@ --- Title: 'Identifier' -Description: 'Refers name given to databases object such as tables, columns, indexes etc.' +Description: 'Refers to the name given to databases object such as tables, columns, indexes, etc.' Subjects: - 'Data Science' - 'Computer Science' @@ -16,7 +16,7 @@ In PostgreSQL, identifiers refer to the names given to database objects such as ## Identifier Types -`Identifier Types` refers to names given to objects in a database like tables, columns, indexes and other components of a database. These names help **PostgreSQL** to interact with database. The are different types of Identifier that are usedL +`Identifier Types` refers to names given to objects in a database like tables, columns, indexes, and other components of a database. These names help **PostgreSQL** to interact with the database. The are different types of Identifier that are used. - Table Identifier - Column Identifiers @@ -34,7 +34,7 @@ In PostgreSQL, identifiers refer to the names given to database objects such as ### Table Identifier -Table Identifier refers to name given to a table in the database. These name are used for interacting with the tables, they must follow certain rules like each table must have a unique name, using only character in the name that are allowed etc. +Table Identifier refers to the name given to a table in the database. These names are used for interacting with the tables, they must follow certain rules like each table must have a unique name, using only characters in the name that are allowed, etc. #### Example @@ -62,11 +62,11 @@ CREATE TABLE "Employee Details" ( ### Column Identifiers -Column Identifier refers to name given to a colums in the database. These name are used for interacting with the columns, they must follow certain rules like each column in the same table must have a unique name but they do not need to be unique across different tables. For cases where using different tables with same column name using tables aliases is important to differentiate between them. +Column Identifier refers to the name given to columns in the database. These names are used for interacting with the columns, they must follow certain rules like each column in the same table must have a unique name but they do not need to be unique across different tables. For cases where using different tables with the same column name using table aliases is important to differentiate between them. #### Example -In the below example two different tables `employees` and `departments` with same `name` column to access both tables aliases are used. +In the below example, two different tables `employees` and `departments` with the same `name` column to access both tables aliases are used. ```sql CREATE TABLE employees ( @@ -87,11 +87,11 @@ JOIN departments dep ON emp.department_id = dep.id; ### Index Identifiers -Index Identifier refers to name given to index created on tables or columns. Indexes are database objects that help improve the speed of query operations by providing quick access paths to data. +Index Identifier refers to the name given to the index created on tables or columns. Indexes are database objects that help improve the speed of query operations by providing quick access paths to data. #### Example -In the below example `employee_name_idx` index is created on `employees` table: +In the below example, `employee_name_idx` index is created on the `employees` table: ```sql CREATE INDEX employee_name_idx ON employees (name); @@ -99,11 +99,11 @@ CREATE INDEX employee_name_idx ON employees (name); ### View Identifiers -View Identifier refers to name given to views which are virtual tables defined by a query it allows user to encapsulate complex queries under a single name and interact with the query results as if they were actual tables. +View Identifier refers to the name given to views which are virtual tables defined by a query it allows the user to encapsulate complex queries under a single name and interact with the query results as if they were actual tables. #### Example -In the below example `employee_summary` view is created which works as a shortcut to select from the `employees` table. +In the below example, the `employee_summary` view is created which works as a shortcut to select from the `employees` table. ```sql CREATE VIEW employee_summary AS @@ -115,11 +115,11 @@ SELECT * FROM employee_summary; ### Constraint Identifiers -Constraint Identifiers are name assgined to constraint that enforce rules on the data in the table. They ensure data integrity and include types such as primary keys, foreign keys, unique constraints, check constraints, and not-null constraints. +Constraint Identifiers are names assigned to constraints that enforce rules on the data in the table. They ensure data integrity and include types such as primary keys, foreign keys, unique constraints, check constraints, and not-null constraints. #### Example -In the below example we use different kinds of constraint +In the below example, how to use different kinds of constraints is shown: ```sql CREATE TABLE employees ( @@ -151,11 +151,11 @@ CREATE TABLE employees ( ### Sequence Identifiers -Sequence Identifiers refers to name assigned to sequence which is used to generate unique integer values and can be used in scenario when the purpose is to make sure each row has a distinct value, typically used for auto-incrementing primary key columns. +Sequence Identifiers refer to names assigned to sequences which are used to generate unique integer values and can be used in scenarios when the purpose is to make sure each row has a distinct value, typically used for auto-incrementing primary key columns. #### Example -In the below example a seqeunce is create and its current value is returned: +In the below example, a sequence is created and its current value is returned: ```sql -- Naming sequence using @@ -166,7 +166,7 @@ SELECT currval('employee_id_seq'); ### Function Identifiers -Function Identifiers are name assigned to custom function, these function are made for certain purpose and they return a result based on the logic inside them. +Function Identifiers are names assigned to custom functions, these functions are made for certain purposes and they return a result based on the logic inside them. #### Example @@ -184,11 +184,11 @@ SELECT add_numbers(5, 10); ### Trigger Identifiers -Trigger Identifiers are names assgined to triggers, which automatically execute a specific procedure in response to certain events on the table. First we need to create trigger by specifying when to execute and the action to be performed. +Trigger Identifiers are names assigned to triggers, which automatically execute a specific procedure in response to certain events on the table. First, a trigger is created by specifying when to execute and the action to be performed. #### Example -In the below example we have a trigger `update_timestamp` that will be trigger when a user information is updated and create a timestamp for when the update happened. +In the below example, we have a trigger `update_timestamp` that will be triggered when user information is updated and create a timestamp for when the update happened. ```sql CREATE TABLE employees ( @@ -215,11 +215,11 @@ EXECUTE FUNCTION update_last_updated(); ### Role Identifiers -Role Identifiers refers to name given to role which are used to manage roles within the database. They are fundamental part for security and permission system it can be used to give certain access to a user like read all the tables, write to all tables etc. +Role Identifiers refer to names given to roles which are used to manage roles within the database. They are fundamental parts of security and permission systems They can be used to give certain access to a user like reading all the tables, writing to all tables, etc. #### Example -In the below example how to create roles and assigned it to a user is shown: +In the below example how to create roles and assign them to a user is shown: ```sql CREATE ROLE admin; @@ -229,11 +229,11 @@ GRANT admin TO alice; ### Foreign Key Identifiers -Foreign Key Identifiers is used to give names to foreign key constraint that help in establishes a relationship between two tables, it helps in building relation between columns in different tables. It ensures that values in one column (or a set of columns) match values in another column, typically the primary key of another table. +Foreign Key Identifiers are used to give names to foreign key constraints that help in establishing a relationship between two tables, it helps in building relations between columns in different tables. It ensures that values in one column (or a set of columns) match values in another column, typically the primary key of another table. ### Example -In the below example two tables are created `departments` and `employees` and a foreign key relationship between them is established. +In the below example, two tables are created `departments` and `employees` and a foreign key relationship between them is established. ```sql CREATE TABLE departments ( @@ -253,11 +253,11 @@ CREATE TABLE employees ( ### Aggregate Type Identifiers -Aggregate Type Identifiers refers to name given to aggregate function, these functions operate on set of values and return a single value. There are can build-in aggregate functions in like `COUNT`, `SUM` etc and they can also be custom aggregate function. +Aggregate Type Identifiers refer to names given to aggregate functions, these functions operate on the set of values and return a single value. There are can build-in aggregate functions like `COUNT`, `SUM` etc and they can also be custom aggregate functions. #### Example -In the below example build-in `SUM` aggregate function is used and a custom aggregate function `my_sum` is created and used. +In the below example, a build-in `SUM` aggregate function is used and a custom aggregate function `my_sum` is created and used. ```sql SELECT SUM(salary) FROM employees; @@ -273,7 +273,7 @@ SELECT my_sum(salary) FROM employees; ### Tablespace Identifiers -Tablespace Identifiers refers to name given to tablespace, which is used to give storage location where the data will be stored. It is used control where the data is stored that can help optimise performance by distributing I/O across different storage devices. +Tablespace Identifiers refer to the name given to tablespace, which is used to give a storage location where the data will be stored. It is used to control where the data is stored which can help optimise performance by distributing I/O across different storage devices. #### Example From a670b422603a4ece24de3abd1f53a498feade60e Mon Sep 17 00:00:00 2001 From: shantanu <56212958+cigar-galaxy82@users.noreply.github.com> Date: Sun, 29 Sep 2024 17:57:14 +0530 Subject: [PATCH 3/5] Update identifers.md --- .../concepts/identifers/identifers.md | 23 +++++++++---------- 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/content/postgresql/concepts/identifers/identifers.md b/content/postgresql/concepts/identifers/identifers.md index e38c5a32cb4..e9153e73f11 100644 --- a/content/postgresql/concepts/identifers/identifers.md +++ b/content/postgresql/concepts/identifers/identifers.md @@ -1,6 +1,6 @@ --- Title: 'Identifier' -Description: 'Refers to the name given to databases object such as tables, columns, indexes, etc.' +Description: 'Refers to the name given to database objects such as tables, columns, indexes, etc.' Subjects: - 'Data Science' - 'Computer Science' @@ -12,13 +12,12 @@ CatalogContent: - 'paths/analyze-data-with-sql' --- -In PostgreSQL, identifiers refer to the names given to database objects such as tables, columns, indexes, views, sequences, and other database components. +In PostgreSQL, **Identifiers** refer to the names given to database objects such as tables, columns, indexes, views, sequences, and other database components. ## Identifier Types -`Identifier Types` refers to names given to objects in a database like tables, columns, indexes, and other components of a database. These names help **PostgreSQL** to interact with the database. The are different types of Identifier that are used. - -- Table Identifier +`Identifier Types` refers to names given to objects in a database like tables, columns, indexes, and other components of a database. These names help **PostgreSQL** to interact with the database. There are different types of identifiers that are used. +- Table Identifiers - Column Identifiers - Index Identifiers - View Identifiers @@ -34,7 +33,7 @@ In PostgreSQL, identifiers refer to the names given to database objects such as ### Table Identifier -Table Identifier refers to the name given to a table in the database. These names are used for interacting with the tables, they must follow certain rules like each table must have a unique name, using only characters in the name that are allowed, etc. +A Table Identifier refers to the name given to a table in the database. These names are used for interacting with the tables, and must follow certain rules such as each table must have a unique name, using only characters in the name that are allowed, etc. #### Example @@ -133,11 +132,11 @@ CREATE TABLE employees ( ### Schema Identifiers -Schema Identifiers refers to name given to schemas, which are logical container for building relation between tables, views, indexes and other database objects. It helps in organizing database object and avoid conflicts. +Schema Identifiers refer to names given to schemas, which are a logical container for building relations between tables, views, indexes, and other database objects. It helps organize database objects and avoid conflicts. #### Example -In the below example we use different kinds of constraint +In the below example, we use different kinds of constraint ```sql CREATE TABLE employees ( @@ -151,7 +150,7 @@ CREATE TABLE employees ( ### Sequence Identifiers -Sequence Identifiers refer to names assigned to sequences which are used to generate unique integer values and can be used in scenarios when the purpose is to make sure each row has a distinct value, typically used for auto-incrementing primary key columns. +Sequence Identifiers refer to names assigned to sequences that are used to generate unique integer values and can be used in scenarios when the purpose is to make sure each row has a distinct value, typically used for auto-incrementing primary key columns. #### Example @@ -170,7 +169,7 @@ Function Identifiers are names assigned to custom functions, these functions are #### Example -In the below example a custom function `add_numbers` used to add numbers passed to it: +In the below example a custom function `add_numbers` is used to add numbers passed to it: ```sql CREATE FUNCTION add_numbers (a INT, b INT) RETURNS INT AS $$ @@ -215,7 +214,7 @@ EXECUTE FUNCTION update_last_updated(); ### Role Identifiers -Role Identifiers refer to names given to roles which are used to manage roles within the database. They are fundamental parts of security and permission systems They can be used to give certain access to a user like reading all the tables, writing to all tables, etc. +Role Identifiers refer to names given to roles that are used to manage roles within the database. They are fundamental parts of security and permission systems They can be used to give certain access to a user like reading all the tables, writing to all tables, etc. #### Example @@ -257,7 +256,7 @@ Aggregate Type Identifiers refer to names given to aggregate functions, these fu #### Example -In the below example, a build-in `SUM` aggregate function is used and a custom aggregate function `my_sum` is created and used. +In the below example, a built-in `SUM` aggregate function is used and a custom aggregate function `my_sum` is created and used. ```sql SELECT SUM(salary) FROM employees; From 0fd5986848bcf29240d0cba46d4b2d5304d687ea Mon Sep 17 00:00:00 2001 From: Daksha Deep Date: Fri, 15 Nov 2024 16:08:34 +0530 Subject: [PATCH 4/5] Changes --- .../concepts/identifers/identifers.md | 284 +----------------- 1 file changed, 14 insertions(+), 270 deletions(-) diff --git a/content/postgresql/concepts/identifers/identifers.md b/content/postgresql/concepts/identifers/identifers.md index e9153e73f11..c999c3ea9a8 100644 --- a/content/postgresql/concepts/identifers/identifers.md +++ b/content/postgresql/concepts/identifers/identifers.md @@ -16,273 +16,17 @@ In PostgreSQL, **Identifiers** refer to the names given to database objects such ## Identifier Types -`Identifier Types` refers to names given to objects in a database like tables, columns, indexes, and other components of a database. These names help **PostgreSQL** to interact with the database. There are different types of identifiers that are used. -- Table Identifiers -- Column Identifiers -- Index Identifiers -- View Identifiers -- Constraint Identifiers -- Schema Identifiers -- Sequence Identifiers -- Function Identifiers -- Trigger Identifiers -- Role Identifiers -- Foreign Key Identifiers -- Aggregate Type Identifiers -- Tablespace Identifiers - -### Table Identifier - -A Table Identifier refers to the name given to a table in the database. These names are used for interacting with the tables, and must follow certain rules such as each table must have a unique name, using only characters in the name that are allowed, etc. - -#### Example - -In the below example `employees` is the table identifier: - -```pseudo --- Creating a Table with Standard identifiers -CREATE TABLE employees ( - id SERIAL PRIMARY KEY, - name VARCHAR(100) -); - --- Creating a Table with case-sensitive identifiers -CREATE TABLE "Employees" ( - "ID" SERIAL PRIMARY KEY, - "Name" VARCHAR(100) -); - --- Creating a table with special character identifiers -CREATE TABLE "Employee Details" ( - "Emp ID" SERIAL PRIMARY KEY, - "First Name" VARCHAR(100) -); -``` - -### Column Identifiers - -Column Identifier refers to the name given to columns in the database. These names are used for interacting with the columns, they must follow certain rules like each column in the same table must have a unique name but they do not need to be unique across different tables. For cases where using different tables with the same column name using table aliases is important to differentiate between them. - -#### Example - -In the below example, two different tables `employees` and `departments` with the same `name` column to access both tables aliases are used. - -```sql -CREATE TABLE employees ( - id SERIAL PRIMARY KEY, - name VARCHAR(100), - department_id INT -); - -CREATE TABLE departments ( - id SERIAL PRIMARY KEY, - name VARCHAR(100) -); - -SELECT emp.name AS employee_name, dep.name AS department_name -FROM employees emp -JOIN departments dep ON emp.department_id = dep.id; -``` - -### Index Identifiers - -Index Identifier refers to the name given to the index created on tables or columns. Indexes are database objects that help improve the speed of query operations by providing quick access paths to data. - -#### Example - -In the below example, `employee_name_idx` index is created on the `employees` table: - -```sql -CREATE INDEX employee_name_idx ON employees (name); -``` - -### View Identifiers - -View Identifier refers to the name given to views which are virtual tables defined by a query it allows the user to encapsulate complex queries under a single name and interact with the query results as if they were actual tables. - -#### Example - -In the below example, the `employee_summary` view is created which works as a shortcut to select from the `employees` table. - -```sql -CREATE VIEW employee_summary AS -SELECT name, department, salary -FROM employees; - -SELECT * FROM employee_summary; -``` - -### Constraint Identifiers - -Constraint Identifiers are names assigned to constraints that enforce rules on the data in the table. They ensure data integrity and include types such as primary keys, foreign keys, unique constraints, check constraints, and not-null constraints. - -#### Example - -In the below example, how to use different kinds of constraints is shown: - -```sql -CREATE TABLE employees ( - id SERIAL PRIMARY KEY, - department_id INT, - FOREIGN KEY (department_id) REFERENCES departments(id), - email VARCHAR(255) UNIQUE, - price NUMERIC CHECK (price > 0) -); -``` - -### Schema Identifiers - -Schema Identifiers refer to names given to schemas, which are a logical container for building relations between tables, views, indexes, and other database objects. It helps organize database objects and avoid conflicts. - -#### Example - -In the below example, we use different kinds of constraint - -```sql -CREATE TABLE employees ( - id SERIAL PRIMARY KEY, - department_id INT, - FOREIGN KEY (department_id) REFERENCES departments(id), - email VARCHAR(255) UNIQUE, - price NUMERIC CHECK (price > 0) -); -``` - -### Sequence Identifiers - -Sequence Identifiers refer to names assigned to sequences that are used to generate unique integer values and can be used in scenarios when the purpose is to make sure each row has a distinct value, typically used for auto-incrementing primary key columns. - -#### Example - -In the below example, a sequence is created and its current value is returned: - -```sql --- Naming sequence using -CREATE SEQUENCE employee_id_seq; - -SELECT currval('employee_id_seq'); -``` - -### Function Identifiers - -Function Identifiers are names assigned to custom functions, these functions are made for certain purposes and they return a result based on the logic inside them. - -#### Example - -In the below example a custom function `add_numbers` is used to add numbers passed to it: - -```sql -CREATE FUNCTION add_numbers (a INT, b INT) RETURNS INT AS $$ -BEGIN - RETURN a + b; -END; -$$ LANGUAGE plpgsql; - -SELECT add_numbers(5, 10); -``` - -### Trigger Identifiers - -Trigger Identifiers are names assigned to triggers, which automatically execute a specific procedure in response to certain events on the table. First, a trigger is created by specifying when to execute and the action to be performed. - -#### Example - -In the below example, we have a trigger `update_timestamp` that will be triggered when user information is updated and create a timestamp for when the update happened. - -```sql -CREATE TABLE employees ( - id SERIAL PRIMARY KEY, - name VARCHAR(100), - position VARCHAR(100), - last_updated TIMESTAMP -); - -CREATE OR REPLACE FUNCTION update_last_updated() -RETURNS TRIGGER AS $$ -BEGIN - -- Set the last_updated column to the current time - NEW.last_updated = NOW(); - RETURN NEW; -END; -$$ LANGUAGE plpgsql; - -CREATE TRIGGER update_timestamp -BEFORE UPDATE ON employees -FOR EACH ROW -EXECUTE FUNCTION update_last_updated(); -``` - -### Role Identifiers - -Role Identifiers refer to names given to roles that are used to manage roles within the database. They are fundamental parts of security and permission systems They can be used to give certain access to a user like reading all the tables, writing to all tables, etc. - -#### Example - -In the below example how to create roles and assign them to a user is shown: - -```sql -CREATE ROLE admin; - -GRANT admin TO alice; -``` - -### Foreign Key Identifiers - -Foreign Key Identifiers are used to give names to foreign key constraints that help in establishing a relationship between two tables, it helps in building relations between columns in different tables. It ensures that values in one column (or a set of columns) match values in another column, typically the primary key of another table. - -### Example - -In the below example, two tables are created `departments` and `employees` and a foreign key relationship between them is established. - -```sql -CREATE TABLE departments ( - id SERIAL PRIMARY KEY, - name VARCHAR(100) -); - -CREATE TABLE employees ( - id SERIAL PRIMARY KEY, - name VARCHAR(100), - department_id INT, - CONSTRAINT fk_department FOREIGN KEY (department_id) - REFERENCES departments(id) - ON DELETE CASCADE -); -``` - -### Aggregate Type Identifiers - -Aggregate Type Identifiers refer to names given to aggregate functions, these functions operate on the set of values and return a single value. There are can build-in aggregate functions like `COUNT`, `SUM` etc and they can also be custom aggregate functions. - -#### Example - -In the below example, a built-in `SUM` aggregate function is used and a custom aggregate function `my_sum` is created and used. - -```sql -SELECT SUM(salary) FROM employees; - -CREATE AGGREGATE my_sum(int) ( - SFUNC = sum_state, - STYPE = int, - INITCOND = 0 -); - -SELECT my_sum(salary) FROM employees; -``` - -### Tablespace Identifiers - -Tablespace Identifiers refer to the name given to tablespace, which is used to give a storage location where the data will be stored. It is used to control where the data is stored which can help optimise performance by distributing I/O across different storage devices. - -#### Example - -In the below example `data/my_tablespace` is the path in the filesystem where data will reside and we are specifying which tablespace to use. - -```sql -CREATE TABLESPACE my_tablespace LOCATION '/data/my_tablespace'; - -CREATE TABLE employees ( - id SERIAL PRIMARY KEY, - name VARCHAR(100) -) TABLESPACE my_tablespace; -``` +`Identifier Types` refers to names given to objects in a database like tables, columns, indexes, and other database components. These names help **PostgreSQL** to interact with the database. Different types of identifiers are used: +- **Table Identifiers**: Used to uniquely identify tables in a database. +- **Column Identifiers**: Specify individual columns within a table. +- **Index Identifiers**: Represent indexes created for faster query performance. +- **View Identifiers**: Name views, which are virtual tables based on queries. +- **Constraint Identifiers**: Refer to rules like primary keys or unique constraints. +- **Schema Identifiers**: Distinguish schemas that group related database objects. +- **Sequence Identifiers**: Denote sequences used for auto-incrementing values. +- **Function Identifiers**: Name functions or stored procedures. +- **Trigger Identifiers**: Refer to triggers that execute actions automatically. +- **Role Identifiers**: Specify roles for managing user permissions. +- **Foreign Key Identifiers**: Represent foreign key constraints linking tables. +- **Aggregate Type Identifiers**: Identify aggregate data types for grouped queries. +- **Tablespace Identifiers**: Name tablespaces where database objects are stored. From 09a2b191ef18220d7f6ce1c08dfee49b20ffba7a Mon Sep 17 00:00:00 2001 From: Daksha Deep Date: Fri, 15 Nov 2024 16:18:18 +0530 Subject: [PATCH 5/5] formating --- .../concepts/identifers/identifers.md | 27 ++++++++++--------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/content/postgresql/concepts/identifers/identifers.md b/content/postgresql/concepts/identifers/identifers.md index c999c3ea9a8..415724d7c5f 100644 --- a/content/postgresql/concepts/identifers/identifers.md +++ b/content/postgresql/concepts/identifers/identifers.md @@ -17,16 +17,17 @@ In PostgreSQL, **Identifiers** refer to the names given to database objects such ## Identifier Types `Identifier Types` refers to names given to objects in a database like tables, columns, indexes, and other database components. These names help **PostgreSQL** to interact with the database. Different types of identifiers are used: -- **Table Identifiers**: Used to uniquely identify tables in a database. -- **Column Identifiers**: Specify individual columns within a table. -- **Index Identifiers**: Represent indexes created for faster query performance. -- **View Identifiers**: Name views, which are virtual tables based on queries. -- **Constraint Identifiers**: Refer to rules like primary keys or unique constraints. -- **Schema Identifiers**: Distinguish schemas that group related database objects. -- **Sequence Identifiers**: Denote sequences used for auto-incrementing values. -- **Function Identifiers**: Name functions or stored procedures. -- **Trigger Identifiers**: Refer to triggers that execute actions automatically. -- **Role Identifiers**: Specify roles for managing user permissions. -- **Foreign Key Identifiers**: Represent foreign key constraints linking tables. -- **Aggregate Type Identifiers**: Identify aggregate data types for grouped queries. -- **Tablespace Identifiers**: Name tablespaces where database objects are stored. + +- **Table Identifiers**: Used to uniquely identify tables in a database. +- **Column Identifiers**: Specify individual columns within a table. +- **Index Identifiers**: Represent indexes created for faster query performance. +- **View Identifiers**: Name views, which are virtual tables based on queries. +- **Constraint Identifiers**: Refer to rules like primary keys or unique constraints. +- **Schema Identifiers**: Distinguish schemas that group related database objects. +- **Sequence Identifiers**: Denote sequences used for auto-incrementing values. +- **Function Identifiers**: Name functions or stored procedures. +- **Trigger Identifiers**: Refer to triggers that execute actions automatically. +- **Role Identifiers**: Specify roles for managing user permissions. +- **Foreign Key Identifiers**: Represent foreign key constraints linking tables. +- **Aggregate Type Identifiers**: Identify aggregate data types for grouped queries. +- **Tablespace Identifiers**: Name tablespaces where database objects are stored.