Free Newsletters:
DatabaseJournal  
DBANews
Database Journal
Search Database Journal:
 
HOME News MS SQL Oracle DB2 Access MySQL PostgreSQL PHP SQL Etc Scripts Links Discussion
internet.com

» HOME
» NEWS
» FEATURES
» SERIES
MS SQL
Oracle
MS Access
MySQL
DB2
» RESOURCES
Products
Scripts
Links
» DISCUSSION
» TECH JOBS

Marketplace Partners
Be a Marketplace Partner




internet.commerce
Be a Commerce Partner
Imprinted Promotions
Rackmount LCD Monitor
GPS
Auto Insurance Quote
Memory Upgrades
SMS Gateway
Televisions
Imprinted Gifts
Calling Cards
Find Software
KVM Switches
Promotional Pens
Logo Design
Laptop Batteries




MySpace Joins eBay, Yahoo in Open Profile Push

News Corp. Unit Under Fire for Ties to Hacker

Are Non-PC Devices Hurting 'Net Innovation?

internet.com
IT
Developer
Internet News
Small Business
Personal Technology
International

Search internet.com
Advertise
Corporate Info
Newsletters
Tech Jobs
E-mail Offers


Linked Data Planet Conference & Expo

CA ERwin® Data Modeler Proven database design and modeling. Efficiently analyze, design and deploy effective database solutions. Whitepaper: Manage SQL Server Deployments
Try it free: CA ERwin® Data Modeler


Solaris 8 Migration Assistant
Rapidly move your Solaris 8 application environments to new systems running Solaris 10 with the Solaris 8 Migration Assistant. Reduce migration risk while taking advantage of increased performance, reliability and security of the latest SPARC hardware platforms and Solaris 10 OS. »

 
Sun Eco Innovation: Good for Business, Good for the Environment
A complete solution to help you optimize and refresh your datacenter while properly recycling equipment and eliminating eWaste, including money-saving promotions to lower hardware acquisition costs. »

 
Sun Eco Innovation: Power Calculators
Power consumption has increasingly become a priority in customer's minds when purchasing new systems or storage. Sun's Power Calculators provide data on power consumption of Sun products allowing IT managers to better plan the power requirements in the datacenter to achieve better energy and cost savings. »

 
Optimize the Web Tier: Consolidate to Get More Performance in Less Space and Lower Power Consumption
Expansion in the Web tier is generally accomplished by adding more servers whenever extra capacity is needed. As the pool of servers grows larger, however, the complexity of the environment can grow exponentially. »

Production Manager (hands on)
Aquent
US-MA-Cambridge

Justtechjobs.com Post A Job | Post A Resume
MySQL
July 12, 2005
MySQL Oddities
By Ian Gilfillan

Introduction

For a number of years Ian Barwick has maintained an excellent page called MySQL Gotchas. These are MySQL features, which do not work as expected according to either the SQL standard, or the way other common relational databases would do things. I have been sent the link to this page countless times, usually by somebody trying to persuade me I have made a horrible mistake by using MySQL, or that I shouldn't be suggesting MySQL in certain instances.

The list itself is not anti-MySQL, although it has often been used in that context, not only by people sending me the link, but more frequently in the sort of ill-informed forum comment that plagues cyberspace along the lines of:
MySQL sucks - rather use a real database like X. See MySQL Gotchas.
This article introduces some of the oddities to new readers, and examines some of the points raised in the section on general SQL.

NULLS

