I am using SQL Server Management Studio and would like to create a table that when dates are entered in to the table, it checks that someone is over 18.
I have got as far as:
CREATE TABLE tCustomer
(
FirstName VARCHAR(20) NOT NULL,
DateOfBirth DATE CHECK (DateOfBirth < GETDATE())
);
I know this is currently wrong as all it is doing is checking that the DateOfBirth is less than the current time. But I can't quite seem to find what I am looking for.
If it is too complex for create table, maybe some sort of trigger? I do not really want to use a stored procedure.
Your help would be appreciated.
Cheers.
This condition is invalid in a check constraint because it is non-deterministic. If the clock is turned back suddenly rows that were valid could now become invalid. The constraint would retroactively be invalidated.
You should probably move this check to the application.
Related
I'm having some issues getting a query that worked just fine in SQL Server 2008 R2 and after upgrading to 2014 it no longer does. Any help would be appreciated. The error i'm getting is:
Msg 213, Level 16, State 7, Line 1
Column name or number of supplied values does not match table definition.
DBCC execution completed. If DBCC printed error messages, contact your system administrator.
Here is the Query:
use AccessControl
set nocount on
declare #MaintResults table
(
Result int identity(1,1),
Cardholder_Count varchar(15),
Events_Count varchar(15),
User_Count varchar(15),
etc...
)
The query all together is about 10 pages long. Its purpose is to pull a decent amount of information out of a database, into a temporary table and where it can be viewed. I'm honestly not sure why i'm getting this issue because the name of the database is in fact "AccessControl".. so why am i getting the error I am?
For us to solve this for you, we will need to see the entire statement.
As plain as the error states, either you are selecting a different number of columns when you insert into the table variable
But you stated nothing has changed except an upgrade to SQL server
Otherwise, one of the types in the select statement is no longer compatible with the table variable. This could be as simple as one or two of the fields are the wrong way around, from your select and your insert statements.
I'm not going to analyse too deeply what caused this without a lot more information from OP but if you have upgraded your DB and your application is maintaining the Schema it could be that some minor schema changes were also introduced, either that or some implicit type conversion that worked in the previous version is no longer supported?
Run the Select to fill the table variable on its own, manually review that types are ok and that they match your table variable.
You can cheat by saving the select as a view and then compare the column definitions of the view to your table variable
I have 3 tables, "Courses"(id, start_date), "Subscriptions"(id, assistant_id, course_id, date) and "Assistants"(id, registration_date).
Subscriptions reference Courses and Assistants with foreign keys as you see.
I need to add CHECK constraint that will prevent to create Subscription record if referenced Courses.start_date is older than referenced Assistants.registration_date. Is there a way to do this in Libre Base?
Table organization could not be changed.
Such a CHECK constraint cannot be created with the default engine. From the HSQLDB 1.8 documentation:
ALTER TABLE <tablename> ADD [CONSTRAINT <constraintname>]
CHECK (<search condition>);
Adds a check constraint to the table. In the current version, a check constraint can reference only the row
being inserted or updated.
This means that commands like the following from TestSelfCheckConstraints.txt produce an error:
/*e*/CREATE TABLE TC6(A CHAR, B CHAR, C CHAR, D INT, CHECK(A IN (SELECT A FROM
TC5)));
So, to perform such a check, you will have to verify it ahead of time (or afterwards) using a query. This could be done for a form by adding a macro in the Events tab. See this post for ideas: https://forum.openoffice.org/en/forum/viewtopic.php?f=20&t=21414.
The default engine is rather old, so for such complex requirements it may be better to set up LibreOffice Base to use a different database engine. For example using MySQL, it is possible to set up a stored procedure trigger to do this kind of checking. See CHECK constraint in MySQL is not working.
I looked around for an answer to this. How to add a column using SQL in an Oracle Database.
I keep finding the same answer, but my Oracle SQL Developer tool keeps telling me that the syntax is wrong even though I write it exactly as they do.
What am I missing exactly? (Before you ask, yes I do use ALTER TABLE before this)
The syntax is supposed to be:
ADD Column_Name constraint Data_Type;
Issue is, I have no constraints for this column so I've seen examples not use it. I tried that as well and I get the same error. The value can be null and have no constraints, yet I am not allowed to do this:
ADD SERIES_YEAR NUMBER(2,10);
Any suggestions? It's probably something incredibly simple.
EDIT: Here is the error it gives me:
You are missing parentheses. Try the following:
ALTER TABLE
Foo
ADD
(
SERIES_YEAR NUMBER(2,10) NOT NULL,
);
You have to write the data type before the constraint, not the opposite, like:
ADD
(
column1_name column1_datatype column1_constraint,
column2_name column2_datatype column2_constraint
);
I want to create a table for making a comment box. I was told that I should be wary of sql injection (dont even know what that means).
So I thought I should ask around at SO. my requirements are:
Comments table
a comment row ~400 chars
aid -> every comment should be linked to an aid. duplicates should be allowed. means aid = 21, can have more than 1 comment. I should be able to search through the DB to see all the comments related to aid = 21.
timestamp for the comment
userid for the comment.
A MySQL query for the above table that should not allow SQL injection. I am pretty confused. any help would be highly appreciated. thanks a lot in advance.
Creating a table usually happens only once, when the system is installed. There is, therefore, no risk of SQL injection (which happens when a query is run with data provided by the user).
The above description would probably be implemented as:
CREATE TABLE `comment` (
`comment_id` INTEGER NOT NULL AUTO_INCREMENT PRIMARY KEY,
`comment_text` VARCHAR(400) NOT NULL,
`aid_id` INTEGER NOT NULL REFERENCES `aid`(`aid_id`),
`comment_time` DATETIME NOT NULL,
`user_id` INTEGER NOT NULL REFERENCES `user`(`user_id`)
);
Try and use stored procedures in mysql .
Use parameters to pass the input to the stored procedure.
thuis tutorial is for you .
http://www.tizag.com/mysqlTutorial/mysql-php-sql-injection.php
SQL injection is explained at Wikipedia and other places.
Use mysql_real_escape_string() or stored procedures are standard techniques that will avoid SQL injection.
I need to test a column which changed from varchar(30) to varchar(65). I don't have a access to insert a value into that column.Is there any other way so that i can test the column?
Your best bet is to put together a testing database and make the change there first. Point your application at the test database and see what happens.
Use "desc" in oracle or an equivalent command depending on the database to describe the table. This will list out all the table columns along with their data types.
DESC TableName shows the table structure. Isn't that more than an assurance? :-D