w3reference home
SQL Tutorial


Bookmark and Share

Creating Tables

All tables within a database must be created at some point in time...let's see the format of a simple create table statement:

CREATE TABLE table_name
(column_name1 data_type(constraint),
column_name2 data_type(constraint),
..);
Let's have a quick look at the above query.

  • "CREATE TABLE" is the keyword to create a new table.
  • "table_name" and "column_name1" are the name of the table and column respectively. These names must start with a letter and can be followed by letters, numbers or underscores and should not exceed a total of 30 characters in length. Do not use any SQL reserved keywords as names for table or column names.
  • The data type specifies what type of data that particular column can store. Most common data types are shown below:

    char(x)A column of fixed-length character string, where x is a number designating the number of characters allowed. Max 255 bytes.
    varchar(x)Variable-length character string, where x denotes the maximum size.
    integerA column of whole numbers, positive or negative.
    dateA date column in a DBMS-specific format.
    logicalA column that can hold only two values: TRUE or FALSE.

  • The constraint is optional. A constraint is basically a rule associated with a column that the data entered into that column must follow. E.g. a most popular constraint is "not null" which specifies that a column can't be left blank.

    Example:

    CREATE TABLE Emp_Add
    (ssn INTEGER NOT NULL,
    fname VARCHAR (200),
    lname VARCHAR (200),
    address VARCHAR (500),
    city VARCHAR (100),
    state VARCHAR (100));
    This statement creates a table named "Emp_Add", containing six columns named ssn, fname, lname, address, city and state. It also tells the DBMS about the type of data that can be stored in each column of the table. The table when viewed at this point will look as follows:
    ssn fname lname address city state
               
  • Code Validator
    Learn FTP
    Color finder
    Link Checker
    Free web designs
    Coming soon!
    Interview Questions...
    'w3reference : Learn by examples ... Advanced to beginner's tutorials ...'
    Ajax: AJAX tutorial1 | Apache: Apache HTTP Server | Restarting Apache | CSS: CSS Border | CSS Syntax | CSS Selector | CSS Comment | CVS: CVS Release | CVS Login | CVS Logout | CVS Annotate | Databases: Rolap Tutorial | OLAP Tutorial | OLTP Tutorial | data warehousing | Expect: HTML: html | Linux: Dot (.) conf files | Linux Mount Point | Linux Filesystem | SSH Tutorial | Linux Commands: cal | cat | cfdisk | chroot | MySQL: MySQL Commands | PHP: PHP Basics | PHP Variables | PHP Output (echo/print) | PHP String Concat | PL/SQL: PL/SQL Data Types | PL/SQL Control Structures | PL/SQL File Extensions | PL/SQL DBMS_OUTPUT package | Python: My first Python program | Shell: Starting Bash | Bash Redirection | Bash Pipes | Bash Variables | SQL: SQL Transactions | SQL Constraints | SQL Drop | SQL Union & Union All | SVN: svn architecture | SVN Repository | SVN Import | SVN Checkout | Tech: soap | Web Designing: Web Hosting | HTML/XHTML/CSS code validator | Learn FTP | Search Engine Optimization Tips | www: XML: XML vs HTML | XML Syntax | XML Tags, Elements and Attributes | XML Namespaces |
    Sitemap | Disclaim | Privacy Policy | Contact | ©2007-2009 w3reference.com All Rights Reserved.