How to add a NOT NULL foreign key to SQlite3 table in node.js - node.js

I am having some trouble adding a foreign key column to an already existing table on SQLite.
Here's my SQL code:
CREATE TABLE IF NOT EXISTS director (
director_id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL
);
CREATE TABLE IF NOT EXISTS movie (
movie_id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL
);
ALTER TABLE movie ADD COLUMN director_id INTEGER NOT NULL REFERENCES
director(director_id);
This give me the following error:
Uncaught Error: Cannot add a NOT NULL column with default value NULL
However, if I remove the "NOT NULL" constraint it runs fine:
ALTER TABLE movie ADD COLUMN director_id INTEGER REFERENCES
director(director_id);
The following also works just fine but I already have an existing table:
CREATE TABLE IF NOT EXISTS director (
director_id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL
);
CREATE TABLE IF NOT EXISTS movie (
movie_id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL,
director_id INTEGER NOT NULL,
FOREIGN KEY(director_id) REFERENCES director(director_id)
);
Am I missing something or it cannot be made in SQLite?

When altering a table to add a column with NOT NULL, you need to add a DEFAULT value. Otherwise, it has no idea what to put in an existing record when adding the column (since NULL is not valid).
Docs ref: https://sqlite.org/lang_altertable.html
For example:
CREATE TABLE IF NOT EXISTS movie (
movie_id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL
);
ALTER TABLE movie ADD COLUMN director_id INTEGER NOT NULL DEFAULT 0 REFERENCES director(director_id);

Related

Sqlite Autoincrementing a field in a table

I have a table PatientRegistration with Columns
ID INTEGER PRIMARY KEY AUTOINCREMENT,
NAME VARCHAR,
PHONE TEXT,
AGE BLOB,
TURNNUMBER INT,
REG_DATE DATE TIME
I have been having troubles trying to automatically declare TURNNUMBER and assign it 0(zero) if it's null else increase it by +1 with respect to date, this is correct but only in SQL
declare #turn tinyint,
#turn=isnull((select top 1 pa.turn from PatientRegistration pa where Day(pa.Reg_Date)=#day order by pa.ID desc),0)+1
How can I achieve this using SQLite,,,, please
Edit: this should happen each a new patient is added

Python sqlite3 OperationalError in a simple table create

here is the code
def __init__(self):
self._db = sqlite3.connect("Reservation.db")
self._db.row_factory = sqlite3.Row
self._db.execute("create table if not exists Ticket(ID integer primary key autoincrement, Name text, Gender text, Order text)")#create a table called Ticket with 4 columns
self._db.commit()
the proplem
self._db.execute("create table if not exists Ticket(ID integer primary key autoincrement, Name text, Gender text, Order text)")
sqlite3.OperationalError: near "Order": syntax error
order is a reserved word in SQL. I suggest you find a different name, that isn't a reserved word (e.g., order_text) for the column. If you absolutely must use this name, you can escape it by surrounding it with double quotes ("):
self._db.execute("create table if not exists Ticket(ID integer primary key autoincrement, Name text, Gender text, \"Order\" text)
# Here -----------------------------------------------------------------------------------------------------------^------^

Alter column in table to auto increment in Sybase ASE 16.0

I am using Sybase ASE16.0 database in which I am trying to alter a column in an existing USER table so that it autoincrements every time a row is added to the table. The column: user_id is set as primary key and not null.
I have gone through many sybase tutorials and have tried many approaches but of no avail. Here are some queries that I wrote to make this change:
ALTER TABLE USER (user_id smallint IDENTITY not null)
ALTER TABLE USER ALTER user_id smallint IDENTITY not null
ALTER TABLE USER MODIFY user_id smallint NOT NULL IDENTITY
ALTER TABLE USER MODIFY user_id smallint NOT NULL AUTO_INCREMENT
ALTER TABLE USER MODIFY user_id smallint NOT NULL AUTOINCREMENT
ALTER TABLE USER ALTER user_id smallint NOT NULL AUTOINCREMENT
ALTER TABLE USER user_id smallint AUTOINCREMENT
I expect a SYBASE DB compliant query that would alter the user_id column in the table to autoincrement it by 1 on adding a new record
From documentation:
Adds an IDENTITY column to a table. For each existing row in the table, Adaptive Server assigns a unique, sequential column value. The IDENTITY column could be type numeric or integer, and a scale of zero. The precision determines the maximum value (10 5 -1, or 99,999) that can be inserted into the column:
alter table sales_daily
add ord_num numeric (5,0) identity
Found here

In Microsoft SQL Server 2014, how do I set another key when there's already a primary key in that table?

Apologies in advance that this is definitely a beginner issue; but I'm genuinely having trouble finding the solution.
In the chart linked here, I am told which columns must be linked to other tables. chart
However, I'm stumped with the Books table because it needs to have 2 keyed columns. One is the primary key (Book ID), which I've already set up. But the Title is also supposed to be keyed with the Book ID in the Book Copies table. I tried to specify Book ID as a foreign key that references the Title in Books and got an error message saying
No primary or candidate key are referenced in 'Books'
This is because I didn't know how to set Title as a key, since one already exists in that table. My table creation code for Books and Book Copies is shown below. The foreign key link doesn't work right now because I'm unsure how to reference a column that is not a primary key. If someone could help me out with how to set "Title" in "Books" as a non-primary key that can be referenced by "Book ID" in "Book Copies", I'd truly appreciate it.
Thanks!!
CREATE TABLE Books
(
Book_ID INT PRIMARY KEY NOT NULL IDENTITY (1,1),
Title VARCHAR(50) NOT NULL
Publisher_NAME VARCHAR(50) NOT NULL
CONSTRAINT fk_Publisher_Name
FOREIGN KEY REFERENCES Publisher(Publisher_Name)
ON UPDATE CASCADE ON DELETE CASCADE,
);
CREATE TABLE Book_Copies
(
Book_ID INT NOT NULL
CONSTRAINT fk_Title
FOREIGN KEY REFERENCES Books(Title) ON UPDATE CASCADE ON DELETE CASCADE,
Branch_ID VARCHAR(50) NOT NULL,
Number_Of_Copies INT NOT NULL
);

Cassandra Update/Upsert does not set Static Column?

I am trying to "Upsert" data into my table with CQLSSTableWriter. Everything works fine, except for my static column not being set correctly. They end up being null for every occasion. My static column is defined as brand TEXT static.
After failing with the CQLSSTableWriter, I went into the cqlsh and tried to update the static column manually:
update keyspace.data set brand='Nestle' where id = 'whatever' and date = '2015-10-07';
and with a batch as well (even though it should not matter)
begin batch
update keyspace.data set brand='Nestle' where id = 'whatever' and date = '2015-10-07';
apply batch;
My "brand" column still shows null when I retrieve some of my data (select * from keyspace.data LIMIT 100;)
My entire schema:
CREATE TABLE keyspace.data (
id text,
date text,
ts timestamp,
id_two text,
brand text static,
latitude double,
longitude double,
signals_double map<text, double>,
signals_string map<text, text>,
name text static,
PRIMARY KEY ((id, date), ts, id_two)
) WITH CLUSTERING ORDER BY (ts ASC, id_two ASC);
The reason why I chose Update instead of Insert is because I have collections that I do not want to overwrite, but rather add more elements to. Using insert would overwrite the previously stored elements of my collections.
Why can I not set a static column with an Update query?

Resources