473,549 Members | 2,715 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

database constraints

Ben
If I have have the table:

create table foo
(
a int references bar(id),
b int references baz(id)
)

.... how do I make sure one and only one of the columns a and b are
non-null? Is it even reasonable?
---------------------------(end of broadcast)---------------------------
TIP 4: Don't 'kill -9' the postmaster

Nov 23 '05 #1
12 1553
On Wed, Oct 06, 2004 at 08:23:11AM -0700, Ben wrote:
If I have have the table:

create table foo
(
a int references bar(id),
b int references baz(id)
)

... how do I make sure one and only one of the columns a and b are
non-null?
You could write it like this:

CREATE TABLE foo
(
a INT REFERENCES bar(id),
b INT REFERENCES baz(id),
CHECK((a IS NULL AND b IS NOT NULL) OR (a IS NOT NULL AND b IS NULL))
);
Is it even reasonable?


What's "reasonable ?" ;)

BTW, "id" is a terrible name for a column. Better call it foo_id.

Cheers,
D
--
David Fetter da***@fetter.or g http://fetter.org/
phone: +1 510 893 6100 mobile: +1 415 235 3778

Remember to vote!

---------------------------(end of broadcast)---------------------------
TIP 3: if posting/reading through Usenet, please send an appropriate
subscribe-nomail command to ma*******@postg resql.org so that your
message can get through to the mailing list cleanly

Nov 23 '05 #2
* Ben <be***@silentme dia.com> [2004-10-06 08:23:11 -0700]:
If I have have the table:

create table foo
(
a int references bar(id),
b int references baz(id)
)

... how do I make sure one and only one of the columns a and b are
non-null? Is it even reasonable?


ALTER TABLE foo ADD CONSTRAINT must_have_a_or_ b CHECK (a::int IS NULL
AND b::int IS NOT NULL OR a::int IS NOT NULL AND b::int IS NULL);

--
Steven Klassen - Lead Programmer
Command Prompt, Inc. - http://www.commandprompt.com/
PostgreSQL Replication & Support Services, (503) 667-4564

---------------------------(end of broadcast)---------------------------
TIP 4: Don't 'kill -9' the postmaster

Nov 23 '05 #3


What's "reasonable ?" ;)

BTW, "id" is a terrible name for a column. Better call it foo_id.
Hello,

I disagree with the idea that "id" is a terrible name for a column. The
only negative to it, is that you will have to be explicit in your
declarations when doing joins and such... ex:

SELECT * FROM foo
JOIN bar on (foo.id = bar.id)

Personally I would rather see, and write that then:

SELECT * FROM foo
JOIN bar on (foo_id = bar_id)
Sincerely,

Joshua D. Drake

Cheers,
D

--
Command Prompt, Inc., home of Mammoth PostgreSQL - S/ODBC and S/JDBC
Postgresql support, programming shared hosting and dedicated hosting.
+1-503-667-4564 - jd@commandpromp t.com - http://www.commandprompt.com
Mammoth PostgreSQL Replicator. Integrated Replication for PostgreSQL
---------------------------(end of broadcast)---------------------------
TIP 9: the planner will ignore your desire to choose an index scan if your
joining column's datatypes do not match

Nov 23 '05 #4
On Wed, Oct 06, 2004 at 09:32:02AM -0700, Joshua D. Drake wrote:
What's "reasonable ?" ;)

BTW, "id" is a terrible name for a column. Better call it foo_id.


Hello,

I disagree with the idea that "id" is a terrible name for a column. The
only negative to it, is that you will have to be explicit in your
declarations when doing joins and such... ex:

SELECT * FROM foo
JOIN bar on (foo.id = bar.id)

Personally I would rather see, and write that then:

SELECT * FROM foo
JOIN bar on (foo_id = bar_id)


With all due respect, Josh, naming your columns with decipherable
names, i.e. *not* having 50 different things called "id" in your db
helps enormously with maintenance, especially when the current
maintainer has never met the designer, a common situation. Also, many
databases have documents that are inadequate, out of date, or both, so
decipherable names, along with as much other self-documentation, is a
big plus.

Cheers,
D

P.S. As a rule, SELECT * doesn't belong in production code.</nit>
--
David Fetter da***@fetter.or g http://fetter.org/
phone: +1 510 893 6100 mobile: +1 415 235 3778

Remember to vote!

---------------------------(end of broadcast)---------------------------
TIP 7: don't forget to increase your free space map settings

Nov 23 '05 #5
>>
SELECT * FROM foo
JOIN bar on (foo.id = bar.id)

Personally I would rather see, and write that then:

SELECT * FROM foo
JOIN bar on (foo_id = bar_id)

