A simple restriction of the domain of the SQL data type by a constraint is the most natural way. You have two options
- adding a constraint to the column of your table(s) in question
- creating a domain for each data type you want to be tailored to fit your given limitations of the programming language
An example of the first options looks like
ALTER TABLE table_name
ADD CONSTRAINT constraint_name
CHECK (column_name > -128 AND column_name < 127);
(change the table, constraint and column name as needed, and adjust the range of the checked bounds)
The second option is a two step process. First add a new domain to your DB (in this example the domain type is named c_byte8) with the command
CREATE DOMAIN c_byte8 NUMERIC CONSTRAINT c_byte8_constraint
CHECK (VALUE > -128 AND VALUE < 127);
Second create your table with the appropriate types, i.g. c_byte8 instead of NUMERIC as the attribute's type. E.g.
CREATE TABLE test_table_1 (id NUMERIC, byte_value c_byte8);
Please keep in mind that the first solution will require you to add constraints to each attribute you have defined, or you will define in the future. It makes your DB-create script less readable, because of the repetition of the same constraint condition every time you use the restricted domain. A second downside of this approach is, that you can easily miss some columns of your schema when you add those constraints to an existing schema.
An upside of the first solution is that you can extend an existing schema without much hassle.
As for the second approach, the plus is a more readable DB creation script, as is the same with the table definitions of a running production DB, but changing an existing production DB is more complicated.
In my opinion, a procedure should do one thing and one thing only. The logic should be performed in the application code. Secondly, a procedure should not return a result set. It should do work and then possibly return a status e.g. success, failure or something similar. Also, the name of your procedure is meaningless. Choose a descriptive name.
So, break it up into three procedures (insert, update and delete). Move the select statement into a view rather than a procedure. Let your programmers add the logic in their code to determine which one to call. Make sure you put index on the table.
Never do a select *
. Only select the columns you need. It may be that you add columns later on but you will still SELECT *
. Your SELECT *
will then store all (incl unnecessary columns) columns in memory and, in worst case, run out of memory and be swapping to disk.
Best Answer
There is no way to do that with table types. You will need to replicate that in all databases. But, if you want this table type available while creating a new database, you can just add it in the model database. There are a couple restrictions to using table-valued parameters. You can't even use table-valued parameters across databases. Check some details here.