Академический Документы
Профессиональный Документы
Культура Документы
Data Types
BIGINT(x) integer +/- 9,223,372,036,854,775,807 Rename a TABLE
BLOB string up to 65,535 characters ALTER TABLE table1 RENAME TO table2
CHAR(x) string up to 255 characters
DATE date format: YY-MM-DD Add an Index
DATETIME date format: YY-MM-DD HH:MM:SS CREATE INDEX indexname ADD INDEX (col_1)
DECIMAL(x,d) double precision stored as string
DOUBLE (x,d) double precision floating number Optimize a Table
ENUM(‘Y’,’N’) string value is restricted to list This can speed up database performance; especially if there have
FLOAT floating point decimal been a lot of deletes and changes made to a database. You can list
INT(x) integer +/- 2,147,483,647 any number of tables in a database and they will be optimized. This
LONGBLOB string up to 4,294,967,295 characters re-indexes the tables.
LONGTEXT string up to 4,294,967,295 characters $sql = “OPTIMIZE TABLE table1, table2”;
MEDIUMBLOB string up to 16,777,215 characters mysql_query($sql) or die(mysql_error());
MEDIUMINT(x) integer +/- 8,388,608
MEDIUMTEXT string up to 16,777,215 characters Setting Up a Database Connection
SET string can hold any number of strings $dbName = “”; database name
SMALLINT(x) integer +/- 32,767 $dbHost = “localhost”; localhost or a hostname
TEXT string up to 65,535 characters $dbUser = “”; database user name
TIME time format: HH:MM:SS $dbPW = “”; database user password
TIMESTAMP time format: YY-MM-DD HH:MM:SS
TINYBLOB string up to 255 characters // as separate statements
TINYINT(x) integer +/- 127 $dbHandle = mysql_connect($dbHost,$dbUser,$dbPW)
TINYTEXT string up to 255 characters or die(“Could not connect to MySQL database”);
VARCHAR(x) string designated length up to 255 chars $dbSelect = mysql_select_db($dbName,$dbHandle)
YEAR date 4-digit year – 1901 to 2155 or die(mysql_error());
* Can declare as UNSIGNED, which starts range at 0. SMALLINT // combined into a single compound statement
range would therefore be 0 to 65,536 mysql_select_db($dbName,
x is an integer value representing the maximum display width or mysql_connect($dbHost,$dbUser,$dbPW))
number of digits displayed or die(mysql_error() );
d represents the number of decimal places
3 Ways to Insert a New Row
Data Column Attributes The basic INSERT statement:
AUTO_INCREMENT next numeric value auto assigned INSERT INTO products (item_id, retailer_sku, item_name,
DEFAULT sets default value for column item_description, status_id, timestamp,active)
NOT NULL null not allowed VALUES (‘A123’,’ZM8849’,’Fillrod’,’ ‘A rod used to check fluid volume
NULL null allowed of a tank’,1,’2008-08-31’,’Y’);
PRIMARY KEY assigns primary key When you have all the values you can skip column names:
UNSIGNED starts range at 0 INSERT INTO products VALUES (‘A123’, ‘ZM8849’,’Fillrod’,’A rod
used to check fluid volume of a tank’,1,’2008-08-31’,’Y’);
Create a New Table The following is the most flexible INSERT method. Can be used
Sample code shows how to use various data type declarations when when only a few (with NOT NULL or defaults set) or all values need
creating a table. to be inserted. Easier to troubleshoot because column names are
CREATE TABLE products ( matched to values.
item_id VARCHAR(50) NOT NULL, INSERT INTO products SET
retailer_sku VARCHAR(50) NOT NULL, item_id = ‘A123’,
item_name VARCHAR(20) NOT NULL, retailer_sku = ‘ZM8849’,
item_description TEXT NOT NULL, item_name = ‘Fillrod’,
status_id TINYINY(2) UNSIGNED DEFAULT '0', item_description = ‘A rod used to check fluid volume of a tank’,
datestamp DATE DEFAULT '0000-00-00', status_id = 1,
active ENUM('Y','N') DEFAULT 'Y', datestamp = ‘2008-08-31’,
PRIMARY KEY (item_id), active = ‘Y’;
KEY (retailer_sku),
FULLTEXT (item_name, item_description) Updating an Existing Row
) ENGINE=MyISAM DEFAULT CHARSET=latin1 ; Column name-value pairs can be used in any order. Can also be
used when only a few columns need to be updated. You must know
Add a Column a key or column value in order to update one of more records.
ALTER TABLE table1 ADD COLUMN col_5 MEDIUMTEXT UPDATE products SET
retailer_sku = ‘ZM8849’,
Change a Column Type item_name = ‘Fillrod’,
ALTER TABLE table1 CHANGE col_3 col_3 TEXT item_description = ‘A rod used to check fluid volume of a tank’,
status_id = 1,
Remove a Column datestamp = ‘2008-08-31’,
ALTER TABLE table1 DROP COLUMN col_4 active = ‘Y’,
WHERE item_id = ‘A123’;
Rename a Column
ALTER TABLE table1 CHANGE col_3 col_4 TEXT
page 1
page 2