Defining Constraints and Data Types

Tutorial 2 of 5

1. Introduction

1.1 Tutorial's Goal

In this tutorial, we will focus on defining data types and constraints in SQL. These two elements are essential to ensure the integrity and optimal performance of your database.

1.2 Learning Outcomes

By the end of this tutorial, you will be able to:
- Understand the concept of data types in SQL
- Define constraints in SQL
- Apply these concepts in your database design

1.3 Prerequisites

Basic understanding of SQL commands and database concepts will be beneficial.

2. Step-by-Step Guide

2.1 Data Types in SQL

Data types in SQL define the kind of data that can be stored inside a table column. Different types of data include integer, date, string, etc.

2.2 Constraints in SQL

Constraints in SQL are rules enforced on data columns on a table. They ensure the accuracy and reliability of the data in the table.

3. Code Examples

3.1 Example: Defining Data Types

CREATE TABLE Employees (
    ID INT,
    Name VARCHAR (20),
    BirthDate DATE,
    Salary DECIMAL (8, 2)
);

In this code snippet, we are creating a table called "Employees" with four columns: ID, Name, BirthDate, and Salary. Each column has a specific data type defined.

  • INT: Integer data type
  • VARCHAR(20): Variable character string with a maximum length of 20 characters
  • DATE: Date data type
  • DECIMAL(8, 2): Decimal data type with 8 digits in total and 2 digits after the decimal point

3.2 Example: Defining Constraints

CREATE TABLE Employees (
    ID INT PRIMARY KEY,
    Name VARCHAR (20) NOT NULL,
    BirthDate DATE,
    Salary DECIMAL (8, 2) CHECK (Salary > 0)
);

Here, we are defining constraints on the columns:

  • PRIMARY KEY: The ID column is the primary key and cannot have duplicate or null values
  • NOT NULL: The Name column cannot have null values
  • CHECK: The Salary column must have a value greater than 0

4. Summary

In this tutorial, we went over SQL data types and constraints. We learned how to define data types for table columns and how to enforce rules on these columns using constraints.

The next step in your learning could be to delve deeper into the different types of constraints available in SQL: UNIQUE, FOREIGN KEY, DEFAULT, among others.

5. Practice Exercises

5.1 Exercise 1

Create a table called "Products" with the following columns: ProductID (integer and primary key), ProductName (string with a maximum length of 30 characters and not null), and Price (decimal with a maximum of 5 digits in total and 2 after the decimal point, must be positive).

5.2 Exercise 2

Create a table called "Orders" with the following columns: OrderID (integer and primary key), ProductID (integer and foreign key referencing ProductID in the Products table), and OrderDate (date).

5.3 Solutions and Explanations

5.3.1 Solution to Exercise 1

CREATE TABLE Products (
    ProductID INT PRIMARY KEY,
    ProductName VARCHAR (30) NOT NULL,
    Price DECIMAL (5, 2) CHECK (Price > 0)
);

ProductID is the primary key, ProductName can't be null, and Price must be positive.

5.3.2 Solution to Exercise 2

CREATE TABLE Orders (
    OrderID INT PRIMARY KEY,
    ProductID INT,
    OrderDate DATE,
    FOREIGN KEY (ProductID) REFERENCES Products(ProductID)
);

OrderID is the primary key. ProductID is a foreign key referencing the ProductID in the Products table.

Keep practicing and exploring more about SQL data types and constraints to enhance your skills!