PetCatalog | MemberDirectory |
---|---|
Pet | Member |
PetType | Login |
PetColor |
With the CREATE TABLE query you should include a list of the column names with their definitions.
The information for each column is separated by a coma.
A coma is not needed for the different definitions syntax, just to separate the column names.
Look at the examples below.
Each column name is followed by its data type. and additional information.
The most commonly used is NOT NULL.
Here is a list of definitions that can be used:
Definitions | Descriptions |
---|---|
NOT NULL | column must have an entry, it cannot be empty. |
DEFAULT value | this value is stored with the column when the row is created if no other value is entered by the user. |
AUTO_INCREMENT | this creates a sequence number as each row is added. it increases by one integer from the last row entered. You can override this value by assigning a specific value to the column . |
UNSIGNED | use this definition if the numeric field will never be a negative number. |
075 The last line in the CREATE TABLE query is PRIMARY KEY(columnname) which indicates the key to the table.
If you are using two or more column names to define the Primary Key use the following format:
PRIMARY KEY(columnname1, columnname2)
075 There are two sets of parentheses used, one for the column name of the primary key and one that surrounds all the column names.
If you are using the localhost/mysql_send.php to create the table,
make sure you include the Databasename of petCatagory or MemberDirectory.
The five queries to create the tables are as follows:
For the PetCatalog database:
CREATE TABLE `Pet` ( `petID` SERIAL NOT NULL, `petName` CHAR(25), `petType` CHAR(15) NOT NULL, `petDescription` VARCHAR(255), `price` DECIMAL(9,2), `pix` CHAR(20), PRIMARY KEY (`petID`) )
CREATE TABLE `PetType` ( `petType` CHAR(15) NOT NULL, `typeDescription` VARCHAR(255), PRIMARY KEY (`petType`) )
CREATE TABLE `PetColor` ( `petName` CHAR(25) NOT NULL, `petColor` CHAR(15) NOT NULL, `pix` CHAR(20), PRIMARY KEY (petName,petColor) )
CREATE TABLE `Member` ( `loginName` VARCHAR(20) NOT NULL, `password` CHAR(255) NOT NULL, `createDate` DATE NOT NULL, `lastName` VARCHAR(50), `firstName` VARCHAR(40), `street` VARCHAR(50), `city` VARCHAR(50), `state` CHAR(2), `zip` CHAR(10), `email` VARCHAR(50), `phone` CHAR(15), `fax` CHAR(15), PRIMARY KEY (`loginName`) )
CREATE TABLE `Login` ( `loginName` VARCHAR(20) NOT NULL, `loginTime` DATETIME, PRIMARY KEY (loginName,loginTime) )
076 To remove any table, use a query in this format:
DROP TABLE tablename
If you are using the localhost/mysql_send.php to drop a table,
make sure you include the Databasename of petCatagory or MemberDirectory.
Change | Description |
---|---|
ADD columnname definition | Adds a column; definition includes the data type and optional definitions. |
ALTER columnname SET DEFAULT value | Changes the default value of the column. |
ALTER columnname DROP DEFAULT | Removes the default value for a column. |
CHANGE columnname newcolumnname definition | Changes the definition of a column and renames the column; definition includes the data type and optional definitions. |
DROP columnname | Deletes a column, including all the data in the column. The data cannot be recovered. |
MODIFY columnname definition | Changes the definition of a column, definition includes the data type and optional definitions. |
RENAME newtablename | Renames a table |
Requests | Description |
---|---|
Adding information | Adding a row to a table. |
Updating information | Changing information in an existing row, including adding data to a blank field in an existing row. |
Retrieving information | Looking for data. |
Removing information | Deleting data from a database. |
078 ADDING INFORMATION
Adding one row at a time - use the INSERT query
adding lots of rows in one shot - use the LOAD query
079 Adding one row at a time
The INSERT query tells MySQL which table to place the data in and what the values are for the columns or fields.
The format for the INSERT query follows:
INSERT INTO tablename (columnname,
columnname, ... columnname)
VALUES (values, value, ... value)
example:
INSERT INTO petColor (petName, petColor, pix)
VALUES ("Unicorn", "white", "/pix/unicorwhite.jpg")
If you are using the localhost/mysql_send.php to add data to a table,
make sure you enter one query at a time, not the entire list as displayed below.
make sure you include the Databasename of petCatagory.
INSERT INTO pet (petName, petType, petDescription, price, pix) VALUES ("Unicorn", "Horse", "Spiral horn centered in forehead", "10000", "/pix/unicorn.jpg") ; INSERT INTO pet (petName, petType, petDescription, price, pix) VALUES ("Pegasus", "Horse", "Flying; wings sprouting from back", "15000", "/pix/pegasus.jpg") ; INSERT INTO pet (petName, petType, petDescription, price, pix) VALUES ("Pony", "Horse", "Very small; half the size of standard horse", "500", "/pix/pony.jpg") ; INSERT INTO pet (petName, petType, petDescription, price, pix) VALUES ("Asian Dragon", "Dragon", "Serpentine body", "30000", "/pix/asiandragon.jpg") ; INSERT INTO pet (petName, petType, petDescription, price, pix) VALUES ("Medieval Dragon", "Dragon", "Lizard-like body", "30000", "/pix/medievdrago.jpg") ; INSERT INTO pet (petName, petType, petDescription, price, pix) VALUES ("Lion", "Cat", "Large; maned", "2000", "/pix/lion.jpg") ; INSERT INTO pet (petName, petType, petDescription, price, pix) VALUES ("Gryphon", "Cat", "Lion body; eagle head; wings", "25000", "/pix/gryphodrago.jpg") ;
INSERT INTO petType (petType, typeDescription) VALUES ("Cat", "Beautiful and dignified. Independent. Range in size from large lions to small cats. Carnivorous.") ; INSERT INTO petType (petType, typeDescription) VALUES ("Dog", "Strong, Courageous. Extremely intelligent. Can be trained for very useful work, such as watch dog or seeing-eye dog. Or bringing in the newspaper. Includes wild species, such as coyotes and wolves.") ; INSERT INTO petType (petType, typeDescription) VALUES ("Dragon", "Magnificent, large reptiles. Dragons fly tirelessly and many are large enough to ride. Very goog watch animals.") ; INSERT INTO petType (petType, typeDescription) VALUES ("Fish", "Many colorful varieties. Relaxing and mesmerizing to watch. Size varies from great white sharkes to guppies.") ; INSERT INTO petType (petType, typeDescription) VALUES ("Horse", "Beautiful four legged animals that you can ride. Includes magical varities such as Unicorn and Pegasus.") ; INSERT INTO petType (petType, typeDescription) VALUES ("Lizard", "Small reptiles. Fascinating to watch. Requires warm environment. Eat vegatables and bugs.") ;
INSERT INTO petColor (petName, petColor, pix) VALUES ("Unicorn", "white", "/pix/unicorwhite.jpg") ; INSERT INTO petColor (petName, petColor, pix) VALUES ("Unicorn", "silver", "/pix/unicorsilve.jpg") ; INSERT INTO petColor (petName, petColor, pix) VALUES ("Fish", "gold", "/pix/fishgold.jpg") ;
080 ADDING A BUNCH OF DATA
Large amounts of data can be added to a table by using the LOAD query.
The default deliminator between fields is the tab character, however you can change that if you like.
The end of line character is the default for the end of a row.
An example of a tab deliminated file for the pet table follows:
Unicorn<TAB>horse<TAB>Spiral horn centered in forehead<TAB>5000.00<TAB>/pix/unicorn.jpg Pegasus<TAB>horse<TAB>Winged<TAB>8000.00<TAB>/pix/pegasus.jpg Lion<TAB>cat<TAB>Large; mane on neck<TAB>2000.00<TAB>/pix/lion.jpgThe basic form of the LOAD query follows
Information: Adding | Updating | Retrieving | Removing | PHP and MySQL