| Word | Example | Decription |
|---|---|---|
| default | create table example( name char(24) not null, areacode smallint(3) default 304 ); |
Sets a dafault value for the attribute |
| unique [key] | create table example( name char(32) not null default 'bob', code smallint(3) default 304, phone int(7) unique );or create table example( name char(32) not null default 'bob', code smallint(3) default 304, phone int(7), unique (code, phone) ); |
Set a candidate key. Remember you can have several candidate keys. |
| primary key | create table example( name char(32) not null default 'bob', code smallint(3) not null default 304, phone int(7) not null, primary key (code, phone) ); |
Sets the primary key of the table. Remember all attributes included in the primary key must be nul NULL. If you can't request this make this combination unique and choose a different primary key. |
| foreign key | create table fk_test ( name char(32) not null primary key, boss int(11), foreign key (boss) references def_test(id) ); |
MySQL Server does parse the FOREIGN KEY syntax in CREATE TABLE commands, but without further action being taken. The FOREIGN KEY syntax without ON DELETE ... is mostly used for documentation purposes. Some ODBC applications may use this to produce automatic WHERE clauses, but this is usually easy to override. FOREIGN KEY is sometimes used as a constraint check, but this check is unnecessary in practice if rows are inserted into the tables in the right order. In MySQL Server, you can work around the problem of ON DELETE ... not being implemented by adding the appropriate DELETE statement to an application when you delete records from a table that has a foreign key. In practice this is as quick (in some cases quicker) and much more portable than using foreign keys. |
insert into customers values (NULL, 'Julie Smith', '25 Oak St', 'Airport West');Example 2:
insert into customers (name, city) values ('Melissa Jones', 'Leeton');
LOAD DATA INFILE "pet.txt" INTO TABLE pet;This will read data from the file pet.txt into the table pet. By default, data fields in the file must be separated by tabs and enclosed in single quotes, and each row must be separated by a new line (\n).
INSERT INTO tblTemp2 (fldID) SELECT tblTemp1.fldOrder_ID FROM tblTemp1 WHERE tblTemp1.fldOrder_ID > 100;A couple of restrictions:
drop table pet;This command will remove the table from the database and delete all the rows.
| Syntax | Description |
|---|---|
| ADD [COLUMN] coulmn column_description [FIRST | AFTER coulmn] |
Add a new coulmn in the specified location (if not specified, the column goes at the end). |
| ADD [COULMN] (coulmn column_description [, coulmn column_description ...]) |
Add one or more columns at the end of the table. |
| ADD INDEX [index_name] (column [, column ...]) | Add an index to the table on the specified column or columns. |
| ADD PRIMARY KEY (column [, column ...]) | Make the specified column or columns the primary key. |
| ADD UNIQUE INDEX [index_name] (column [, column ...]) |
Add a unique index to the table on the specified column or columns. |
| ALTER [COLUMN] column {SET DEFAULT value | DROP DEFAULT} | Add or remove a default value for a particular column. |
| CHANGE [COLUMN] column new_column_name new_column_description | Change the column called column so it has the description listed. |
| MODIFY [COLUMN] column column_description | Similar to CHANGE. Can be used to change column types, not names. |
| DROP [COLUMN] column | Delete the name column. |
| DROP PRIMARY KEY | Delete the primary index (but not the column). |
| DROP INDEX index | Delete the named index. |
| RENAME [AS] new_table_name | Rename a table. |
Example:
create table alter_test ( id bigint(10), name char(10) ); alter table alter_test modify id int not null; alter table alter_test add primary key (id), modify id int not null auto_increment; alter table alter_test add column lastname varchar(32) binary after id; alter table alter_test alter column lastname set default 'Smith'; alter table alter_test add unique (lastname, name); alter table alter_test change name firstname char(24) not null;