CREATE TABLE default data question

This is a discussion on CREATE TABLE default data question within the Database Discussions forums in Database and Unix Discussions category; When you create a table, what's the syntax for setting default values. For example (pseudo): CREATE TABLE mytable ( id INT PRIMARY KEY AUTO_INCREMENT, myoption DEFAULT ['option1', 'option2'] )...

Go Back   Database Forum > Database and Unix Discussions > Database Discussions

Database Forums

Register FAQ Calendar Search Today's Posts Mark Forums Read
  #1  
Old 12-30-2006, 08:51 PM
Default CREATE TABLE default data question

When you create a table, what's the syntax for setting default
values.

For example (pseudo):
CREATE TABLE mytable (
id INT PRIMARY KEY AUTO_INCREMENT,
myoption DEFAULT ['option1', 'option2']
)
Reply With Quote
  #2  
Old 12-31-2006, 12:39 AM
Default Re: CREATE TABLE default data question

Sanders Kaufman wrote:
> When you create a table, what's the syntax for setting default values.
>
> For example (pseudo):
> CREATE TABLE mytable (
> id INT PRIMARY KEY AUTO_INCREMENT,
> myoption DEFAULT ['option1', 'option2']
> )


I'm not sure what you're trying to do. What is the type of "myoption"?
And a column can have only one default value.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex-at-attglobal.net
==================
Reply With Quote
  #3  
Old 12-31-2006, 12:39 AM
Default Re: CREATE TABLE default data question

Sanders Kaufman wrote:
> When you create a table, what's the syntax for setting default values.
>
> For example (pseudo):
> CREATE TABLE mytable (
> id INT PRIMARY KEY AUTO_INCREMENT,
> myoption DEFAULT ['option1', 'option2']
> )


I'm not sure what you're trying to do. What is the type of "myoption"?
And a column can have only one default value.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex-at-attglobal.net
==================
Reply With Quote
  #4  
Old 12-31-2006, 03:41 PM
Default Re: CREATE TABLE default data question

Jerry Stuckle wrote:
> Sanders Kaufman wrote:
>
>> When you create a table, what's the syntax for setting default values.
>>
>> For example (pseudo):
>> CREATE TABLE mytable (
>> id INT PRIMARY KEY AUTO_INCREMENT,
>> myoption DEFAULT ['option1', 'option2']
>> )

>
>
> I'm not sure what you're trying to do. What is the type of "myoption"?
> And a column can have only one default value.
>



A DEFAULT value can only be *one* value (not a list of values). If you want to
make sure only certain values are in a field, then you want an "enumerated"
field or a constraint. See the docs for each method.

--
Michael Austin.
Database Consultant
Reply With Quote
  #5  
Old 12-31-2006, 03:41 PM
Default Re: CREATE TABLE default data question

Jerry Stuckle wrote:
> Sanders Kaufman wrote:
>
>> When you create a table, what's the syntax for setting default values.
>>
>> For example (pseudo):
>> CREATE TABLE mytable (
>> id INT PRIMARY KEY AUTO_INCREMENT,
>> myoption DEFAULT ['option1', 'option2']
>> )

>
>
> I'm not sure what you're trying to do. What is the type of "myoption"?
> And a column can have only one default value.
>



A DEFAULT value can only be *one* value (not a list of values). If you want to
make sure only certain values are in a field, then you want an "enumerated"
field or a constraint. See the docs for each method.

--
Michael Austin.
Database Consultant
Reply With Quote
  #6  
Old 01-01-2007, 03:57 PM
Default Re: CREATE TABLE default data question

Jerry Stuckle wrote:
> Sanders Kaufman wrote:
>> When you create a table, what's the syntax for setting default values.
>>
>> For example (pseudo):
>> CREATE TABLE mytable (
>> id INT PRIMARY KEY AUTO_INCREMENT,
>> myoption DEFAULT ['option1', 'option2']
>> )

>
> I'm not sure what you're trying to do. What is the type of "myoption"?
> And a column can have only one default value.


The type doesn't matter.
I just want something (logically) like "DEFAULT IN (a, b, c)".

I'm not sure, but I think some folks call in an ENUM?
Reply With Quote
  #7  
Old 01-01-2007, 03:57 PM
Default Re: CREATE TABLE default data question

Jerry Stuckle wrote:
> Sanders Kaufman wrote:
>> When you create a table, what's the syntax for setting default values.
>>
>> For example (pseudo):
>> CREATE TABLE mytable (
>> id INT PRIMARY KEY AUTO_INCREMENT,
>> myoption DEFAULT ['option1', 'option2']
>> )

