Oracle

Chapter 3 - Database Tables, Columns and Rows

ROWs and COLUMNs


In Oracle databases, rows and columns are fundamental concepts that define how data is organized within a table. A column represents a specific attribute or field in a table, such as "book_title" or "author_name." Each column holds a specific type of data, like text or numbers. A row is a single record that contains data for all the columns in the table. Each row represents a unique entry or entity, such as a specific book or an individual author. Together, rows and columns allow you to structure and retrieve data efficiently.

Key Concepts of Rows and Columns:

  1. Columns:

    • Each column defines a specific data attribute.
    • Columns have a defined data type (e.g., VARCHAR2 for text, NUMBER for numeric data).
    • All rows in the table will have data or a null value for each column.

    Example of creating a table with columns:

    CREATE TABLE books (
        book_id NUMBER PRIMARY KEY,
        book_title VARCHAR2(100),
        author_id NUMBER,
        publication_year NUMBER
    );
    
  2. Rows:

    • A row represents a single record in a table.
    • Each row contains values for every column in the table.
    • Rows are added through the INSERT command and can be modified using UPDATE.

    Example of inserting a row:

    INSERT INTO books (book_id, book_title, author_id, publication_year)
    VALUES (1, 'Oracle Database Concepts', 101, 2020);
    
  3. Defining and Managing Columns:

    • Columns are created during the table creation process.
    • You can specify constraints such as NOT NULL or UNIQUE for columns to enforce data rules.

    Example:

    CREATE TABLE authors (
        author_id NUMBER PRIMARY KEY,
        author_name VARCHAR2(100) NOT NULL,
        birth_year NUMBER
    );
    
  4. Managing Rows in a Table:

    • You can add, update, or delete rows as needed.
    • Queries can retrieve specific rows based on conditions.

    Example of updating a row:

    UPDATE books
    SET publication_year = 2021
    WHERE book_id = 1;
    
  5. Queries on Rows and Columns:

    • Data retrieval involves selecting specific columns from rows that meet certain criteria.
    • The SELECT statement allows you to retrieve data from specified columns.

    Example:

    SELECT book_title, publication_year
    FROM books
    WHERE author_id = 101;
    

By understanding rows and columns, you can efficiently structure, store, and retrieve data in an Oracle database, ensuring your data management is organized and optimized.

Tansy SQL Course | ROWs and COLUMNs | Chapter 3 | Lesson 2 - Video Thumbnail

Understanding Rows and Columns in SQL Tables

Columns

Columns represent the attributes or fields of the data table, each designed to hold a specific type of information.

  • Data Type: Dictates the kind of data a column can store (e.g., integers, text, dates).
  • Column Name: A unique name within the table that identifies the column.

Rows

Rows represent individual records or data entries, containing a unique instance of data for the columns.

  • Primary Key: Uniquely identifies each row in the table.
  • Uniqueness: Each row should have a unique combination of values.
Example Table: Employees
EmployeeIDFirstNameLastNameEmailHireDate
1JohnDoejohn.doe@example.com2020-01-10
2JaneSmithjane.smith@example.com2020-02-15

SQL Operations on Rows and Columns

Inserting Data

Adds new rows to the table.

INSERT INTO Employees (EmployeeID, FirstName, LastName, Email, HireDate) VALUES (3, 'Alice', 'Johnson', 'alice.johnson@example.com', '2020-03-20');
Querying Data

Retrieves data from the table, potentially filtering both rows and columns.

SELECT FirstName, LastName FROM Employees WHERE HireDate > '2020-01-01';
Updating Data

Modifies existing rows in the table.

UPDATE Employees SET Email = 'new.email@example.com' WHERE EmployeeID = 1;
Deleting Data

Removes rows from the table.

DELETE FROM Employees WHERE EmployeeID = 2;

SQL Column Components

SQL column components define the structure, constraints, and behavior of data within a database table. Below are the key components and attributes for SQL columns:

1. Data Type

Specifies the kind of data a column can store. Common types include:

  • INTEGER: For whole numbers.
  • VARCHAR(n): For variable-length strings, where n defines the maximum length.
  • CHAR(n): For fixed-length strings, with n defining the string length.
  • DATE: For dates.
  • FLOAT, DOUBLE: For floating-point numbers.

2. Default Value

Automatically assigns a specific value if no value is provided during row insertion.

Age INT DEFAULT 18

3. Not Null Constraint

Ensures a column cannot store a NULL value, requiring every row to have a value for this column.

Name VARCHAR(100) NOT NULL

4. Unique Constraint

Ensures all values in the column are unique across the table, important for non-primary key uniqueness.

Email VARCHAR(100) UNIQUE

5. Primary Key Constraint

A unique identifier for each row, cannot be NULL and must be unique. Can be a single column or a combination of columns.

CustomerID INT PRIMARY KEY

6. Foreign Key Constraint

Establishes a link between the data in two tables, referencing the primary key of another table to enforce data integrity.

OrderID INT FOREIGN KEY REFERENCES Orders(OrderID)

7. Check Constraint

Specifies a condition on a column that must be true for all rows, used to enforce domain integrity.

Age INT CHECK (Age >= 18)

8. Auto Increment

Automatically assigns a unique value to the column for each new row, commonly used for ID columns.

CustomerID INT AUTO_INCREMENT
Example

Combining these components, here's an example of a table creation statement in SQL:

CREATE TABLE Customers (
    CustomerID INT AUTO_INCREMENT PRIMARY KEY,
    Name VARCHAR(100) NOT NULL,
    Email VARCHAR(100) UNIQUE,
    Age INT DEFAULT 18 CHECK (Age >= 18),
    Address VARCHAR(255)
);

Each column component plays a specific role in defining how data is stored, validated, and related to other tables' data.

Employee Table with Data

Employee table with data
Comments(0 comments)

Comments Not Found