Microsoft SQL Server

Chapter 3 - Database Tables, Columns and Rows

SQL

SQL (Structured Query Language) is a standard programming language used to manage and manipulate relational databases. It allows users to create, read, update, and delete data (commonly referred to as CRUD operations). SQL is widely used in database management systems like Microsoft SQL Server to interact with databases efficiently. SQL commands are used to retrieve data, create tables, modify structures, and manage users. Whether you are handling customer records or tracking sales, SQL plays a vital role in the process.

  1. Basic SQL Commands

    SQL commands are divided into several categories:

    • DDL (Data Definition Language): Used to define and manage database structures (e.g., creating tables).
    • DML (Data Manipulation Language): Used to insert, update, delete, and query data.
    • DCL (Data Control Language): Used to control access to data.
    • TCL (Transaction Control Language): Used to manage transactions.

    Here's an example of a basic DML query to select all data from a Products table:

    SELECT*FROM Products;
    • The SELECT command is used to retrieve data from the table.
    • The * symbol retrieves all columns from the table.
  2. Creating a Table in SQL

    A fundamental use of SQL is creating tables. Here's how to create a Sales table to store data on store sales:

    CREATE TABLE Sales (
        SaleID INT PRIMARY KEY,
        ProductID INT,
        SaleDate DATE,
        QuantitySold INT
    );
    
    • This creates a Sales table with columns for SaleID, ProductID, SaleDate, and QuantitySold.
    • The PRIMARY KEY constraint ensures that SaleID uniquely identifies each sale.
  3. Inserting Data into a Table

    Once a table is created, you can insert data using the INSERT INTO statement. For example, inserting a sale record into the Sales table:

    INSERT INTO Sales (SaleID, ProductID, SaleDate, QuantitySold)
    VALUES (1, 101, '2023-09-01', 5);
    
    • This adds a new row where SaleID is 1, ProductID is 101, SaleDate is '2023-09-01', and QuantitySold is 5.
  4. Querying Data from a Table

    SQL is widely used for retrieving data. For example, to get sales records for a specific product:

    SELECT * FROM Sales WHERE ProductID = 101;
    
    • This retrieves all rows from the Sales table where ProductID equals 101.
  5. Updating Data in a Table

    The UPDATE command allows you to modify existing data. To update the quantity of a product sold:

    UPDATE Sales
    SET QuantitySold = 10
    WHERE SaleID = 1;
    
    • This updates the QuantitySold to 10 where SaleID is 1.
  1. Deleting Data from a Table

    If you need to remove data, the DELETE statement is used. For example, to delete a sale record:

    DELETE FROM Sales
    WHERE SaleID = 1;
    
    • This deletes the row where SaleID is 1 from the Sales table.

    SQL is a powerful language that helps manage databases efficiently, and these basic operations form the foundation of any interaction with a SQL-based database system like Microsoft SQL Server.

Tansy SQL Course - SQL - 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