With all due respect, Josh, naming your columns with decipherable
names, i.e. *not* having 50 different things called "id" in your db
helps enormously with maintenance,


I agree 100%. For us, it makes sense to have id, because if we have
table foo, bar, and baz. We know that the primary key is always, foo.id,
bar.id and baz.id.

especially when the current maintainer has never met the designer, a common situation.
What is what documentation is for.

Also, many
databases have documents that are inadequate, out of date, or both, so
decipherable names, along with as much other self-documentation, is a
big plus.

Agreed. Which is why we make exhaustive use of the comment command :)
Cheers,
D

P.S. As a rule, SELECT * doesn't belong in production code.</nit>


Heh, I agree with this. SELECT * is bad for many, many reasons :). I was
just making an example.

Sincerely,

Joshua D. Drake

--
Command Prompt, Inc., home of Mammoth PostgreSQL - S/ODBC and S/JDBC
Postgresql support, programming shared hosting and dedicated hosting.
+1-503-667-4564 - jd@commandpromp t.com - http://www.commandprompt.com
Mammoth PostgreSQL Replicator. Integrated Replication for PostgreSQL
---------------------------(end of broadcast)---------------------------
TIP 2: you can get off all lists at once with the unregister command
(send "unregister YourEmailAddres sHere" to ma*******@postg resql.org)

Nov 23 '05 #6
On Wed, Oct 06, 2004 at 10:07:43AM -0700, Joshua D. Drake wrote:

SELECT * FROM foo
JOIN bar on (foo.id = bar.id)

Personally I would rather see, and write that then:

SELECT * FROM foo
JOIN bar on (foo_id = bar_id)

With all due respect, Josh, naming your columns with decipherable
names, i.e. *not* having 50 different things called "id" in your db
helps enormously with maintenance,


I agree 100%. For us, it makes sense to have id, because if we have
table foo, bar, and baz. We know that the primary key is always, foo.id,
bar.id and baz.id.


Coding standards are a Good Thing(TM). :)
especially when the current maintainer has never met the designer,
a common situation.


What is what documentation is for.


And in a perfect world... ;)
Also, many databases have documents that are inadequate, out of
date, or both, so decipherable names, along with as much other
self-documentation, is a big plus.


Agreed. Which is why we make exhaustive use of the comment command
:)


This brings up an interesting idea. What if it were possible to set
some kind of rules on DDL at database creation time? For example, I'd
like to be able to throw an error if somebody tries to name an object
any of the SQL keywords.

Other possible rules:

* Every table must have a comment
* noCamelCaps
* downcase all entities
* underbar_separa tors_required
* abbrev_w_beg, nt_by_rmvng_vwl s
* Your favorite algorithmic coding standard here

What do y'all think?

Cheers,
D
--
David Fetter da***@fetter.or g http://fetter.org/
phone: +1 510 893 6100 mobile: +1 415 235 3778

Remember to vote!

---------------------------(end of broadcast)---------------------------
TIP 2: you can get off all lists at once with the unregister command
(send "unregister YourEmailAddres sHere" to ma*******@postg resql.org)

Nov 23 '05 #7
On Wed, Oct 06, 2004 at 08:35:21 -0700,
sk******@comman dprompt.com wrote:
* Ben <be***@silentme dia.com> [2004-10-06 08:23:11 -0700]:

ALTER TABLE foo ADD CONSTRAINT must_have_a_or_ b CHECK (a::int IS NULL
AND b::int IS NOT NULL OR a::int IS NOT NULL AND b::int IS NULL);


A simpler constraint is:
check (a is null <> b is null);

---------------------------(end of broadcast)---------------------------
TIP 6: Have you searched our list archives?

http://archives.postgresql.org

Nov 23 '05 #8
This brings up an interesting idea. What if it were possible to set
some kind of rules on DDL at database creation time? For example, I'd
like to be able to throw an error if somebody tries to name an object
any of the SQL keywords.

Other possible rules:

* Every table must have a comment
* noCamelCaps
* downcase all entities
* underbar_separa tors_required
* abbrev_w_beg, nt_by_rmvng_vwl s
* Your favorite algorithmic coding standard here
Hmmm.. like a PostgreSQL syntax mode... in the PostgreSQL.conf :

syntax_mode = lazy, standard, strict

Where lazy is the current , standard would throw exceptions if you try
to use a reserved word or use uppercase, and strict would do things like
force a comment.

Ooooh... Bastard Operator From Hell here we come.

Sincerely,

From a managerial perspective I like it... Just like you have to be
explicit when using cvs or subversion to NOT comment.

Sincerely,

Joshua D. Drake


What do y'all think?

Cheers,
D

