create table test ( id int not null auto_increment primary key, name varchar(50), age tinyint ) engine = myisam; insert into test (name) values ('jim'),('john'),('paul'),('mike'); -- The age field is now NULL. select * from test; update test set age = case when name = 'jim' then 10 when name = 'paul' then 20 else 30 end; -- The age field now has data. select * from test;
Show what IS NOT NULL:
SELECT *
FROM table
WHERE YourColumn IS NOT NULL;
This seems to show what IS NULL:
SELECT *
FROM table
WHERE YourColumn <=> NULL;
This seems to show what IS empty:
SELECT *
FROM table
WHERE YourColumn <=> ”;
SELECT SUBSTRING(<BLOB COLUMN_NAME>,1,2500) FROM <Table_name>;
How would I display BLOB data with PHP? I’ve entered the BLOB into the DB, but how would I retrieve it? Any examples would be great.
sorry, by BLOB, i meant like a picture
Simple example:
$blob_data = “something you’ve got from BLOB field”;
header(‘Content-type: image/jpeg’); // e.g. if it’s JPEG image
echo $blob_data;
Escaping an apostrophe or a quote in MySQL can be done by putting a \ (backslash) in front of them.
Example: insert into tablename (columnname) values ('It\'s time to eat.');
Escaping an apostrophe or quote in MSSQL can be done by doubling up the apostrophe or quote.
Example: insert into tablename (columnname) values ('It''s time to eat.');
Source: from
If your system has its own zoneinfo database (the set of files describing time zones),
you should use the mysql_tzinfo_to_sql program for filling the time zone tables.
Examples of such systems are Linux, FreeBSD, Solaris, and Mac OS X. One likely
location for these files is the /usr/share/zoneinfo directory. If your system does
not have a zoneinfo database, you can use the downloadable package described later
in this section.
The mysql_tzinfo_to_sql program is used to load the time zone tables. On the command
line, pass the zoneinfo directory path name to mysql_tzinfo_to_sql and send the output
into the mysql program. For example:
shell> mysql_tzinfo_to_sql /usr/share/zoneinfo | mysql -u root mysql -p
mysql_tzinfo_to_sql reads your system's time zone files and generates SQL statements
from them. mysql processes those statements to load the time zone tables.
mysql_tzinfo_to_sql also can be used to load a single time zone file or to generate
leap second information:
To load a single time zone file tz_file that corresponds to a time zone name tz_name,
invoke mysql_tzinfo_to_sql like this:
shell> mysql_tzinfo_to_sql tz_file tz_name | mysql -u root mysql -p
With this approach, you must execute a separate command to load the time zone file
for each named zone that the server needs to know about.
If your time zone needs to account for leap seconds, initialize the leap second
information like this, where tz_file is the name of your time zone file:
shell> mysql_tzinfo_to_sql --leap tz_file | mysql -u root mysql -p
After running mysql_tzinfo_to_sql, it is best to restart the server so that it does
not continue to use any previously cached time zone data.
If your system is one that has no zoneinfo database (for example, Windows or HP-UX),
you can use the package of pre-built time zone tables that is available for download
at the MySQL Developer Zone:
http://dev.mysql.com/downloads/timezones.html
This time zone package contains .frm, .MYD, and .MYI files for the MyISAM time zone
tables. These tables should be part of the mysql database, so you should place the
files in the mysql subdirectory of your MySQL server’s data directory. The server
should be stopped while you do this and restarted afterward.
The example below uses a column named “utc_dttm” that contains UTC timestamp data.
Without the timezone files added to the MySQL database server you will have to get
the time using the time like this:
select id, tasklist, convert_tz(utc_dttm, '+00:00','+03:00') as Baghdad_Time from tasktracker;
With the timezone files added to the MySQL database server you can use the name of
the timezone like this:
select id, tasklist, convert_tz(utc_dttm, 'utc','us/arizona') as Arizona_Time from tasktracker;
select id , date, entry , convert_tz(date, '+03:00','+00:00') as Time_UTC from journal1 limit 0, 400;
The name of the timezone is especially helpful for places like Arizona and parts of
the world that do not observe daylight savings time.
Source
mysql -h mysql.example.com -u root -p
Description:
If I try to alter a table to add a foreign key on an INNODB table, I get an error.
ERROR 1005 (HY000): Can’t create table ‘./mndb/#sql-992_406.frm’ (errno: 150)
How to repeat:
create table foo(id integer) ;
create table bar(id integer) ;
alter table foo add foreign key(id) references bar(id) ;
returns:
ERROR 1005 (HY000): Can’t create table ‘./mndb/#sql-992_406.frm’ (errno: 150)
The INNODB engine has an issue with foreign key constraints. Use the MYISAM engine.
create table foo(id integer) engine=myisam;
Query OK, 0 rows affected (0.02 sec)
create table bar(id integer) engine=myisam;
Query OK, 0 rows affected (0.01 sec)
alter table foo add foreign key(id) references bar(id);
Query OK, 0 rows affected (0.00 sec)
Records: 0 Duplicates: 0 Warnings: 0
From: http://bugs.mysql.com/bug.php?id=15324
My Example:
drop table if exists preselect_questions;
create table preselect_questions (
preselect_questions_id bigint(20) unsigned not null primary key auto_increment,
preselect_questions varchar(20) not null,
preselect_questions_desc varchar(50) not null,
person_id int not null,
prsnl_id int not null,
encntr_id int not null) engine=myisam;
drop table if exists preselect_questions_response;
create table preselect_questions_response (
preselect_questions_response_id bigint(20) unsigned not null primary key auto_increment,
preselect_questions_id bigint(20) not null,
person_id int not null,
prsnl_id int not null,
encntr_id int not null) engine=myisam;
alter table preselect_questions_response
add constraint fk_preselect_questions_to_preselect_questions_response
foreign key (preselect_questions_id)
references preselect_questions(preselect_questions_id)
on update cascade;
Prepending data in a column can be done with any alphanumeric characters. This example uses a period.
mysql> select * from namelist;
+—-+———-+————+
| id | first | last |
+—-+———-+————+
| 61 | Dutch | Shultz |
| 62 | Winnie | Pooh |
| 63 | Spider | Man |
| 64 | Super | Man |
| 65 | Green | Lantern |
| 66 | Yellow | Lantern |
| 67 | Bunky | Carmichael |
| 68 | Mo | Zilla |
| 69 | God | Zilla |
| 70 | File | Zilla |
| 71 | Wonder | Woman |
| 72 | Johnny | Jack |
| 73 | Tubby | Twoton |
| 74 | Darrel | McFarrel |
| 75 | Nancy | McFancy |
| 76 | Donovan | Williams |
| 77 | Willie | Mays |
| 78 | Abraham | Lincoln |
| 79 | Barry | Bonds |
| 80 | Mohammad | Ali |
+—-+———-+————+
20 rows in set (0.00 sec)
Prepend the data in the column with a period by using the update command.
mysql> update namelist set last = concat(“.”, last);
The last column now looks like this.
mysql> select * from namelist;
+—-+———-+————-+
| id | first | last |
+—-+———-+————-+
| 61 | Dutch | .Shultz |
| 62 | Winnie | .Pooh |
| 63 | Spider | .Man |
| 64 | Super | .Man |
| 65 | Green | .Lantern |
| 66 | Yellow | .Lantern |
| 67 | Bunky | .Carmichael |
| 68 | Mo | .Zilla |
| 69 | God | .Zilla |
| 70 | File | .Zilla |
| 71 | Wonder | .Woman |
| 72 | Johnny | .Jack |
| 73 | Tubby | .Twoton |
| 74 | Darrel | .McFarrel |
| 75 | Nancy | .McFancy |
| 76 | Donovan | .Williams |
| 77 | Willie | .Mays |
| 78 | Abraham | .Lincoln |
| 79 | Barry | .Bonds |
| 80 | Mohammad | .Ali |
+—-+———-+————-+
20 rows in set (0.00 sec)
3.6.9. Using AUTO_INCREMENT The AUTO_INCREMENT attribute can be used to generate a unique identity for new rows: CREATE TABLE animals ( id MEDIUMINT NOT NULL AUTO_INCREMENT, name CHAR(30) NOT NULL, PRIMARY KEY (id) ) ENGINE=MyISAM; INSERT INTO animals (name) VALUES ('dog'),('cat'),('penguin'), ('lax'),('whale'),('ostrich'); SELECT * FROM animals; Which returns: +----+---------+ | id | name | +----+---------+ | 1 | dog | | 2 | cat | | 3 | penguin | | 4 | lax | | 5 | whale | | 6 | ostrich | +----+---------+ No value was specified for the AUTO_INCREMENT column, so MySQL assigned sequence numbers automatically. You can also explicitly assign NULL or 0 to the column to generate sequence numbers. You can retrieve the most recent AUTO_INCREMENT value with the LAST_INSERT_ID() SQL function or the mysql_insert_id() C API function. These functions are connection-specific, so their return values are not affected by another connection which is also performing inserts. Use a large enough integer data type for the AUTO_INCREMENT column to hold the maximum sequence value you will need. When the column reaches the upper limit of the data type, the next attempt to generate a sequence number fails. For example, if you use TINYINT, the maximum permissible sequence number is 127. For TINYINT UNSIGNED, the maximum is 255. Note For a multiple-row insert, LAST_INSERT_ID() and mysql_insert_id() actually return the AUTO_INCREMENT key from the first of the inserted rows. This enables multiple-row inserts to be reproduced correctly on other servers in a replication setup. For MyISAM and BDB tables you can specify AUTO_INCREMENT on a secondary column in a multiple-column index. In this case, the generated value for the AUTO_INCREMENT column is calculated as MAX(auto_increment_column) + 1 WHERE prefix=given-prefix. This is useful when you want to put data into ordered groups. CREATE TABLE animals ( grp ENUM('fish','mammal','bird') NOT NULL, id MEDIUMINT NOT NULL AUTO_INCREMENT, name CHAR(30) NOT NULL, PRIMARY KEY (grp,id) ) ENGINE=MyISAM; INSERT INTO animals (grp,name) VALUES ('mammal','dog'),('mammal','cat'), ('bird','penguin'),('fish','lax'),('mammal','whale'), ('bird','ostrich'); SELECT * FROM animals ORDER BY grp,id; Which returns: +--------+----+---------+ | grp | id | name | +--------+----+---------+ | fish | 1 | lax | | mammal | 1 | dog | | mammal | 2 | cat | | mammal | 3 | whale | | bird | 1 | penguin | | bird | 2 | ostrich | +--------+----+---------+ In this case (when the AUTO_INCREMENT column is part of a multiple-column index), AUTO_INCREMENT values are reused if you delete the row with the biggest AUTO_INCREMENT value in any group. This happens even for MyISAM tables, for which AUTO_INCREMENT values normally are not reused. If the AUTO_INCREMENT column is part of multiple indexes, MySQL will generate sequence values using the index that begins with the AUTO_INCREMENT column, if there is one. For example, if the animals table contained indexes PRIMARY KEY (grp, id) and INDEX (id), MySQL would ignore the PRIMARY KEY for generating sequence values. As a result, the table would contain a single sequence, not a sequence per grp value. To start with an AUTO_INCREMENT value other than 1, you can set that value with CREATE TABLE or ALTER TABLE, like this: mysql> ALTER TABLE tbl AUTO_INCREMENT = 100; More information about AUTO_INCREMENT is available here: How to assign the AUTO_INCREMENT attribute to a column: Section 12.1.10, “CREATE TABLE Syntax”, and Section 12.1.4, “ALTER TABLE Syntax”. How AUTO_INCREMENT behaves depending on the NO_AUTO_VALUE_ON_ZERO SQL mode: Section 5.1.6, “Server SQL Modes”. How to use the LAST_INSERT_ID() function to find the row that contains the most recent AUTO_INCREMENT value: Section 11.13, “Information Functions”. Setting the AUTO_INCREMENT value to be used: Section 5.1.3, “Server System Variables”. AUTO_INCREMENT and replication: Section 15.4.1.1, “Replication and AUTO_INCREMENT”. Server-system variables related to AUTO_INCREMENT (auto_increment_increment and auto_increment_offset) that can be used for replication: Section 5.1.3, “Server System Variables”.
Snippet from the MySQL Docs Online.