sqlite insert with primary key - ios

I use sqlite(3.7.4) in iphone.
I create a table like:
create table A (UserName varchar (50) primary key, Num integer);
Then I insert the record below twice:
('abc',1);
Normally there should be only one record in DB.
However I found in the DB
(abc,1);
( ,1);
I'm confused that as UserName is primary key and why there are two records!
I don't know what's the problem.
Can anyone help me?
thank you.

Why you want use username as primary key. Primary key should unique identify record. What is why when you inserted twice in second time you have primary key constraint violation
create table A (UserName varchar (50) primary key, Num integer, unique (UserName));

Related

Unable to find column names in a FK constraint

I have created two tables in Snowflake.
create or replace TRANSIENT TABLE TESTPARENT (
COL1 NUMBER(38,0) NOT NULL,
COL2 VARCHAR(16777216) NOT NULL,
COL3 VARCHAR(16777216) NOT NULL,
constraint UNIQ_COL3 unique (COL3)
);
create or replace TRANSIENT TABLE TESTCHILD3 (
COL_A NUMBER(38,0) NOT NULL,
COL_B NUMBER(38,0) NOT NULL,
ABCDEF VARCHAR(16777216) NOT NULL,
constraint FKEY_1 foreign key (COL_A, COL_B) references TEST_DB.PUBLIC.TESTPARENT1(COL1,COL2),
constraint FKEY_2 foreign key (ABCDEF) references TEST_DB.PUBLIC.TESTPARENT(COL3)
);
Now I want to execute a query and see the names of columns that are involved in FKEY_2 FOREIGN KEY
in Table TESTCHILD3, but it seems like there are no DB Table/View that keeps this information. I can find out the column names for UNIQUE KEY & PRIMARY KEY but there is nothing for FOREIGN KEYS.
EDIT
I have already tried INFORMATION_SCHEMA.TABLE_CONSTRAINTS, along with INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS and all the other system tables. No luck. Only DESC TABLE is giving me some info related to CONSTRAINTS and COLUMNS but that also has FOREIGN KEY CONSTRAINTS information missing.
SHOW IMPORTED KEYS IN TABLE <fk_table_name>;
Updated answer:
I was checking on something unrelated and noticed a very efficient way to list all primary and foreign keys:
show exported keys in account; -- Foreign keys
show primary keys in account;
When you limit the call to a table, it appears you have to request the foreign keys that point to the parent table:
show exported keys in table "DB_NAME"."SCHEMA_NAME"."PARENT_TABLE";
You can check the documentation for how to limit the show command to a specific database or schema, but this returns rich information in a table very quickly.
maybe you can try to query this view: INFORMATION_SCHEMA.TABLE_CONSTRAINTS
Note: TABLE_CONSTRAINTS only displays objects for which the current role for the session has been granted access privileges.
For more see: https://docs.snowflake.net/manuals/sql-reference/info-schema/table_constraints.html

Save a list of strings to sqlite database in Swift 4

Is it possible to save a list of strings into a SQLite column in swift4?
If you're looking for an array data type like you find in some SQL engines, SQLite does not have that. You theoretically could encode this list somehow (e.g. a JSON array), but that's pretty kludgy. So, I'd probably go ahead and normalize that, putting the multiple strings in a separate table.
E.g. Let's say you wanted a table users, that had user_id, name, and an array of privileges. You'd probably instead do something like:
CREATE TABLE users (
user_id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL
);
CREATE TABLE users_privileges (
user_privilege_id INTEGER PRIMARY KEY AUTOINCREMENT,
user_id INTEGER NOT NULL,
privilege TEXT NOT NULL,
FOREIGN KEY (user_id) REFERENCES users (user_id)
);

Creating a 'join' table - sqlite3

I think I'm pretty close on this one, but can't get it to click.
I've got two simple tables set up.
Table A:
CREATE TABLE customer(
id INTEGER PRIMARY KEY,
first_name TEXT,
last_name TEXT,
email TEXT UNIQUE NOT NULL,
password TEXT NOT NULL,
create_time TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP
);
I've got two rows of data populating correctly in Table A.
Table B:
CREATE TABLE address(
...> id INTEGER PRIMARY KEY,
...> street_address_1 TEXT NOT NULL,
...> street_address_2 TEXT,
...> street_address_3 TEXT,
...> city TEXT NOT NULL,
...> state TEXT NOT NULL,
...> zip TEXT NOT NULL);
And I've successfully imported a CSV file into that table.
I'm trying to create a 3rd table that joins Table A to Table B with the use of Foreign Keys.
I can create the table with the code below, but when I try to select the table, I'm getting a blank, which means I'm obviously doing something wrong. I'm expecting to see data where the two tables overlap on mutual Id numbers, i.e. where the ID from customer = Id from address I'd like to see the data from both tables for those rows appear in Table C.
Table C (the join table):
CREATE TABLE customer_address(
id INTEGER PRIMARY KEY,
customer_id INTEGER,
address_id INTEGER,
first_name TEXT NOT NULL,
last_name TEXT NOT NULL,
email TEXT NOT NULL,
password TEXT NOT NULL,
street_address_1 TEXT NOT NULL,
street_address_2 TEXT,
street_address_3 TEXT,
city TEXT NOT NULL,
state TEXT NOT NULL,
zip TEXT NOT NULL,
FOREIGN KEY (customer_id) REFERENCES customer(id),
FOREIGN KEY (address_id) REFERENCES address(id)
);
Thanks!
I imported the data to the address table using this:
sqlite> .mode csv
sqlite> .import address.csv address
I manually typed in data to the first table using this:
insert into customer(first_name, last_name, email, password)
values('Ad','Mac','a.Mac#gmail.com','Mab'),('Brian','Obrien','bob#example.com','123456');
Don't duplicate the data in your join table (often called a bridge table). This should do for Table C:
CREATE TABLE customer_address(
id INTEGER PRIMARY KEY,
customer_id INTEGER,
address_id INTEGER,
FOREIGN KEY (customer_id) REFERENCES customer(id),
FOREIGN KEY (address_id) REFERENCES address(id));
Duplicating columns is bad practice because it 1)defeats the purpose of using a relational model; 2)can lead to conflicting records if information is updated or deleted in one table, but not another.
Furthermore, you shouldn't have street_address_1, street_address_2, street_address_3 all in the same table. That's a violation of First Normal Form. Think of it this way, can a person have more than three addresses? Can they have two addresses in different cities? Do all three of those addresses have the same zip?

Duplicate key value violates unique constraint in psql

Key (owner_id)=(0) already exists.
Getting the above error relating to the owners table when I try to run both tables, although the first table is created fine on its own. Any guidance would be greatly appreciated.
DROP TABLE pet CASCADE;
DROP TABLE owner CASCADE;
CREATE TABLE pet(
pet_id SERIAL4 primary key,
pet_type varchar(255),
pet_name varchar(255),
pet_age INT2,
pet_weight INT4,
behaviour_score INT4
);
CREATE TABLE owners(
owner_id SERIAL4 primary key,
owner_name varchar(255),
owner_email varchar(255)
);

sqlite: Cannot select id while rowid works

I have a database schema like this
CREATE TABLE IF NOT EXISTS
rephotos (id integer AUTO_INCREMENT,
beforePath text,
beforeThumbnail blob,
afterPath text,
afterThumbnail blob,
PRIMARY KEY(id)
);
When trying to SELECT the ids in the database like this
SELECT id FROM rephotos;
it prints nothing. However if I use
SELECT rowid FROM rephotos;
it works as expected. The reason this confuses me is that the sqlite documentation specifically states that
If a table contains a column of type INTEGER PRIMARY KEY, then that
column becomes an alias for the ROWID. You can then access the ROWID
using any of four different names, the original three names described
above or the name given to the INTEGER PRIMARY KEY column. All these
names are aliases for one another and work equally well in any
context.
What am I doing wrong?
The documentation says:
A PRIMARY KEY column only becomes an integer primary key if the declared type name is exactly "INTEGER". Other integer type names like "INT" or "BIGINT" or "SHORT INTEGER" or "UNSIGNED INTEGER" causes the primary key column to behave as an ordinary table column with integer affinity and a unique index, not as an alias for the rowid.
AUTOINCREMENT is spelled wrong, so the column type is not exactly "INTEGER" but "INTEGER AUTO_INCREMENT".

Resources