Skip to content

Computer Science · Ch 9 — Structured Query Language (SQL)

Data Types and Constraints in MySQL

9.3

Data Types and Constraints in MySQL

A database is made up of tables, and each table is made up of columns (also called attributes). Every column stores a specific kind of information — for example, a student's name is text, their date of birth is a date, and their marks are numbers. This kind of information is called the data type of the column.

Beyond just the type of data, we often need to place restrictions on what values are allowed in a column. For instance, a student's roll number should be unique, or a fee amount should never be negative. These restrictions are called constraints.

In MySQL, when you create a table, you must decide two things for every column:

  1. What data type it will hold.
  2. What constraints (if any) apply to it. …