By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,436 Members | 2,994 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,436 IT Pros & Developers. It's quick & easy.

add constraints to views

P: n/a
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Please consider the following situation.

CREATE TABLE test_table (
~ id SERIAL PRIMARY_KEY,
~ tag BOOLEAN,
~ field1 INTEGER
);

CREATE VIEW test_view (
~ SELECT * FROM test_table WHERE tag
);

Now I want ad a NOT NULL constraint to the view on field1. I tryed the
following, but neither works.

ALTER TABLE test_view ADD CONSTRAINT isit CHECK ( field1 IS NOT NULL);
ALTER TABLE test_view ALTER field1 SET NOT NULL;

How can I do this?

Regards

Andreas
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.3 (GNU/Linux)
Comment: Using GnuPG with Debian - http://enigmail.mozdev.org

iD8DBQE/bCnEPkvkZVZzNY0RAgiBAKChAPbr+RV1bTYX5+2vnCg/KU6k5ACfeGmd
/tbh47tLhPee5mAkFLzODZU=
=LDWj
-----END PGP SIGNATURE-----

---------------------------(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 11 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Andreas Fromm wrote:
CREATE VIEW test_view (
~ SELECT * FROM test_table WHERE tag
);

Now I want ad a NOT NULL constraint to the view on field1. I tryed the
following, but neither works.


what does mean add a null constrain to a view ? Do you mean
filter out the records with the field1 null ?

CREATE OR REPLACE test_view AS
SELECT *
FROM test_table
WHERE field1 IS NOT NULL AND
tag;
Regards
Gaetano Mendola
---------------------------(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 11 '05 #2

P: n/a
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Gaetano Mendola wrote:
| Andreas Fromm wrote:
|
|> CREATE VIEW test_view (
|> ~ SELECT * FROM test_table WHERE tag
|> );
|>
|> Now I want ad a NOT NULL constraint to the view on field1. I tryed the
|> following, but neither works.
|
|
| what does mean add a null constrain to a view ? Do you mean
| filter out the records with the field1 null ?
|
| CREATE OR REPLACE test_view AS
| SELECT *
| FROM test_table
| WHERE field1 IS NOT NULL AND
| tag;
|
No, I mean that the view behaves like a table with the same columns as
table, but that restricts to records on wich tag is set. To insert a
record to this "special" table it requires to have field1 set. In other
words: A record of table is a record of view if tag is set. If tag is
set, then field1 has also have to have a value. If tag is not set, it
may have a value for field1, but will not show up in the view. Of course
I could achive this be triggers, but I thought it could be possible to
do via constraints on the view.

Regards

Andreas
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.3 (GNU/Linux)
Comment: Using GnuPG with Debian - http://enigmail.mozdev.org

iD8DBQE/bF30PkvkZVZzNY0RAqh8AJ0ZwagGrOhyuma/7gARKl1l35/wOACfVj9d
xYvyd2Pet25drqcv4vBE5eg=
=jXUQ
-----END PGP SIGNATURE-----
---------------------------(end of broadcast)---------------------------
TIP 7: don't forget to increase your free space map settings

Nov 11 '05 #3

P: n/a
Andreas Fromm <An***********@physik.uni-erlangen.de> writes:
No, I mean that the view behaves like a table with the same columns as
table, but that restricts to records on wich tag is set. To insert a
record to this "special" table it requires to have field1 set. In other
words: A record of table is a record of view if tag is set. If tag is
set, then field1 has also have to have a value. If tag is not set, it
may have a value for field1, but will not show up in the view. Of course
I could achive this be triggers, but I thought it could be possible to
do via constraints on the view.


Constraints on a view are meaningless --- it has no real rows to
constrain. Put the constraints on the underlying table.

regards, tom lane

---------------------------(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 11 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.