liveBook · Manning

Data type

As explained in Learn SQL in a Month of Lunches

A data type classifies values and determines the operations and rules applicable to them.

A data type identifies the kind of value a column, variable, or parameter holds. Common examples are character strings, integers, decimals, floats, dates, and times. The type matters because numbers, names, and dates follow different filtering rules, and compatible types are required when combining query results. section 13.1.1 section 5.1 section 10.1

When designing a table, choose a data type for every column alongside its name. For example, a CategoryID primary-key column can use int, allowing a unique numeric value for each category row. Clear column names and appropriate types make a database easier to query and understand. section 18.1.1

Matching a procedure parameter

A parameter that evaluates an OrderID should use int when OrderID is an integer column. Matching parameter, variable, and column types avoids extra work by the RDBMS and helps prevent errors; in MySQL, SHOW COLUMNS can reveal a column's type. section 20.5 section 20.2.3 section 21.2.1

MySQL user-defined variables infer their type from the assigned value: a quoted title becomes a string, while a numeric value becomes numeric. A variable beginning as NULL has no meaningful type until another value is assigned. Although MySQL permits a variable's type to change later, reusing one variable for different types is best avoided. section 13.1.1 section 13.4.2 section 13.4.3

DELIMITER //
 
CREATE PROCEDURE AddPromotion (
    IN _PromotionID int,
    IN _PromotionCode varchar(10),
    IN _PromotionStartDate datetime,
    IN _PromotionEndDate datetime
 )
 BEGIN
 
IF _PromotionCode IS NOT NULL THEN
    INSERT INTO promotion (
    PromotionID,
    PromotionCode,
    PromotionStartDate,
    PromotionEndDate
    )
    SELECT
        _PromotionID,
        _PromotionCode,
        _PromotionStartDate,
        _PromotionEndDate
        ;
END IF;
END //
 
DELIMITER ;

This procedure declares int, varchar(10), and datetime parameters to match the values written to the promotion table. section 21.2.1

Type declarations vary by database product. MySQL can infer the type of a user-defined variable assigned with SET, but variables declared inside stored procedures use DECLARE with an explicit type. SQL Server and PostgreSQL also require declared user-defined variables to specify a type. section 13.1.1 section 22.1.1

How Learn SQL in a Month of Lunches covers it

How deep it goes
Explains practical type selection and matching through tables, variables, and procedure parameters, without detailing type internals.
Examples
Uses CategoryID as int, changing a MySQL variable from 1 to a title string, and promotion procedure parameters.
What it assumes
Builds on familiarity with rows, columns, values, tables, and SQL queries while keeping type selection practical.
What you can do after
Apply it by choosing column types and matching variable or procedure parameter types to the columns they evaluate.
How it connects
Connects data types to tables, columns, variables, NULL, set operators, stored procedures, and primary keys.

Also explained in

Compare how 18 books explain data type →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Learn SQL in a Month of Lunches. The section links point to the parts of the book it draws on.