I was thinking about a simple yet good way to support check constraints in PA.
After checking the syntax and support across different DBMS I noticed that the basic stuff is really pretty standard.
I think a simple yet pretty powerful way to support this, would be to define them on table level (as a check constraint could possibly involve more than one column). A check constraint would simply consist of two input fields: the name of the constraint and the expression (e.g. salary > 0). The UI would need to allow multiple entries and the DDL Generators need to be adjusted in order to include the constraints after the column definitions.
I have seen pretty sophisticated "designers" for this kind of thing, but after all any "guided" selection will always have some kind of limitation which a simple text field does not suffer from :)
If there is a need to support multiple DBMS (which the "constraint designers" try to support by having a very high-level definition) I think a third property that selects the DBMS (or more precisely the DDL Generator) for which the specific constraint applies should take care of most of cases.
My proposal for managing and storing them is something similar to the index handling.
The table popup menu would have two new items: "New Check Constraint", and"Check Constraint Properties" (as a sub-menu, just like the indexes).
Each of them would open a dialog with the above mentioned input fields (a JTextField for the name and a JTextArea for the expression)
In the XML this could be stored in a new folder (name="Check Constraints" or something similar)
The biggest problem will be the model compare I guess.
What do you think about this?
I can do the implementation as having check constraints inside PA would be a major improvement for us.
I will most probably need help for the XML reading/saving part though
Regards
Thomas
Thomas O'Brien, 17.06.2010 00:10:
> It's interesting that you bring this up now because we are actually
> working on a feature similar to this on our enterprise edition.
>
> I don't know if you've tried out the preview release of the enterprise
> edition yet but we have added the concept of domains and user defined
> data types where you can also add constraints.
A domain is not the same thing and can especially not capture a constraint like (column1 > column2).
Additionally I'm not very happy that such an important feature is only available in the enterprise edition. Check constrains are an important part of the model.
As we are using the "community" version, any feature in the enterprise version is not available for us.
> If you looked at a recent build of SQL Power Architect you would see
> there are now check boxes beside some of the fields in the column
> editor.
Yes I noticed that. Very nice, especially for data types that don't need scale or precision.
> This is because on the enterprise edition you can choose to override
> the data type information or un-check the box beside the field to keep
> the values defined in the domain or type. To introduce constraints
> into the community edition of SQL Power Architect we would need to do
> a similar overriding field on the column to override the domain or
> types constraints and the community edition would always be overriding
> the empty constraint of the data type.
Hmm, I don't think it would need to override the domain check.
Just think of a domain "positive_integer", you might want a check constraint (column1 > column2).
One shouldn't think of a check constraint as a column level but a table level constraint.
> If the constraint stays as a text field it can be a property of the
> object directly and doesn't need to be a new folder in the XML.
> Hopefully comparing will also be simple enough to do.
As I said: there could be multiple constraints on the table (col1 > col2), (col4 between 1 and 100) and so on
So it will at least be a collection attribute of the table.
Regards
Thomas
> Sorry, I must not have been clear about what we would like to do in
> terms of constraints in the community edition versus constraints in
> the enterprise edition. We would like to put constraints into both the
> table and the column editors for both editions.
Great! Thanks for the clarification.
> In the current UI design of the domain editor the constraint editor is
> just a text field so the suggestion was just to keep the UI consistent
> for the column editors.
With "just a textfield" I miss the possibility to give the constraint a name.
> That being said what benefit
> do you get in terms of constraints on a table or in terms of the
> editor if you place two constraints on a table (col1> col2), (col4
> between 1 and 100) versus one constraint that ands them together (col1
> col2 and col4 between 1 and 100)?
Having different names for the constraint, so that the error message would immediately indicate which constraint has been violated.
Naming constraints is very useful when you need to find an error in logfiles. As you usually don't see the actual values in there the name of the constraint can be of great help. I even know people who give every NOT NULL constraint a name (which is very handy sometimes...)
Regards
Thomas
> Reverse engineering and comparing data models on tables and columns
> that have constraints may be a bit tricky though.
Been there, done that ;)
Check out my SQL Workbench/J source code.
I have implemented the retrieval for check constraints (table and column level) for all major DBMS.
The interesting classes are workbench.db.AbstractConstraintReader and then descendant classes for the specific databases.
The source code can be downloaded from my homepage (http://www.sql-workbench.net) or through svn (details on the homepage)
Feel free to either re-use the source code, or to simply grab the necessary SQL statements from the classes.
Regards
Thomas
thanks a lot for the long explanation.
> With respect to the UI, there are CheckConstraintTable,
> CheckConstraintTableModelListener, AddCheckConstraintTableRowAction,
> and RemoveSelectedTableRowsAction classes that deal with operations on
> a SQLCheckConstraintContainer. When table and column level check
> constraints are implemented, these components should be able to be
> reused without loss of generality. If you would like to see how it
> works, SQL Power Architect Enterprise Edition Preview 5 has this
> implemented in the Domains& Data Types managers.
Do I understand this correctly, that the UI and code for supporting check constraints is there, but not available in the community edition?
Regards
Thomas