I'm getting an error with this SQL code when I execute it in my Flask app. I swear I've done this exact thing before and it worked, so I'm not sure what's happening.
Here is the SQL:
DROP TABLE IF EXISTS user;
CREATE TABLE order (
id TEXT PRIMARY KEY,
plan_id INTEGER NOT NULL,
placed TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
ssh_key TEXT NOT NULL,
region INTEGER NOT NULL,
operating_system INTEGER NOT NULL,
enable_ipv6 INTEGER NOT NULL
expires INTEGER NOT NULL
);
Here is the relevant part of my python error:
sqlite3.OperationalError: near "order": syntax error
Thank you for the help
Order is a reserved keyword for sqlite. If you want to use a keyword as a name, you need to quote it.
https://www.sqlite.org/lang_keywords.html
I have table with chromosomes (objects that have length) and table with regions (for example genes) on the chromosomes (objects that have range defined as two integers - position start and position end). I would like to forbid inserting into database regions with coordinates greater than length of particular chromosome.
Is it possible in SQLite?
If not is it possible in any other SQL system (preferably free)?
DROP TABLE IF EXISTS chromosomes;
CREATE TABLE chromosomes
(
chromosome_id INTEGER UNIQUE NOT NULL CHECK(TYPEOF(chromosome_id) = 'integer'),
name VARCHAR UNIQUE NOT NULL CHECK(TYPEOF(name) = 'text'),
length INTEGER NOT NULL CHECK(TYPEOF(length) = 'integer' AND length > 0),
PRIMARY KEY (chromosome_id)
);
DROP TABLE IF EXISTS genes;
CREATE TABLE genes
(
gene_id INTEGER UNIQUE NOT NULL CHECK(TYPEOF(gene_id) = 'integer'),
symbol VARCHAR NOT NULL CHECK(TYPEOF(symbol) = 'text'),
refseq_id VARCHAR NOT NULL CHECK(TYPEOF(refseq_id) = 'text'),
chromosome_id INTEGER NOT NULL CHECK(TYPEOF(chromosome_id) = 'integer'),
start INTEGER NOT NULL CHECK(TYPEOF(start) = 'integer' AND start > 0 AND start < end),
end INTEGER NOT NULL CHECK(TYPEOF(end) = 'integer' AND end > 0 AND end > start),
external_db_link VARCHAR NOT NULL CHECK(TYPEOF(external_db_link) = 'text'),
PRIMARY KEY (gene_id)
FOREIGN KEY (chromosome_id) REFERENCES chromosomes(chromosome_id)
);
This type of constraint is not easily available in any database. In general, this would be handled using a trigger. The problem is that it is a constraint between two tables, but it does not use equality.
Triggers are available in SQLite as well as other databases.
One work-around is a check constraint using a user-defined function. The function can do the lookup into the chromosomes table and be used in a check constraint. SQLite doesn't really have user-defined functions. One database that supports this is Postgres.
Another option is to wrap all data modifications in stored procedures (this tends to be the way that I design systems). Then the stored procedure can do all the checks that are needed.
Redundantly - bring 'length' into the child table using a foreign key.
Then your Check Constraint can reference that.
I'm just feeling my way in the world of LINQ, and I've managed to get my VB app to SELECT from my Azure DB, now I'm trying to insert a new user.
I'm getting the error description 'Cannot insert explicit value for identity column in table x when IDENTITY_INSERT is set to OFF'.
I know there are similar questions on this subject, but it seems that most of them are having the problem because they ARE trying to insert an explicit value, and need to change the flag to ON. I'm trying to do the insert while letting the DB assign the ID value, so I believe the IDENTITY_INSERT flag should be off, and I need to change my VB or LINQ statement to tell it not to try to assign a value to my ID column.
Here is my code in the VB form button that runs the insert:
Private Sub btn_AddUser_Click(sender As Object, e As EventArgs) Handles btn_AddUser.Click
Dim db As New stem1DataContext()
Dim usr As New Users With
{.firstname = txt_firstName.Text,
.lastname = txt_lastName.Text,
.username = txt_username.Text,
.password = txt_password.Text,
.email = txt_email.Text}
db.Users.InsertOnSubmit(usr)
Try
db.SubmitChanges()
Catch
MsgBox("Form not valid: " & Err.Description)
End Try
End Sub
The usr statement has all fields in the table, besides the ID column. I don't really see where my code is trying to insert a value for the ID column, but it may be doing it somewhere behind the scenes and I need to explicitly tell it not to.
The SQL for the Users table is below:
CREATE TABLE [dbo].[Users] (
[Id] INT IDENTITY (1, 1) NOT NULL,
[username] VARCHAR (50) NOT NULL,
[firstname] VARCHAR (50) NOT NULL,
[lastname] VARCHAR (50) NOT NULL,
[email] VARCHAR (50) NOT NULL,
[password] NCHAR (10) NULL,
PRIMARY KEY CLUSTERED ([Id] ASC)
);
Any help would be greatly appreciated.
For anyone else who comes across this, I solved it by going into the .dbml for my connection, and under the ID column for the table, looked at the properties and set the property "Auto Generated Value" to TRUE.
I also realised, the reason I had this issue. When I first created the table, I forgot to set the ID field to an Identity column. I later went back in and updated the table. It turns out this update doesn't modify the dbml, which I guess makes sense, but you need to know that you need to go in and make this additional change to avoid the issue.
I have just started with SQL and want to clear the basic keywords of SQL.
What is the difference between
"number" and "numeric" & "number & integer"?
While creating a table
Create table myTable
(
my_Id int(6) primary key
...
Above query Gives me an error suggesting to put null or not null before "primary key".
Do I always need to put either null or not null for the keyword integer?
If I replace int(6) with number(6), that statement works.
1."number" and "numeric" & "number & integer"?
An integer cannot take inputs such as 1.1 and the likes since float or decimal datatype handles this, while a number can take this both. I believe the reason why INT does not display it with a decimal its because its being rounded off try to input a 1.5 on an int column and you'll get a 2 instead
2.While creating a table
Create table myTable (
my_Id int(6) primary key, <--- Gives me an error suggesting to put
null or not null before "primary key". Do I always need to put either
null or not null for the keyword integer?
you need to either put a null or not null before a primary key unless I believe its been set into an Auto Increment
BTW my answer was based on MYSQL since that's what I used.. although I'm not sure if your using it since you didn't add any tags :)
for more info for this topic I think this could add a little more light to your inquiry
reference link
In MYSQL a primary key has to be a non-null value ie you will have to indicate by typing in NOT NULL You can re-write the code as follows:
my_id INT([optional]) PRIMARY KEY NOT NULL
When you want to make a Primary Key field it shouldn't be Null.
And
When you use int data type it don't have any (<value>), But number has.
SO
my_Id int not null primary key
I have the following table:
CREATE TABLE `events` (
`evt_id` int(11) NOT NULL AUTO_INCREMENT,
`evt_name` varchar(50) NOT NULL,
`evt_description` varchar(100) DEFAULT NULL,
`evt_startdate` date NOT NULL,
`evt_enddate` date DEFAULT NULL,
`evt_starttime` time DEFAULT NULL,
`evt_endtime` time DEFAULT NULL,
`evt_amtpersons` int(11) DEFAULT NULL,
`sts_id` int(11) NOT NULL,
`adr_id` int(11) DEFAULT NULL,
`evt_amtPersonsSubs` int(11) DEFAULT NULL,
`evt_photo` varchar(50) DEFAULT NULL,
`sys-mut-dt` timestamp NULL DEFAULT NULL,
`sys-mut-user` varchar(20) DEFAULT NULL,
`sys-mut-id` int(11) NOT NULL DEFAULT '0',
PRIMARY KEY (`evt_id`),
KEY `sts_id` (`sts_id`),
KEY `adr_id` (`adr_id`),
CONSTRAINT `sts_id` FOREIGN KEY (`sts_id`) REFERENCES `statusses` (`sts_id`) O
N DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB AUTO_INCREMENT=10 DEFAULT CHARSET=latin1
Now I have got two problems:
Here is my query:
INSERT INTO `events`(`evt_name` , `evt_description` , `evt_startdate` , `evt_enddate` , `evt_starttime` , `evt_endtime` , `evt_amtpersons` , `sts_id` , `adr_id` , `evt_amtPersonsSubs` , `evt_photo` , `sys-mut-user` , `sys-mut-id`) VALUES ('asf' , 'asf' , '2009-04-02' , '2009-04-22' , '00:00:00' , '00:00:00' , '3' , '1' , '' , '' , '' , 'test' , '1')
When I execute this query through my php programs I get no error. But when I execute the query in a shell directly on the mysql database I get two warnings. How can I get PHP to alert me when there are warnings because if there are warnings mysql doesn't do the insert.
About the warnings:
| Warning | 1366 | Incorrect integer value: '' for column 'adr_id' at row 1
| Warning | 1366 | Incorrect integer value: '' for column 'evt_amtPersonsSubs' a t row 1
How can I get rid of these warnings. Tried to make some changes but it didn't work out so far.
You are inserting an empty string. You should remove the '' and put a number in that field
As you said, the column does not have to have a value specified when you insert. The fact is indicated by the "DEFAULT NULL" for that column at table creation. This fact, however, means that if you do not specify the column name in your list of columns while doing INSERT (and therefore you will not specify the corresponding value either), then the tuple can be inserted anyway, and for that column value you will get a NULL automagically by default.
However, in your query you specify that you are going to insert that column value, and the column value you say is '' (an empty string). This is of course not valid, because that column accepts integers (or NULL, because you havent' declared the column NOT NULL), and an empty string is an empty string, not an integer.
The SQL server is generous and accepts the empty string anyway (probably it casts it to zero) but reports you a warning. If you set a strict mode for the server (something I strongly suggest you to do), you will get an error and the insert will fail.
Please note that if you follow my suggestion of setting strict mode, this is server wide, involving all your databases and all your tables (at least with the mysql released one year ago). If you have awfully written software that need a forgiving server, then you cannot use it.
The error message tells you that that the empty string ('') is not a valid value for an integer field - in this case the fields adr_id and evt_amtPersonsSubs. Did you mean to put NULL instead?
In PHP, you can retrieve the error or warning message, for the most recent query only, using the mysql_error() function.
'' is not an integer.... how about using NULL in the query if you actually want a null value?
The warnings tell you that you're trying to insert a string value into an integer column.
In all the places where you have an int column you must not put the value between ' but just put the value as is
[...]'00:00:00' , '00:00:00' , 3 , 1 , [...]
If you don't want to provide a value for a certain column you should define the column with NULL. Then you can even leave your '' for the insert.
BUT
In general it's bad practice to do inserts like that. What if you one day need to add a column to your table? Then you have to go and rewrite your code as well.
Therefore you should do inserts like that:
INSERT INTO tbl_name (col1, col2) VALUES(value1, value2);
This way your code will still work, even if you decide to add columns. Plus the code is easier to read!!
Implicit defaults are defined as follows:
For numeric types, the default is 0, with the exception that for integer or floating-point types declared with the AUTO_INCREMENT
attribute, the default is the next value in the sequence.
Reference:
MySQL 5.7 Reference Manual / Data Types / Data Type Default Values