Skip to main content
Engineering LibreTexts

9.2: CREATE a Database

  • Page ID
    93688
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)

    \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)

    \( \newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\)

    ( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\)

    \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\)

    \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\)

    \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\)

    \( \newcommand{\Span}{\mathrm{span}}\)

    \( \newcommand{\id}{\mathrm{id}}\)

    \( \newcommand{\Span}{\mathrm{span}}\)

    \( \newcommand{\kernel}{\mathrm{null}\,}\)

    \( \newcommand{\range}{\mathrm{range}\,}\)

    \( \newcommand{\RealPart}{\mathrm{Re}}\)

    \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\)

    \( \newcommand{\Argument}{\mathrm{Arg}}\)

    \( \newcommand{\norm}[1]{\| #1 \|}\)

    \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\)

    \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    \( \newcommand{\vectorA}[1]{\vec{#1}}      % arrow\)

    \( \newcommand{\vectorAt}[1]{\vec{\text{#1}}}      % arrow\)

    \( \newcommand{\vectorB}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)

    \( \newcommand{\vectorC}[1]{\textbf{#1}} \)

    \( \newcommand{\vectorD}[1]{\overrightarrow{#1}} \)

    \( \newcommand{\vectorDt}[1]{\overrightarrow{\text{#1}}} \)

    \( \newcommand{\vectE}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash{\mathbf {#1}}}} \)

    \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)

    \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)

    How to Create a Database

    The major SQL DDL statements are CREATE DATABASE and CREATE/DROP/ALTER TABLE. The SQL statement CREATE is used to create the database and table structures.

    SQL Statement: CREATE DATABASE SW

    A new database named SW is created by the SQL statement CREATE  DATABASE SW. Once the database is created, the next step is to create the database tables.

    The general format for the CREATE TABLE command is:

    CREATE TABLE <tablename>
    (
    ColumnName, Datatype, Optional Column Constraint,
    ColumnName, Datatype, Optional Column Constraint,
    Optional table Constraints
    );

    Tablename is the name of the database table such as Employee. Each field in the CREATE TABLE has three parts (see above):

    1. ColumnName
    2. Data type
    3. Optional Column Constraint

    ColumnName

    The ColumnName must be unique within the table. Some examples of ColumnNames are FirstName and LastName.

    Data Type

    The data type, as described below, must be a system data type or a user-defined data type. Many of the data types have a size such as CHAR(35) or Numeric(8,2).

    Bit Integer data with either a 1 or 0 value

    Int –Integer (whole number) data from -2^31 (-2,147,483,648) through 2^31 – 1  (2,147,483,647)

    Smallint –Integer data from 2^15 (-32,768) through 2^15 – 1 (32,767)

    Tinyint Integer data from 0 through 255

    Decimal –Fixed precision and scale numeric data from -10^38 -1 through 10^38

    Numeric –A synonym for decimal

    Timestamp –A database-wide unique number

    Uniqueidentifier –A globally unique identifier (GUID)

    Money – Monetary data values from -2^63 (-922,337,203,685,477.5808) through 2^63 – 1 (+922,337,203,685,477.5807), with accuracy to one-ten-thousandth of a monetary unit

    Smallmoney –Monetary data values from -214,748.3648 through +214,748.3647, with accuracy to one-ten-thousandth of a monetary unit

    Float –Floating precision number data from -1.79E + 308 through 1.79E + 308

    Real –Floating precision number data from -3.40E + 38 through 3.40E + 38

    Datetime –Date and time data from January 1, 1753, to December 31, 9999, with an accuracy of one-three-hundredths of a second, or 3.33 milliseconds

    Smalldatetime –Date and time data from January 1, 1900, through June 6, 2079, with an accuracy of one minute

    Char –Fixed-length non-Unicode character data with a maximum length of 8,000 characters

    Varchar –Variable-length non-Unicode data with a maximum of 8,000 characters

    Text –Variable-length non-Unicode data with a maximum length of 2^31 – 1 (2,147,483,647) characters

    Binary –Fixed-length binary data with a maximum length of 8,000 bytes

    Varbinary –Variable-length binary data with a maximum length of 8,000 bytes

    Image  Variable-length binary data with a maximum length of 2^31 – 1 (2,147,483,647) bytes


    9.2: CREATE a Database is shared under a not declared license and was authored, remixed, and/or curated by LibreTexts.

    • Was this article helpful?