# Attributes

Attributes are the properties or characteristics of an entity in a database. They represent the data stored about the entity and are implemented as columns in a table. Graphically represented as ovals or circles.&#x20;

Here are examples of different types of attributes with explanations,

### **1. Simple Attribute**

* **Definition:** Cannot be divided into smaller parts.
* **Example:**
  * **Entity:** `Student`
  * **Attributes:** `StudentID`, `Name`, `Age`
  * Explanation: `StudentID` is a simple, indivisible identifier for a student.

**Table Example**

```sql
CREATE TABLE Student (
    StudentID SERIAL PRIMARY KEY,
    Name TEXT NOT NULL,
    Age INT
);
```

<figure><img src="https://2781136461-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FK4KaCc7tzh0tRXwFV2A5%2Fuploads%2FinGd7EecHMuDcoe85iYg%2Fimage.png?alt=media&#x26;token=ba2ce8a5-d0e8-4d75-b847-98fe6b43e51a" alt=""><figcaption></figcaption></figure>

### **2. Composite Attribute**

* **Definition:** Can be divided into smaller sub-parts, each with independent meaning.
* **Example:**
  * **Entity:** `Student`
  * **Attribute:** `FullName` (composed of `FirstName` and `LastName`)
  * Explanation: `FullName` can be divided into `FirstName` and `LastName`.

**Table Example**

```sql
CREATE TABLE Student (
    StudentID SERIAL PRIMARY KEY,
    FirstName TEXT NOT NULL,
    LastName TEXT NOT NULL
);
```

<figure><img src="https://2781136461-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FK4KaCc7tzh0tRXwFV2A5%2Fuploads%2F808C0GuMF7bv5mWPffD5%2Fimage.png?alt=media&#x26;token=f6a1924b-2739-4107-9ab7-607e6a92e929" alt=""><figcaption></figcaption></figure>

### **3. Derived Attribute**

* **Definition:** Can be calculated from other attributes.
* **Example:**
  * **Entity:** `Employee`
  * **Attributes:** `DateOfBirth`, `Age`
  * Explanation: `Age` is derived from `DateOfBirth`.

**Table Example**

```sql
CREATE TABLE Employee (
    EmployeeID SERIAL PRIMARY KEY,
    Name TEXT NOT NULL,
    DateOfBirth DATE
);
-- Age can be calculated as:
-- SELECT DATE_PART('year', AGE(DateOfBirth)) AS Age FROM Employee;
```

<figure><img src="https://2781136461-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FK4KaCc7tzh0tRXwFV2A5%2Fuploads%2FaNK0kaq347NtZwgpIFKC%2Fimage.png?alt=media&#x26;token=56f6175b-a793-41e7-9ab6-46ed1ecea92e" alt=""><figcaption></figcaption></figure>

### **4. Multivalued Attribute**

* **Definition:** Can have more than one value for a single entity.
* **Example:**
  * **Entity:** `Student`
  * **Attribute:** `PhoneNumbers` (a student may have multiple phone numbers)
  * Explanation: A student can have a home phone and a mobile phone.

**Table Example**

To handle multivalued attributes, we use a separate table

```sql
CREATE TABLE Student (
    StudentID SERIAL PRIMARY KEY,
    Name TEXT NOT NULL
);

CREATE TABLE StudentPhone (
    StudentID INT REFERENCES Student(StudentID),
    PhoneNumber TEXT NOT NULL
);
```

<figure><img src="https://2781136461-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FK4KaCc7tzh0tRXwFV2A5%2Fuploads%2FP6XaUX0ck2fx6kc9GhsF%2Fimage.png?alt=media&#x26;token=f76aaf27-b03a-404f-9d64-8e54a5feabe0" alt=""><figcaption></figcaption></figure>

### **5. Key Attribute**

* **Definition:** Uniquely identifies an entity in a table.
* **Example:**
  * **Entity:** `Student`
  * **Attribute:** `StudentID`
  * Explanation: `StudentID` is a unique identifier for each student.

**Table Example**

```sql
CREATE TABLE Student (
    StudentID SERIAL PRIMARY KEY,
    Name TEXT NOT NULL
);
```

<figure><img src="https://2781136461-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FK4KaCc7tzh0tRXwFV2A5%2Fuploads%2FfBbJRfAEIbyx5GGIIKvr%2Fimage.png?alt=media&#x26;token=034d1efb-93f0-4001-9cce-790be5b77416" alt=""><figcaption></figcaption></figure>

### Summary

| **Type** | **Description** | **Example** |
| -------- | --------------- | ----------- |

| **Simple** | Cannot be divided further. | `Name`, `Age` |
| ---------- | -------------------------- | ------------- |

| **Composite** | Can be divided into sub-parts. | `FullName` → `FirstName`, `LastName` |
| ------------- | ------------------------------ | ------------------------------------ |

| **Derived** | Calculated from other attributes. | `Age` from `DateOfBirth` |
| ----------- | --------------------------------- | ------------------------ |

| **Multivalued** | Can have multiple values for one entity. | `PhoneNumbers` |
| --------------- | ---------------------------------------- | -------------- |

| **Key** | Uniquely identifies an entity. | `StudentID` |
| ------- | ------------------------------ | ----------- |

| **Foreign Key** | References the primary key of another entity. | `StudentID` in `Enrollment` table |
| --------------- | --------------------------------------------- | --------------------------------- |


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://courses.parottasalna.com/database-engineering/entity-relationship-diagram/attributes.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
