Oracle

Chapter 3 - Database Tables, Columns and Rows

SQL


In Oracle, SQL (Structured Query Language) is the standard language for managing and manipulating data in relational databases. It enables you to interact with tables, columns, and rows to perform operations like inserting data, querying information, updating records, and deleting data. SQL provides a robust way to manage database objects such as those representing books, authors, library memberships, and rentals.

Here's a beginner-friendly guide to using SQL in Oracle:

1. Creating a Table

A table is a structured collection of data organized in rows and columns. Each column holds a specific type of data, and each row represents a record.

  • Syntax:
    CREATE TABLE books (
      book_id NUMBER PRIMARY KEY,
      title VARCHAR2(255),
      author_id NUMBER,
      publication_year NUMBER
    );
    
  • In this example:
    • book_id is a unique identifier for each book.
    • title holds the book's title.
    • author_id links the book to an author.

2. Inserting Data into a Table

You can insert rows of data into a table using the INSERT INTO statement.

  • Syntax:
    INSERT INTO books (book_id, title, author_id, publication_year)
    VALUES (1, '1984', 1, 1949);
    
  • Key points:
    • Insert values in the order of the column definitions.
    • Use single quotes for string values like '1984'.

3. Querying Data with SELECT

The SELECT statement is used to retrieve data from tables. You can specify which columns to display and apply conditions to filter the results.

  • Basic Query:
    SELECT title, publication_year
    FROM books;
    
  • Filtering Results:
    SELECT title
    FROM books
    WHERE publication_year > 1950;
    
  • This query selects books published after 1950.

4. Updating Data in a Table

The UPDATE statement allows you to modify existing data in a table.

  • Syntax:
    UPDATE books
    SET title = 'Animal Farm'
    WHERE book_id = 1;
    
  • Bullet points:
    • The SET clause specifies the new value.
    • The WHERE clause ensures only the intended rows are updated.

5. Deleting Data from a Table

You can remove rows from a table using the DELETE statement.

  • Syntax:
    DELETE FROM books
    WHERE book_id = 1;
    
  • This deletes the row where the book_id is 1.

6. Joining Tables

A JOIN operation allows you to retrieve related data from multiple tables. This is especially useful in databases with related entities like books and authors.

  • Example (joining books and authors):
    SELECT books.title, authors.author_name
    FROM books
    JOIN authors ON books.author_id = authors.author_id;
    
  • Bullet points:
    • The JOIN combines rows from both tables based on a related column (author_id in this case).
    • This query returns the book title and corresponding author's name.

7. Filtering and Sorting Data

You can further refine your results using WHERE for filtering and ORDER BY for sorting.

  • Syntax for filtering:
    SELECT *
    FROM books
    WHERE publication_year > 2000;
    
  • Syntax for sorting:
    SELECT *
    FROM books
    ORDER BY publication_year DESC;
    
  • The ORDER BY clause sorts the results in descending order by publication year.

By understanding these basic SQL operations, you can efficiently manage tables, columns, and rows in an Oracle database.

Tansy SQL Course | SQL | Chapter 3 | Lesson 3 - Video Thumbnail

Introduction to SQL

SQL (Structured Query Language) is a standard language for storing, manipulating, and retrieving data in databases.

1. Creating a Table

To store data, we first create a table. Here's how to create an Authors table:

CREATE TABLE Authors (
    AuthorID int,
    FirstName varchar(255),
    LastName varchar(255),
    BirthYear int
);
  • CREATE TABLE Authors - starts the command to create a new table named 'Authors'.
  • AuthorID int, FirstName varchar(255), etc. - defines the columns and their data types.

2. Inserting Data

Once a table is created, you can add data to it:

INSERT INTO Authors (AuthorID, FirstName, LastName, BirthYear)
VALUES (1, 'Jane', 'Austen', 1775);
  • INSERT INTO Authors - specifies the table to insert data into.
  • VALUES (1, 'Jane', 'Austen', 1775) - defines the data being inserted.

3. Selecting Data

To retrieve and view data from the table:

SELECT FirstName, LastName FROM Authors;
  • SELECT FirstName, LastName - indicates the columns to retrieve.
  • FROM Authors - specifies the table to select data from.

4. Updating Data

If data needs correction or updating:

UPDATE Authors
SET BirthYear = 1776
WHERE AuthorID = 1;
  • UPDATE Authors - indicates the table where the update will occur.
  • SET BirthYear = 1776 - specifies the new value for a column.
  • WHERE AuthorID = 1 - identifies which record(s) to update.

5. Deleting Data

To remove data from the table:

DELETE FROM Authors
WHERE AuthorID = 1;
  • DELETE FROM Authors - specifies from which table to delete data.
  • WHERE AuthorID = 1 - identifies which record(s) to delete.

Each SQL statement serves a specific function, allowing for efficient management and manipulation of data within databases.

Understanding SQL Command Categories

Data Definition Language (DDL)

DDL commands define, alter, and manage the structure of database objects like tables and indexes. These commands affect the schema and architecture of the database rather than the data itself.

  • CREATE: Creates new database objects.
  • ALTER: Modifies existing database objects.
  • DROP: Deletes objects from the database.
  • TRUNCATE: Removes all records from a table, deleting the space allocated for the records.

Data Manipulation Language (DML)

DML commands are used for managing data within database tables. These commands allow adding, updating, or deleting data.

  • INSERT: Adds new rows to a table.
  • UPDATE: Modifies existing data within a table.
  • DELETE: Removes rows from a table.
  • SELECT: Queries and retrieves data based on specific criteria. Often considered part of DQL, but crucial for data manipulation.

Data Query Language (DQL)

DQL focuses on querying and retrieving data. It allows fetching and organizing data from one or more tables.

  • SELECT: The primary command used to query the database for specific information, utilizing clauses like WHERE, GROUP BY, and ORDER BY to refine results.
Comments(0 comments)

Comments Not Found