Difference between revisions of "Alter & Drop"

From TRCCompSci - AQA Computer Science
Jump to: navigation, search
(Created page with "==Adding a Field== To add a column in a table, use the following syntax: <syntaxhighlight lang=sql> ALTER TABLE table_name ADD column_name datatype </syntaxhighlight> ==Dele...")
 
 
(4 intermediate revisions by the same user not shown)
Line 1: Line 1:
 +
You will not be asked any questions to do with alter or drop in your exam, however it could be useful for projects.
 
==Adding a Field==
 
==Adding a Field==
 
To add a column in a table, use the following syntax:
 
To add a column in a table, use the following syntax:
Line 5: Line 6:
 
ALTER TABLE table_name
 
ALTER TABLE table_name
 
ADD column_name datatype
 
ADD column_name datatype
 +
</syntaxhighlight>
 +
 +
Example:
 +
 +
<syntaxhighlight lang=sql>
 +
ALTER TABLE Persons
 +
ADD DateOfBirth date
 
</syntaxhighlight>
 
</syntaxhighlight>
  
 
==Deleting a Field==
 
==Deleting a Field==
  
Next, we want to delete the column named "DateOfBirth" in the "Persons" table.
+
To delete the column named "DateOfBirth" in the "Persons" table you could use the following SQL statement:
 
 
We use the following SQL statement:
 
  
 
<syntaxhighlight lang=sql>
 
<syntaxhighlight lang=sql>
Line 20: Line 26:
 
==Change Data Type==
 
==Change Data Type==
  
Now we want to change the data type of the column named "DateOfBirth" in the "Persons" table.
+
To change the data type of the column named "DateOfBirth" in the "Persons" table you could use the following SQL statement:
  
We use the following SQL statement:
 
 
<syntaxhighlight lang=sql>
 
<syntaxhighlight lang=sql>
 
ALTER TABLE Persons
 
ALTER TABLE Persons
 
ALTER COLUMN DateOfBirth year
 
ALTER COLUMN DateOfBirth year
 
</syntaxhighlight>
 
</syntaxhighlight>

Latest revision as of 14:26, 24 December 2016

You will not be asked any questions to do with alter or drop in your exam, however it could be useful for projects.

Adding a Field

To add a column in a table, use the following syntax:

ALTER TABLE table_name
ADD column_name datatype

Example:

ALTER TABLE Persons
ADD DateOfBirth date

Deleting a Field

To delete the column named "DateOfBirth" in the "Persons" table you could use the following SQL statement:

ALTER TABLE Persons
DROP COLUMN DateOfBirth

Change Data Type

To change the data type of the column named "DateOfBirth" in the "Persons" table you could use the following SQL statement:

ALTER TABLE Persons
ALTER COLUMN DateOfBirth year