--
Command Prompt, Inc., home of Mammoth PostgreSQL - S/ODBC and S/JDBC
Postgresql support, programming shared hosting and dedicated hosting.
+1-503-667-4564 - jd@commandpromp t.com - http://www.commandprompt.com
Mammoth PostgreSQL Replicator. Integrated Replication for PostgreSQL
---------------------------(end of broadcast)---------------------------
TIP 2: you can get off all lists at once with the unregister command
(send "unregister YourEmailAddres sHere" to ma*******@postg resql.org)

Nov 23 '05 #9
Joshua D. Drake wrote:
This brings up an interesting idea. What if it were possible to set
some kind of rules on DDL at database creation time? For example, I'd
like to be able to throw an error if somebody tries to name an object
any of the SQL keywords.

Other possible rules:

* Every table must have a comment
* noCamelCaps
* downcase all entities
* underbar_separa tors_required
* abbrev_w_beg, nt_by_rmvng_vwl s
* Your favorite algorithmic coding standard here


Hmmm.. like a PostgreSQL syntax mode... in the PostgreSQL.conf :

syntax_mode = lazy, standard, strict

Where lazy is the current , standard would throw exceptions if you try
to use a reserved word or use uppercase, and strict would do things like
force a comment.


I'd much prefer if PostgreSQL had a generalized event or trigger system.
That way, one can install a trigger (or a plugin, or whatever) to trap
CREATE TABLE, CREATE VIEW, etc.

IMO, your proposed solution is too specific. There are too many coding
styles that people might want to enforce in their environment.

--
dave

---------------------------(end of broadcast)---------------------------
TIP 9: the planner will ignore your desire to choose an index scan if your
joining column's datatypes do not match

Nov 23 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
2301
by: Ruby Tuesdays | last post by:
Perhaps you database guru able to suggest what would be a good choice for opensource database platform to use to develop projects. At the moment the project is small/medium, but it will grow in size both data, users, and number of transactions. I'm using MySQL for right now but it lack of trigger, stored procedures, etc ... it sometimes...
0
2202
by: Rajesh Kapur | last post by:
We use InnoDB tables and foreign key constraints extensively. The mysqldump backs up the database tables in alphabetical order with foreign key constraints defined in the create statement of each table. These foreign key constraints are violated at the time of restore. We have tried the following two solutions... (1) We have tried to backup...
8
3737
by: Mike Nau | last post by:
We currently have all of our schema and testdata laid out in a large set of sql scripts. It currently takes about 15 minutes to run the scripts on a Dual 1.7ghz box with 1gb of ram. Does anyone know of a way to speed this up? Since our database is currently changing all the time, we are constantly having to re-create our database from...
3
2384
by: serge | last post by:
I have all the scrips to create a database. I have a few questions: 1- I am creating a batch file that it will call many lines like: db2 -td@ -f filename.sql -z output.txt The order i am using is: 1- Create the database 2- Tables 3- Insert the data
4
5571
by: Martin Pritchard | last post by:
Hi, I'm working on a project that historically contains around 40 enums. In the database various fields refer to the int values of these enums, but of course ref integrity is not enofrced and when looking at the db we can't tell what the value in a field represents. The other problem is that our enums are currently all stored in a single...
2
1643
by: Bill Moran | last post by:
Hello again, I'm developing a program based on PostgreSQL. It's consists of tables, constraints, _many_ stored procedures, 56M of test data, and a client app written in C. To help me with all of this, I obviously have a Makefile. I can do "make" to rebuild the C app (assuming I haven't added any bugs) and I can do "make database" to...
6
19578
by: sarada7 | last post by:
How to check if DB Constraints are enabled in a database?
12
6979
by: nyathancha | last post by:
Hi, I have a question regarding best practices in database design. In a relational database, is it wise/necessary to sometimes create tables that are not related to other tables through a foreign Key relationship or does this always indicate some sort of underlying design flaw. Something that requires a re evaluation of the problem domain?...
6
2837
by: x5Rb | last post by:
Database: 2 parent tables + 1 child table (child of both parent tables), enforcing foreign key constraints App: winformsapp, stuff (dataset, datatables, tableadatpers) generated by designer Thanks constraints (server's database) I must save changes in this order: 1. If I'm adding rows: a) add parents rows b) then add child rows...
30
5633
by: Neil | last post by:
Yikes! My database, which had been consistently 1 gig for a long time, went from being 1 gig to 3 gigs overnight! Looking at the nightly backups, the database increased on average about 5-15 MB per day, and was 1.06 GB on the Thursday night backup. Then, with the Friday night backup, it was 2.95 GB, and has stayed that way since! I did a...
1
7469
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7808
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6040
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5368
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5087
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3480
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1935
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1057
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
757
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.