Computer Science

SQL Data Types

SQL data types are used to define the type of data that can be stored in a database table. They include numeric, character, date/time, and Boolean data types, among others. Choosing the appropriate data type is important for efficient storage and retrieval of data.

Written by Perlego with AI-assistance

8 Key excerpts on "SQL Data Types"

  • Book cover image for: Learn SQL Database Programming
    eBook - ePub

    Learn SQL Database Programming

    Query and manipulate databases from popular relational database servers using SQL

    Understanding Data Types

    In this chapter, we will learn what data types and their usage . We will go through the specific data types and understand what data can be stored in each of them. The data types include string, numeric, date and time, and other data types. String data types include char and varchar, binary and varbinary, blob, enum, set, and text. Numeric data types include bit, int, float, double, and decimal. Date and time data types include date, time, datetime, timestamp, and year.
    We will learn from the perspective of MySQL Data Types, and will keep noting the differences between Oracle, PostgreSQL, and SQL Server wherever needed.
    We will also go through some examples of types and values of data to see how to assign them correctly to data types, including an explanation of why you need to be careful when selecting a data type and how it can impact database performance.
    In this chapter, we will cover the following topics:
    • Understanding string data types
    • Understanding numeric data types
    • Understanding date and time data types
    • Understanding other data types
    • Choosing the right data type
    Passage contains an image

    Understanding string data types

    String data types can hold plain text and binary data. This section walks you through the different string data types in MySQL. This section also shows you the differences between Oracle, PostgreSQL, and SQL Server string data types.
    Passage contains an image

    String data types in MySQL

    Let's discuss the string data types in MySQL:
    • CHAR: It is a fixed-length string, which can contain letters, numbers, and special characters. This type is blank-padded and contains trailing blanks in the field. This field can range from 0 to 255, and the default (if nothing is specified, that is, just using CHAR instead of CHAR(10)) is 1. The size in parenthesis denotes the maximum length of the char specified by the user when creating the data field with this type. The storage requirement for CHAR is size * n , where n <= size <= 255 , where n
  • Book cover image for: SQL Interview Questions
    eBook - ePub

    SQL Interview Questions

    A complete question bank to crack your ANN SQL interview with real-time examples

    HAPTER 2

    SQL Statements, Keywords, and Datatype

    W e know database is important not only to store data but also to secure it and manage it. SQL keeps data storage very clear and simple with its different datatypes. The datatypes are also very simple and user-friendly. It helps to arrange, store data separately, and speed up the data searching as well. According to data operations, SQL is further divided into different sublanguages like DDL, DML, DCL, and DTL. In this chapter we will see the different SQL datatypes, SQL statements, and SQL keywords.

    Prerequisite for this chapter

    Form this chapter onwards, we will also concentrate on SQL practical, and so you should have a laptop/desktop with SQL server installed on it. There is no specific knowledge required to learn this chapter, just start reading this with interest in database concepts.

    Structure

    After studying this chapter, you will learn the following points:
    • SQL keywords
    • SQL datatypes
      • Numerical SQL datatypes ◆ Exact numeric ◆ Approximate numeric
      • The string and char SQL datatypes ◆ Character string ◆ Unicode string ◆ Binary string
      • Date and time SQL datatypes
      • Miscellaneous SQL datatypes
    • Different types of SQL statements
    • DDL statements
    • DML statements
    • Conclusion
    • Rapid fire questions and answers
    • Do you know (lights on fact?)
    So, let's begin

    SQL keywords

    Every programming language has its own set of reserved words that are used by its compiler to perform various operations. Similarly, in SQL, we have a set of reserved words and it is recommended to not use those reserved words as table name, column, and identifiers. If you want to use them, then enclose them in square braces [] , these words are known as keywords. Basically, database uses this keyword for definition, manipulation, and accessing data.
    SQL has a very large list of keywords, but here I am picking up some important keywords for you:
  • Book cover image for: Database Development and Management
    We use capital letters to indicate keywords or table names for clarity. After executing the command, we will be able to add tables to the database. Defining Data Types For each attribute in a table, we must specify its data type when creating a table. The example below shows how to define commonly used data types in SQL Server. 1. Character data types: Character data consist of any combination of letters, symbols, and numeric characters. a. CHAR: fixed-length character data. Example: To define the column Club_Name of CHAR data type with length 30, use Club_Name CHAR(30) b. VARCHAR: variable-length character data. Example: To define the attribute Description of VARCHAR data type with maximum length of 300, use Description VARCHAR(300) A column with VARCHAR data type has a variable length. This means that there is no wasted storage space. On the other hand, a column with CHAR data type may have many blank spaces if the fixed-length column has many values that will not fill up the column width. However, with the fixed-length, a column of CHAR can be processed faster. 2. Number data types: Number data types include integers and decimals. a. INT: The integer data type consists of positive, zero, and negative whole numbers. The range of INT is from –2,147,483,648 to 2,147,483,647. Usually, INT is used for id numbers. Example: To define the integer type column SID, use 128 Database Development and Management SID INT b. DECIMAL (or NUMERIC): DECIMAL is used for the positive and negative decimal numbers. Example: To define the decimal type column Rate, use Rate DECIMAL 3. Date and time data type: This type is used for date and time combinations. a. DATETIME: It represents date and time from 1–1–1753 to 12–31–9999. Example: To define the date and time data type column Start_Time, use Start_Time DATETIME 4. Monetary data type: This type is used to show amounts of money.
  • Book cover image for: Introduction To Systems Development NQF2 SB
    • M Smit R Jonker(Author)
    • 2017(Publication Date)
    • Macmillan
      (Publisher)
    79 Module 10 Topic 3: Computer data storage Computer data types Module 10 Overview At the end of this module, you should be able to: • Unit 10.1: Distinguish between data types with examples and the way the data is stored in memory. • Unit 10.2: Distinguish between categories of coding systems and their uses in a business environment. • Unit 10.3: Explain and illustrate how data manipulation operations are performed on data types. Unit 10.1: Data types and the way data is stored in memory Data is classified into various types of information. For humans, it is easy to see the difference between data types, but in the case of computers you need to declare (specify) what data type it is. Computers use special internal codes to keep track of different types of data. 10.1.1 Data types A computer program consists of two elements: data and operations on that data. Computers can only represent very simple pieces of information. All complex information must be built up from basic data types. We will now discuss the most common data types. a) Characters and strings A character is a letter, number or symbol. The character data type contains only one character. A string is a linear (continuous) sequence of characters, words or other data that the user can input from the keyboard. This includes the letters of the alphabet, numbers, punctuation and other special characters. Example 10.1: Strings Each of the following is a string: “My name is Lebo.” “My name is Lebo and I am 20.” “My name is Lebo and I was born on 12 January 1970.” “(011) 812 4545” character: a letter, number or symbol string: a linear sequence of characters, words or other data that the user can input from the keyboard Words & Terms 80 Module 10 As you can see from Example 10.1, a string contains all sorts of characters that can be entered via the keyboard. The string is captured between double quotes. It is not possible to do calculations with numbers in a string.
  • Book cover image for: Programming language theory
    • Alvin Albuero De Luna(Author)
    • 2023(Publication Date)
    • Arcler Press
      (Publisher)
    DATA TYPES CHAPTER7 CONTENTS 7.1. Introduction .................................................................................... 188 7.2. Primitive Data Types ....................................................................... 191 7.3. Character String Types..................................................................... 195 References ............................................................................................. 199 Programming Language Theory 188 7.1. INTRODUCTION A data type specifies both a set of data elements as well as a set of activities that may be performed upon these values in a predetermined order. Data is manipulated by computer software to get the desired results. The degree whereby the data types that are accessible in the language that is used are similar to the things that exist in the actual world that are being addressed by the challenge is an essential component in deciding how easily they will be capable to do this job. As a result, a programming language must enable an adequate assortment of data types. For example, In the past 60 years, there has been an evolution in the ideas that constitute modern data types. All challenge space structures would have to be represented in the first languages using just a select handful of the most fundamental data structures that were supported by those languages. Linked sets and branching trees, for instance, were both implemented using arrays in versions of Fortrans before 1990 (Liskov and Zilles, 1974; Mitchell, 1996). In COBOL, the data models made the first step out from the Fortran I paradigm by enabling programers to define the precision of numeric data value, as well as by offering an organized data form for recordings of information (Scott, 1976; Herlihy and Liskov, 1982). This was the first move away first from Fortran I paradigm. The capacity of precision specification has been enhanced by PL/I to include both integer and flying types.
  • Book cover image for: Basic Data Structures and Program Statements
    • Xingni Zhou, Qiguang Miao, Lei Feng(Authors)
    • 2020(Publication Date)
    • De Gruyter
      (Publisher)
    3 Basic data types Main contents – Basic data types, the essence of types, storage mechanism of integers, and floating-point numbers – Definition of variables, referencing method, and their way of storage in memory – Operators and their usage, the concept of expressions, categorization of results of operations – Summary of data elements Learning objectives – Understand and master concept of data types, data storage, data referencing, and data operation – Know how to use common operators and expressions – Understand and master the usage of constants and variables 3.1 Constants and variables At the checkout in supermarkets, we are given a receipt by a cashier, on which in-formation of our purchases is written as shown in Figure 3.1. The column total is computed by multiplying per-unit price with quantity, where the per-unit price is a constant and quantity is a variable. Some values are fixed, whereas others keep changing in many problems. For example, we have speed, time, and distance in moving object problems. In circles, we have a radius, perimeter, and Pi. In the shopping example above, we have the number of purchased goods, per-unit price, and total. Data in programs can be categorized into two types based on how they are used: constants and variables. The value of a constant cannot be modified during the execu-tion of programs, whereas the value of a variable can be changed during execution. 3.1.1 Constants There are two kinds of constants: literals and symbolic constants. One can use lit-eral constants directly in programs as needed without having to define in advance. However, if a constant is used multiple times in a program, we can use a symbolic constant instead to allow easy modification. In this case, we only need to modify once if we need to change the value of the constant. Symbolic constants should be defined before being used.
  • Book cover image for: SQL: 1999
    eBook - PDF

    SQL: 1999

    Understanding Relational Language Components

    • Jim Melton, Alan R. Simon(Authors)
    • 2001(Publication Date)
    • Morgan Kaufmann
      (Publisher)
    The attributes can be given data types that are other structured types, array types, row types, and so forth. This is an arbitrarily com- plex structure, although implementations will certainly have some limits on complexity. In addition to stored data represented as attributes, structured types have behaviors provided by the type definer using methods, ordinary functions, and procedures (see Chapter 17, Routines and Routine Invocation: Functions and Procedures). It is primarily through the use of structured types and related facilities that applications access object technology in SQL. Distinct Types The less complex sort of user-defined type is the distinct type, which is also a per- sistent named type. A distinct type is defined as a single, primitive type instead of a structure of one or more attributes. For example, in our music and video store, we might choose to define a distinct type called PRICE that is based on DECIMAL(5,2). All values whose type is PRICE would have the same internal repre- sentation as values whose type is DECIMAL(5,2). So far, this probably doesn't seem very useful, does it? The value that distinct types add to SQL:1999 applications is the ability to enforce strong typing among application data. Specifically, an expression that would (say) add a value whose type is DECIMAL(5,2) to another value whose type is PRICE, which is based on DECIMAL(5,2), would be identified as a syntax error. In 46 Chapter 2 Introduction to SQL:1999 fact, you would not even be permitted to multiply a PRICE value by the value 0.065 to compute sales tax! Possibly even more surprising, you cannot simply add two PRICE values together: the arithmetic operator + is defined only for numeric types and not for PRICE types.
  • Book cover image for: C# Programming
    eBook - PDF

    C# Programming

    From Problem Analysis to Program Design

    To represent a single character, such as the first initial of your last name, a char data type is used. Your age, for example 21, is a whole number without a fractional part. The data type most often used to represent a number without a fractional part is an int . Table 2-7 gives some examples of different data types, with examples of what might be stored in each of these data items. Description grade Identifier Data type Data Grade point average gradePointAverage double 3.99 Current age age int 19 Full name studentName string Elizabeth Hill Final grade in a course courseGrade char A TABLE 2-7 Sample data types Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s). Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it. Predefined Data Types | 85 2 You learned in Chapter 1 that every program must define a class . There are thousands of classes that make up the .NET FCL. The following section describes some of the predefined data types that are considered primitive data types in most languages. These value and reference types will be explored: int , double , decimal , bool , char , and string . Predefined Data Types The .NET Framework includes a common type system (CTS) that is supported by all .NET languages, including C#. This enables cross-language integration of code meaning a single application can be written using multiple languages, such as C#, Visual C++, and Visual Basic. The types in the CTS are referred to as common types and are divided into two major categories, as seen in Figure 2-3: value types and reference types.
Index pages curate the most relevant extracts from our library of academic textbooks. They’ve been created using an in-house natural language model (NLM), each adding context and meaning to key research topics.