SQL

Key Concepts Related to Database Tables

  1. Primary Key:
    • A column or a set of columns that uniquely identifies each row in the table.
    • Example: EmployeeID in the Employees table.
  2. Foreign Key:
    • A column or a set of columns in one table that references the primary key in another table, creating a relationship between the two tables.
    • Example: DepartmentID in the Employees table might reference DepartmentID in a Departments table.
  3. Constraints:
    • Rules applied to columns to ensure data integrity.
    • Examples include NOT NULL, UNIQUE, CHECK, DEFAULT, and FOREIGN KEY.

Creating a Database Table

Using SQL, you can create a table with a CREATE TABLE statement:


CREATE TABLE Employees (
    EmployeeID INT PRIMARY KEY,
    FirstName VARCHAR(50) NOT NULL,
    LastName VARCHAR(50) NOT NULL,
    DepartmentID INT,
    Salary DECIMAL(10, 2),
    HireDate DATE
);

Inserting Data into a Table

To insert data into a table, use the INSERT INTO statement:


INSERT INTO Employees (EmployeeID, FirstName, LastName, DepartmentID, Salary, HireDate)
    VALUES (1, 'John', 'Doe', 1, 60000.00, '2020-01-15'),
    (2, 'Jane', 'Smith', 2, 65000.00, '2019-03-22'),
    (3, 'Emily', 'Jones', 3, 55000.00, '2021-06-30');

Querying Data from a Table

To retrieve data, use the SELECT statement:


SELECT * FROM Employees;

SELECT FirstName, LastName, DepartmentID
FROM Employees
WHERE Salary > 60000;