Friday, 17 April 2015

create , alter mysql sql query

mysql sql query ( CREATE  , ALTER  ) create is used to create a new table , and alter is used to modify table


CREATE TABLE Participants
(
PartID SMALLINT NOT NULL AUTO_INCREMENT PRIMARY KEY,
PartFN VARCHAR(20) NOT NULL,
PartMN VARCHAR(20) NULL,
PartLN VARCHAR(20) NULL

The above query creates a table (Participants ) with columns ( PartID , PartFN , PartMN , PartLN  )


ALTER TABLE  is used to change 

ALTER TABLE Books
ADD PRIMARY KEY (BookID),
ADD CONSTRAINT fk_1 FOREIGN KEY (PubID) REFERENCES Publishers (PubID),
ADD COLUMN Format ENUM(‘paperback’, ‘hardcover’) NOT NULL AFTER BookName;



The final line of the ALTER TABLE statement adds a column to the table. 
As you can see, a column definition follows the ADD COLUMN keywords. 
The name of the column is Format.
The column is configured with an ENUM data type that is defined with two values: paperback and hardcover. The column is also configured with the NOT NULL option.
AFTER specifies that the new column should be added after the column named BookName.



ALTER TABLE Books
DROP PRIMARY KEY,
DROP FOREIGN KEY fk_1,
DROP COLUMN Format;

DROP means delete, DROP COLUMN Format; means  drop the column named Format from Books Table

ALTER TABLE InStock
ADD COLUMN Quantity SMALLINT UNSIGNED NOT NULL,
MODIFY ProductID SMALLINT UNSIGNED NOT NULL,
ADD PRIMARY KEY (ProductID);

No comments:

Post a Comment