LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-22-2008, 12:21 PM   #1
Murdock1979
Member
 
Registered: Oct 2003
Distribution: Slackware Debian VectorLinux
Posts: 429
Blog Entries: 2

Rep: Reputation: 30
SQL catchall value


Hello!

Is there an SQL catchall value? In other words, if the result of the query is null, then put a value into the fields.

Thanks!
Murdock
 
Old 07-22-2008, 12:44 PM   #2
tronayne
Senior Member
 
Registered: Oct 2003
Location: Northeastern Michigan, where Carhartt is a Designer Label
Distribution: Slackware 32- & 64-bit Stable
Posts: 3,541

Rep: Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065
Actually, NULL is a value and you can test for it; e.g., ... set somecolumn = 0 where blah is null ... Specifically is there a catchall value, well, no unless you want to consider NULL as such (and it pretty much is).
 
Old 07-22-2008, 02:36 PM   #3
Murdock1979
Member
 
Registered: Oct 2003
Distribution: Slackware Debian VectorLinux
Posts: 429

Original Poster
Blog Entries: 2

Rep: Reputation: 30
Hello,

Thanks for the pointer.

So technically, the way to change a Null value into something else would be to code:

mysql> set @a = select man from people where people.man = "charlie";
mysql> set @a = "Bob" where @a is Null;

Thanks,
Murdock
 
Old 07-22-2008, 09:52 PM   #4
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
You would seldom want a default in a "select" (you probably don't want to retrieve a value other than then one that's actually there!) ...

... but most RDBMS's (including mySQL) support a "default" clause any any column for inserts:

http://dev.mysql.com/doc/refman/5.0/...-defaults.html
 
Old 07-23-2008, 07:29 AM   #5
tronayne
Senior Member
 
Registered: Oct 2003
Location: Northeastern Michigan, where Carhartt is a Designer Label
Distribution: Slackware 32- & 64-bit Stable
Posts: 3,541

Rep: Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065
You may want to consider altering your tables so that numeric columns default to zero (integer 0, float 0.0) and important character columns (for example, fname and lname) are not null; i.e., a value is required, and non-critical character columns default to NULL. The idea is to not add a bunch of stuff that just takes up space but at the same time let the engine force you to make entries in critical columns. Defaulting a numeric column to zero will only take up storage space the size of the given data type (an integer and a float typically take up four bytes, a double typically eight bytes). This starts to matter when you get a few 10's of million rows in your tables. That a character is null doesn't really matter because you can test for that; i.e., the result of a query is either "something" or "nothing" and you take appropriate action depending upon that.
 
Old 07-23-2008, 07:40 AM   #6
Murdock1979
Member
 
Registered: Oct 2003
Distribution: Slackware Debian VectorLinux
Posts: 429

Original Poster
Blog Entries: 2

Rep: Reputation: 30
Hello,

Thanks for the info.

How do take action after a select query?

Also, what kind of syntax do you use to add a default statement to a select statement?

Thanks,
Murdock

Last edited by Murdock1979; 07-23-2008 at 07:42 AM.
 
Old 07-23-2008, 07:57 AM   #7
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,369

Rep: Reputation: 2753Reputation: 2753Reputation: 2753Reputation: 2753Reputation: 2753Reputation: 2753Reputation: 2753Reputation: 2753Reputation: 2753Reputation: 2753Reputation: 2753
If you want to convert table.column values to something else if eg they are null, see this page: http://dev.mysql.com/doc/refman/5.0/...functions.html

Last edited by chrism01; 07-23-2008 at 09:21 AM.
 
Old 07-23-2008, 08:03 AM   #8
tronayne
Senior Member
 
Registered: Oct 2003
Location: Northeastern Michigan, where Carhartt is a Designer Label
Distribution: Slackware 32- & 64-bit Stable
Posts: 3,541

Rep: Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065
You don't add a default in a select statement; you do it when you initially design the table or you use alter table after the fact; see http://dev.mysql.com/doc/refman/5.0/en/alter-table.html for the syntax.

SQL is kind of bassackwards to the way you normally think -- you tend to think in terms of "if condition do this else do that," and SQL isn't quite the same. So, you don't take action after a query in SQL, you take action as part of the query.

Something like this, say

update tabname
set col_a = "this string value"
where col_a is null
and col_b = 123
and col_c = 0;

So, the content of col_a will only be changed to "this string value" if col_a is null and the other two values are as stated. Not quite the same thing as "if - else."

Hope this helps some.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
sendmail catchall virtusertable CC problem jborko Linux - Server 1 03-16-2007 03:08 PM
CatchAll Email System delwinbest Linux - Software 1 01-19-2007 03:31 AM
Set catchall email deleted option in qmail lsimon4180 Linux - Software 9 10-21-2004 09:14 PM
email catchall simonhobson Linux - Newbie 1 03-10-2004 07:52 AM
How do you setup a Catchall... rootlinux Linux - General 1 05-31-2002 02:17 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:31 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration