Skip to main content

2. Table Basic


§ Tables are divided into columns and rows.
§ Defining datatype for each column

Creating Table

Syntax


CREATETABLEtable_name(
   column1 datatype,
   column2 datatype,
   column3 datatype,
.....
columnN datatype,
PRIMARYKEY ( one or more columns)
);

§ The unique name or identifier for the table follows the CREATE TABLE statement.
§ Then in brackets comes the list defining each column in the table and what sort of data type it is.

Example


CREATETABLE CUSTOMERS(
   ID   INTNOTNULL,
   NAME VARCHAR (20)NOTNULL,
   AGE INTNOTNULL,
ADDRESSCHAR (25),
   SALARY   DECIMAL (18, 2),
                PRIMARYKEY (ID)
);

Modifying Table

The ALTER TABLE statement is used to add, delete, or modify columns in an existing table.

Syntax

ALTERTABLEtable_name
ADDcolumn_name datatype

Syntax for modifying
ALTERTABLEtable_name
ALTERCOLUMNcolumn_name datatype

Syntax for deleting
ALTERTABLEtable_name
DROPCOLUMNcolumn_name






<<Prev                                                                                                  Next>>

Comments

Popular posts from this blog

INDEX

                                                                                   INDEX      No.     Article                                                                       1.     Introduction of SQL           2.     Table Basic           3.     Keys and Constraints           4.     Referential integrity           5.     INDEX           6.     VIEW         ...

14. Ordering Results

ORDER BY used to sort the result-set by one or more columns. It Sorts the records in ascending order by default. To sort the records in a descending order, you can use the DESC keyword. SELECT column_name , column_name FROM table_name ORDERBY column_name ASC | DESC , column_name ASC | DESC ; SELECT * FROM Customers ORDERBY Country DESC ;      <<Prev                                                                                      Next>>

12. INSERT UPDATE and DELETE Queries

INSERT The INSERT INTO statement is used to insert new records in a table INSERTINTO table_name VALUES ( value1 , value2 , value3 ,...); Or INSERTINTO table_name ( column1 , column2 , column3 ,...) VALUES ( value1 , value2 , value3 ,...); UPDATE Used to update existing records in a table. UPDATE table_name SET column1 = value1 , column2 = value2 ,... WHERE some_column = some_value ; DELETE Used to delete rows in a table. DELETEFROM table_name WHERE some_column = some_value ; <<Prev                                                                           Next>>