ORA-00904: "CITYCODE": invalid identifier in CREATE TABLE - identifier

So, I'm not very good with sql and am trying to jsut great two tables. one called CITY with PK of Citycode, and Warehouse, which uses Citycode as an FK. And I'm not surprised if even this is wrong.
These are my two CREATE requests:
CREATE TABLE City
(
citycode number(2),
cityname varchar2(30),
Population number(7),
Primary Key (citycode)
);
CREATE TABLE Warehouse
(
whid number(2),
address varchar2(30),
capacity number(7),
capacity_used number(7),
mgrname varchar2(30),
mgrgender varchar2(1),
Primary Key (whid),
Foreign Key (citycode) REFERENCES City
);
Then when I run these in iSQL I get the error ORA-00904: "CITYCODE": invalid identifier.
All I can find about this error is with SELECT statements.
Thanks :)

You do need to create the column that references the other table. Like
CREATE TABLE City
(
citycode number(2),
cityname varchar2(30),
Population number(7),
constraint city_pk
PRIMARY KEY (citycode)
);
CREATE TABLE Warehouse
(
whid number(2),
address varchar2(30),
capacity number(7),
capacity_used number(7),
mgrname varchar2(30),
mgrgender varchar2(1),
citycode number(2), <-- note this one
constraint warehouse_pk
PRIMARY KEY (whid),
constraint warehouse_fk
FOREIGN KEY (citycode)
REFERENCES City (citycode)
);

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

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?

how to fetch records from two tables using foreign key

create table studentsDetail (
student_id integer primary key,
rollno text,
name text,
stream text,
school text
);
create table studentsInfo (
studentinfo_id integer primary key,
stdetail_info_id integer,
fathername text,
mothername text,
age text,
address text,
FOREIGN KEY(stdetail_info_id) REFERENCES studentsDetail(student_id)
);
I want to fetch record from both table using foreign key. How can I do it easily?
Something like this:
SELECT
studentsDetail.name, studentsDetail.school,
studentsInfo.age, studentsInfo.address
FROM
studentsDetail INNER JOIN studentsInfo
ON studentsDetail.student_id = studentsInfo.stdetail_info_id

Whats wrong with the foreign-key constraint in this table?

table creation failed because a foreign key constraint was not correctly formed
the first create table my_seeking, works fine, when I try to run the create contactstoseeking I get the following err msg
Error Code: 1005. Can't create table 'db_ferguson.contactstoseeking'
(errno: 150)
CREATE TABLE IF NOT EXISTS `my_seeking` (
`id` INT NOT NULL ,
`seeking` VARCHAR(45) NULL ,
PRIMARY KEY (`id`) )
ENGINE = InnoDB
;
CREATE TABLE IF NOT EXISTS `contactsTOseeking` (
`id` INT NOT NULL ,
`seek` VARCHAR(45) NOT NULL ,
UNIQUE INDEX `id_UNIQUE` (`id` ASC) ,
UNIQUE INDEX `seek_UNIQUE` (`seek` ASC) ,
CONSTRAINT `fk_contactsTOseeking_my_seeking1`
FOREIGN KEY (`id` )
REFERENCES `mydb`.`my_seeking` (`id` )
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
A search of the web shows that err 150 is tied to my fk constraint.
Cannot create table. If the error message refers to error 150, table creation failed because a foreign key constraint was not correctly formed. If the error message refers to error –1, table creation probably failed because the table includes a column name that matched the name of an internal InnoDB table."
this code was generated BY MySQL, and this is my first attempt at foreign keys. I tried coding it manually and had issues so I thought that I would let workbench do it. WHAT EVER the problem is I cant see it or it is beyond my current skill set
Did you try removing the 'mydb' text in the reference? It indicates a table in 'mydb' database.

Grails / Gorm : Difference between declaring object and describing relationship?

I'm having trouble understanding the difference between declaring a domain-object in another domain and specifying the relationship between the domains.
Sample code:
class User {
Book book
}
versus
class User {
static hasOne = Book
}
class Book {
String name
}
The hasOne relationship will put the key on the child object, so in the db you'll find book.user_id with hasOne rather than user.book_id if you just declare Book book on User. You'll see the difference in the DDL generated if you use grails schema-export.
Here's the DDL with hasOne in place:
create table book (id bigint generated by default as identity (start with 1), version bigint not null, user_id bigint not null, primary key (id), unique (user_id));
create table user (id bigint generated by default as identity (start with 1), version bigint not null, primary key (id));
alter table book add constraint FK2E3AE98896CD4A foreign key (user_id) references user;
Here's the DDL with just Book book on User:
create table book (id bigint generated by default as identity (start with 1), version bigint not null, primary key (id));
create table user (id bigint generated by default as identity (start with 1), version bigint not null, book_id bigint not null, primary key (id));
alter table user add constraint FK36EBCB952E108A foreign key (book_id) references book;
Notice that the book table has the reference in the first example and the user has it in the 2nd.
Long answer: I strongly recommend watching Burt Beckwith's presentation on GORM/collections/mapping. Lots of great info around GORM and the consequences of various advantages/problems with describing relationships with hasMany/belongsTo, etc.
The main difference is that when using hasOne the foreign key reference is stored in the child table instead of the parent table, i.e. a user_id column would be stored in the book table instead of a book_id column being stored in the user table. If you didn't use hasOne, then a book_id column would be generated in the user table.
There is an explanation and example in the Grails documentation for hasOne.
Hope this helps.

Resources