NULL values in a relational database have always been contentious. Much has been written about it (you can start with Fabian Pascal's critique at DBAzine), but in brief, many argue that permitting NULL's in a database was a mistake, and that the SQL standard is flawed in this respect. Nonetheless NULL's are here to stay, and are supposed to represent the absence of a value. Let's look at what MySQL does differently in this regard. Try this example:



mysql> CREATE TABLE t1 (
 i1 INT NOT NULL AUTO_INCREMENT PRIMARY KEY
 i2 INT NOT NULL, 
 f1 VARCHAR(10) NOT NULL,
 f2 VARCHAR(10) NOT NULL DEFAULT 'nada', 
 ts TIMESTAMP NOT NULL
);

mysql> INSERT INTO t1 (i1) VALUES(1);

mysql> INSERT INTO t1 (i1,f1) VALUES(NULL,'something');

What would you expect to happen with these examples? In the first example, only the field i1 is supplied. Since the others are defined as NOT NULL, and only f2 has a defined default, you may expect the query to fail, since there are no supplied values for i2, f1 and ts.

Similarly, in the second example, we are attempting to pass NULL into i1, defined as NOT NULL and with no default, as well as not supplying values for i2, f2 and ts. However, both queries run successfully. Here is what is in the table.

mysql> SELECT * FROM t1;
+----+----+-----------+------+---------------------+
| i1 | i2 | f1        | f2   | ts                  |
+----+----+-----------+------+---------------------+
|  1 |  0 |           | nada | 2005-07-06 21:58:15 |
|  2 |  0 | something | nada | 2005-07-06 21:58:40 |
+----+----+-----------+------+---------------------+

The results will be surprising if you are more familiar with another DBMS. MySQL creates default values in certain instances if none are supplied. The default values here are '' (empty string) for the VARCHAR field, 0 for INT field i2 and the current date and time for the TIMESTAMP field ts. The INT field i1 is even more surprising. Although it was specifically assigned the value NULL in the second query, it still contains a value, 2. This is how the MySQL AUTO_INCREMENT sequence works. If the fields were defined as accepting NULLs, MySQL would have inserted a NULL. Instead, since they were defined as NOT NULL, MySQL uses alternative defaults.

There is an even more alarming case, to my mind, where the actual results of query are not consistent, even though ostensibly no data has changed. If you have not run any other queries since the two INSERT statements above, run these two queries below, one after another. Even though they are identical, the result changes.

mysql> SELECT * FROM t1 WHERE i1 IS NULL;
+----+----+-----------+------+---------------------+
| i1 | i2 | f1        | f2   | ts                  |
+----+----+-----------+------+---------------------+
|  2 |  0 | something | nada | 2005-07-06 21:58:40 |
+----+----+-----------+------+---------------------+
1 row in set (0.00 sec)

mysql> SELECT * FROM t1 WHERE i1 IS NULL;
Empty set (0.00 sec) 

There is certainly no NULL value in i1, but MySQL justifies this exception as being of benefit to Access and Delphi (for determining the last AUTO_INCREMENT value) and possibly other ODBC applications. The full list of defaults MySQL uses vary according to the field type, as follows:

  • For numeric fields except for those defined with AUTO_INCREMENT, the default is '0'.
  • For numeric fields defined with AUTO_INCREMENT column, the default is the next value in the sequence.
  • For string types except for ENUM, the default is an empty string.
  • For the ENUM type, the default is the first enumeration value.
  • For DATETIMEs, the default is '0000-00-00 00:00:00'
  • For DATEs, the default is '0000-00-00'
  • For TIMESTAMPs, the default is the current date and time.
  • For TIMEs, the default is '00:00:00'
  • For YEARs, the default is '0000'

Note that as of MySQL 5.0.2, this default behavior can be changed, and MySQL made to behave in the same way as standard SQL by setting one of the strict modes. I plan an article on this topic in future, but for now you can read more in the MySQL manual section about the Server SQL mode. You can also stop MySQL from accepting zero dates with the NO_ZERO_DATE SQL mode. Zero dates are not standard, and are automatically converted to NULL when using newer versions of MyODBC.

Go to page: 1  2  Next  

Tools:
Add databasejournal.com to your favorites
Add databasejournal.com to your browser search box
IE 7 | Firefox 2.0 | Firefox 1.5.x
Receive news via our XML/RSS feed

MySQL Archives

HP eBook: Using Business Service Management (BSM) to Manage Your Business Applications
Data Sheet: IBM Information Server Blade
Whitepaper: HP Integrated Citrix XenServer for HP ProLiant Servers. Sponsored by HP, Citrix, and Intel.
Quest Whitepaper: Improving Oracle Database Performance Using Real-Time Visual Diagnostics
What's The Future Of IT? Find Out By Reading "IT in 2018" Now. Free Registration Required.


Latest Forum Threads
MySQL Forum
Topic By Replies Updated
Transfer information from an access database to MySQl database sculptor44 2 May 14th, 09:50 PM
MERGE STORAGE ENGINE Vs UNION ALL dbnewbie 0 May 14th, 12:16 AM
database sculptor44 0 May 5th, 09:10 PM
arrangeable Database sculptor44 0 May 5th, 08:53 PM







JupiterOnlineMedia

internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

Jupitermedia Corporate Info


Legal Notices, Licensing, Reprints, & Permissions, Privacy Policy.

Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers

Solutions
Whitepapers and eBooks
Microsoft Article: Will Hyper-V Make VMware This Decade's Netscape?
Microsoft Article: 7.0, Microsoft's Lucky Version?
Microsoft Article: Hyper-V--The Killer Feature in Windows Server 2008
Avaya Article: How to Feed Data into the Avaya Event Processor
Microsoft Article: Install What You Need with Windows Server 2008
HP eBook: Putting the Green into IT
Whitepaper: HP Integrated Citrix XenServer for HP ProLiant Servers
Intel Go Parallel Portal: Interview with C++ Guru Herb Sutter, Part 1
Intel Go Parallel Portal: Interview with C++ Guru Herb Sutter, Part 2--The Future of Concurrency
Avaya Article: Setting Up a SIP A/S Development Environment
IBM Article: How Cool Is Your Data Center?
Microsoft Article: Managing Virtual Machines with Microsoft System Center
HP eBook: Storage Networking , Part 1
Microsoft Article: Solving Data Center Complexity with Microsoft System Center Configuration Manager 2007
MORE WHITEPAPERS, EBOOKS, AND ARTICLES
Webcasts
Intel Video: Are Multi-core Processors Here to Stay?
On-Demand Webcast: Five Virtualization Trends to Watch
HP Video: Page Cost Calculator
Intel Video: APIs for Parallel Programming
HP Webcast: Storage Is Changing Fast - Be Ready or Be Left Behind
Microsoft Silverlight Video: Creating Fading Controls with Expression Design and Expression Blend 2
MORE WEBCASTS, PODCASTS, AND VIDEOS
Downloads and eKits
Sun Download: Solaris 8 Migration Assistant
Sybase Download: SQL Anywhere Developer Edition
Red Gate Download: SQL Backup Pro and free DBA Best Practices eBook
Red Gate Download: SQL Compare Pro 6
Iron Speed Designer Application Generator
MORE DOWNLOADS, EKITS, AND FREE TRIALS
Tutorials and Demos
How-to-Article: Preparing for Hyper-Threading Technology and Dual Core Technology
eTouch PDF: Conquering the Tyranny of E-Mail and Word Processors
IBM Article: Collaborating in the High-Performance Workplace
HP Demo: StorageWorks EVA4400
Intel Featured Algorhythm: Intel Threading Building Blocks--The Pipeline Class
Microsoft How-to Article: Get Going with Silverlight and Windows Live
MORE TUTORIALS, DEMOS AND STEP-BY-STEP GUIDES