If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > ANSI SQL > table cannot have more than four row under car_id (was "hi")

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 11-29-06, 12:03
tanbir_17 tanbir_17 is offline
Registered User
 
Join Date: Nov 2004
Posts: 7
table cannot have more than four row under car_id (was "hi")

hi,

i am creating a table called T2 with the following fields: car_id, make, model, sale_date. And would like to know how i can set a rule so that the table cannot have more than four row under car_id.

cheers for the helps guyz
Reply With Quote
  #2 (permalink)  
Old 11-29-06, 12:14
healdem healdem is offline
Jaded Developer
 
Join Date: Nov 2004
Location: out on a limb
Posts: 9,246
I suppose it depends entirely in which SQL database you are using... Oracle, DB2, MySQL Access/JET, dBase... even SQL server
__________________
I'd rather be riding my Versys or my Tiger 800 let alone the Norton
Reply With Quote
  #3 (permalink)  
Old 11-29-06, 12:16
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
Code:
create table T2
( car_id integer
, make integer
, model integer
, sale_date date
, check ( 4 <= ( select count(car_id) from T2 ) )
)
be advised that not all database sytems support this syntax

__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #4 (permalink)  
Old 11-29-06, 18:46
tanbir_17 tanbir_17 is offline
Registered User
 
Join Date: Nov 2004
Posts: 7
hi,

ive tried that method it did not work. i am using database using sql databse language using ssh to create this database if this help any.

sorry im new to this.

any help would be grateful

cheers
Reply With Quote
  #5 (permalink)  
Old 11-29-06, 18:50
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
you posted in the SQL forum, which is the generic forum for the SQL language, and i'm pretty sure what i wrote is valid SQL

as i said, not every database system supports this type of CHECK constraint

which database are you using? and what does "did not work" mean? did you get an error message? if so, what was it?
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #6 (permalink)  
Old 11-29-06, 19:02
tanbir_17 tanbir_17 is offline
Registered User
 
Join Date: Nov 2004
Posts: 7
this is the code i taped into my database and recived the following error.

dataworkbase=> CREATE TABLE t6 (
dataworkbase(> car_id int,
dataworkbase(> make varchar(10),
dataworkbase(> model varchar(10),
dataworkbase(> sale_date date,
dataworkbase(> check ( 4 <= (select count(car_id) from t6) )
dataworkbase(> );
ERROR: cannot use subselect in CHECK constraint expression


i have playes around with the code but still experincing problems

would really be gratefull for any assistance.
cheers
Reply With Quote
  #7 (permalink)  
Old 11-30-06, 09:23
urquel urquel is offline
Registered User
 
Join Date: Aug 2004
Posts: 330
Maybe if you can explain WHY you want this constraint, someone could suggest a design that will accomodate what you want.
Reply With Quote
  #8 (permalink)  
Old 11-30-06, 09:37
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
which database system are you using? sybase? progress? firebird? db2? postgresql?
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #9 (permalink)  
Old 12-03-06, 08:24
Peter.Vanroose Peter.Vanroose is offline
Registered User
 
Join Date: Sep 2004
Location: Belgium
Posts: 1,079
Quote:
Originally Posted by r937
Code:
create table T2
( car_id integer
, make integer
, model integer
, sale_date date
, check ( 4 <= ( select count(car_id) from T2 ) )
)
I'm not sure whether this is a valid check constraint (according to SQL 2) or not.
Conceptually, a constraint should limit its scope to a single row; e.g., when inserting two rows in a table, the order of inserting should not influence which one is accepted by the check constraints or RI constraints. Which is not the case here.

Conceptual (or syntactic) limitations of check / RI constraints can typically be overcome by using triggers.

If the RDBMS supports "before" triggers, it can indeed be implemented by a constraint like
Code:
(select count(*) from T2 where car_id=New.car_id)+(select count(*) from New)<=4
(Don't forget to add the number of rows being currently inserted to the ones already there!)

If only "after" triggers are supported, a "shadow" table will have to be created with a single row per car_id, and a counter (with a check constraint on it) that gets incremented for every INSERT on table T2.
__________________
--_Peter Vanroose,
__IBM Certified Database Administrator, DB2 9 for z/OS
__IBM Certified Application Developer
__ABIS Training and Consulting
__http://www.abis.be/
Reply With Quote
  #10 (permalink)  
Old 12-03-06, 08:27
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
Quote:
Originally Posted by Peter.Vanroose
I'm not sure whether this is a valid check constraint (according to SQL 2) or not.
i am
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #11 (permalink)  
Old 12-03-06, 08:29
Peter.Vanroose Peter.Vanroose is offline
Registered User
 
Join Date: Sep 2004
Location: Belgium
Posts: 1,079
Quote:
Originally Posted by r937
i am
OK, I trust on your knowledge in this matter !

B.t.w., which RDBMS implementations support this particular check constraint?
__________________
--_Peter Vanroose,
__IBM Certified Database Administrator, DB2 9 for z/OS
__IBM Certified Application Developer
__ABIS Training and Consulting
__http://www.abis.be/
Reply With Quote
  #12 (permalink)  
Old 12-03-06, 08:31
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
Quote:
Originally Posted by Peter.Vanroose
B.t.w., which RDBMS implementations support this particular check constraint?
exactly!!! see post #8
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #13 (permalink)  
Old 12-03-06, 08:40
Peter.Vanroose Peter.Vanroose is offline
Registered User
 
Join Date: Sep 2004
Location: Belgium
Posts: 1,079
I'm sure DB2 does not support this.
(While it supports most of the SQL 2 stuff.)
__________________
--_Peter Vanroose,
__IBM Certified Database Administrator, DB2 9 for z/OS
__IBM Certified Application Developer
__ABIS Training and Consulting
__http://www.abis.be/
Reply With Quote
  #14 (permalink)  
Old 12-03-06, 09:13
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
i'm sure sql server and mysql don't, too

__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #15 (permalink)  
Old 12-03-06, 10:00
Peter.Vanroose Peter.Vanroose is offline
Registered User
 
Join Date: Sep 2004
Location: Belgium
Posts: 1,079
Just found the following in the SQL-2 standard description (http://www.contrib.andrew.cmu.edu/~s...ql/sql1992.txt) :
Quote:
The <search condition> shall not generally contain a <query specification> or a <query expression> that is possibly non-deterministic.
My interpretation is that this excludes check constraints using COUNT(*) on the current table, since in that case the result depends on the order of a multi-row insert, which is a "non-deterministic" situation.
__________________
--_Peter Vanroose,
__IBM Certified Database Administrator, DB2 9 for z/OS
__IBM Certified Application Developer
__ABIS Training and Consulting
__http://www.abis.be/
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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
Trackbacks are On
Pingbacks are On
Refbacks are On