>
> I'm not sure what you're trying to do. What is the type of "myoption"?
> And a column can have only one default value.


The type doesn't matter.
I just want something (logically) like "DEFAULT IN (a, b, c)".

I'm not sure, but I think some folks call in an ENUM?
Reply With Quote
  #8  
Old 01-01-2007, 03:59 PM
Default Re: CREATE TABLE default data question

Michael Austin wrote:
> Jerry Stuckle wrote:
>> Sanders Kaufman wrote:


>>> When you create a table, what's the syntax for setting default values.
>>> For example (pseudo):
>>> CREATE TABLE mytable (
>>> id INT PRIMARY KEY AUTO_INCREMENT,
>>> myoption DEFAULT ['option1', 'option2']
>>> )


>> I'm not sure what you're trying to do. What is the type of
>> "myoption"? And a column can have only one default value.


> A DEFAULT value can only be *one* value (not a list of values). If you
> want to make sure only certain values are in a field, then you want an
> "enumerated" field or a constraint. See the docs for each method.


Thanks Michael.
I looked up "enumerated" and "constraint" but then drowned in
the results.
Can you narrow it down? I think, without any sense of knowing,
that it has something to do with creating an ENUM.
Reply With Quote
  #9  
Old 01-01-2007, 03:59 PM
Default Re: CREATE TABLE default data question

Michael Austin wrote:
> Jerry Stuckle wrote:
>> Sanders Kaufman wrote:


>>> When you create a table, what's the syntax for setting default values.
>>> For example (pseudo):
>>> CREATE TABLE mytable (
>>> id INT PRIMARY KEY AUTO_INCREMENT,
>>> myoption DEFAULT ['option1', 'option2']
>>> )


>> I'm not sure what you're trying to do. What is the type of
>> "myoption"? And a column can have only one default value.


> A DEFAULT value can only be *one* value (not a list of values). If you
> want to make sure only certain values are in a field, then you want an
> "enumerated" field or a constraint. See the docs for each method.


Thanks Michael.
I looked up "enumerated" and "constraint" but then drowned in
the results.
Can you narrow it down? I think, without any sense of knowing,
that it has something to do with creating an ENUM.
Reply With Quote
  #10  
Old 01-04-2007, 08:54 PM
Default Re: CREATE TABLE default data question

Sanders Kaufman wrote:

> Jerry Stuckle wrote:
>
>> Sanders Kaufman wrote:
>>
>>> When you create a table, what's the syntax for setting default values.
>>>
>>> For example (pseudo):
>>> CREATE TABLE mytable (
>>> id INT PRIMARY KEY AUTO_INCREMENT,
>>> myoption DEFAULT ['option1', 'option2']
>>> )

>>
>>
>> I'm not sure what you're trying to do. What is the type of
>> "myoption"? And a column can have only one default value.

>
>
> The type doesn't matter.
> I just want something (logically) like "DEFAULT IN (a, b, c)".
>
> I'm not sure, but I think some folks call in an ENUM?



Now we need to narrow down your question just a bit - the answer depends on how
you answer the following:

1) do you want to restrict the values entered into this table?
example:
create table somename ( col1 varchar(20),
col2 enum('0','1','2'));

http://dev.mysql.com/doc/refman/5.0/en/enum.html

2) if no value is specified, do you want it to DEFAULT to some value?
see example below
if yes, what single value do you want as a default?

example:
mysql> create table somename ( col1 varchar(20),
col2 enum('0','1','2') default '0');
Query OK, 0 rows affected (2.19 sec)

***NOTE*** I do not specify col2 and the default value is '0'
mysql> insert into somename (col1) values ('test'),('test1');
Query OK, 2 rows affected (0.14 sec)
Records: 2 Duplicates: 0 Warnings: 0

mysql> select * from somename;
+-------+------+
| col1 | col2 |
+-------+------+
| test | 0 |
| test1 | 0 |
+-------+------+


--
Michael Austin.
Database Consultant
Reply With Quote
Reply


Thread Tools
Display Modes



All times are GMT -4. The time now is 07:01 AM.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Integrated by bbpixel2008 :: jvbPlugin R1013.368.1

Search Engine Friendly URLs by vBSEO 3.1.0
vB Ad Management by =RedTyger=
In an effort to better serve ads to our visitors, cookies are used on Mydatabasesupport.com. For more information, check out our Privacy Policy.