Description of SQL Tables

Description of SQL Tables thumbnail
SQL tables model and store data values.

SQL tables model sets of data for computing applications and websites. Using SQL statements, developers can define the structure and content of database tables. SQL database tables include columns with values of specific data types, all defined in SQL. SQL can also define the constraints within a particular column, or the relationships between different columns and tables. SQL statements can also connect to database tables to query them for data.

  1. Name

    • A database table defined in SQL must have a name. In most cases, developers try to choose names that are meaningful, making the resulting database system easier to work with. The following example SQL excerpt declares a new database table using its name:

      CREATE TABLE Order (

      /* Columns here */

      }

      This table could form part of a customer service system in which order data is stored in the database. SQL queries also refer to table names as follows:

      SELECT * FROM Order

      This query selects all of the data in the "Order" table.

    Column Names

    • Database tables contain columns, so SQL statements creating tables define these columns using their names. Meaningful names are also preferred for columns, as this makes future development more straightforward. For example, the "Order" table could contain columns representing the order date and total amount. In most cases, database tables also include a column that defines each record in the table uniquely, known as the Primary Key. Primary Key columns often have names with "ID" in them, such as "orderID." SQL queries can select particular columns by name as follows:

      SELECT orderID FROM Order

    Data Types

    • SQL statements defining columns use the column name and data type. The data type determines the type and range of value that may be stored within the records for a particular column. The following sample code demonstrates creating the "Order" table using a number of columns with names and data types indicated:

      CREATE TABLE Order (

      orderID int,

      total int,

      customerID int

      )

      This simplified sample defines three integer columns within the table: one for the order ID, one for the total amount and one representing the customer placing the order.

    Values

    • SQL statements can define certain constraints and default values for columns. The following sample excerpt extends the line defining the ID column to ensure each record within it has a value and auto-increment it:

      orderID int NOT NULL AUTO INCREMENT,

      This line means that any time a new record is added to the table, the column must have a value. The auto-increment command instructs SQL to assign a repeatedly incrementing number to each new record entered. To define the column as Primary Key, the statement creating the table can include the following command:

      PRIMARY KEY (orderID)

Related Searches:

References

Resources

  • Photo Credit Thinkstock/Comstock/Getty Images

Comments

Related Ads

Featured