auto creat table row when other table add row - java

i have 2 table
1 persons(email,name)
2 location(email)
now when i add a person to persons table i want the db auto creat a row in location whit the person email (that i just add to persons).
i try to do it white PRIMARY KEY, and FOREIGN KEY but no succses.
thank you all.
this what i try :
CREATE TABLE Persons
(
Email char(50) PRIMARY KEY,
First_Name char(50))
CREATE TABLE location
(
email char(50),
FOREIGN KEY (email) REFERENCES Persons(email)
)
but when i add to persons person its not added to location too.

You would need a trigger to do this not a foreign key.
The FK just enforces that a row cannot be inserted in location without a corresponding record in Persons
But email is a very wide choice for a primary key as well as unstable (see is email address a bad primary key) and the whole design seems odd.
What is the location table for? Does this have a 1:many relationship with Persons? Where are the other columns? What is the PK of location?

Related

I want to display foreign keys in one table from other tables, but I get foreign key mismatch error

I'm very new to SQL and I want the contracts_tb (query details below) is to display and link the foreign id keys referred from:
med_idref (referred from med_id (INTEGER), PRIMARY KEY o mediaadv_tb),
mediatitle_ref (title (TEXT), mediaadv_tb),
mediatype_red (mtype (TEXT), mediaadv_tb),
cus_idref (cus_id (INTEGER),PRIMARY KEY of customer_tb),
cus_companyref (referred from company (TEXT), in customer_tb)
All to be linked and displayed to contracts_tb. When I add/replace values from mediaadv_tb and customer_tb, I get this problem:
foreignkey mismatch
Also, do I have to make or assign a parent table?
Query:
DROP TABLE IF EXISTS customer_tb;
CREATE TABLE IF NOT EXISTS customer_tb (
cus_id INTEGER PRIMARY KEY,
company TEXT,
firstname TEXT,
middlename TEXT,
lastname TEXT,
gender TEXT,
dob TEXT,
dateregistered TEXT,
contactno TEXT,
emailaddress TEXT,
description TEXT,
refpic INTEGER,
cuspic BLOB
);
DROP TABLE IF EXISTS mediaadv_tb;
CREATE TABLE IF NOT EXISTS mediaadv_tb (
med_id INTEGER PRIMARY KEY,
mtype TEXT,
duration TEXT,
title TEXT,
dateadded TEXT,
desription TEXT,
previewimg BLOB,
filepath TEXT
);
DROP TABLE IF EXISTS contracts_tb;
CREATE TABLE IF NOT EXISTS contracts_tb (
contract_id INTEGER PRIMARY KEY,
customer_idref INTEGER REFERENCES customer_tb (cus_id),
media_idref INTEGER REFERENCES mediaadv_tb (med_id),
media_typeref TEXT REFERENCES mediaadv_tb(mtype),
media_titleref TEXT REFERENCES mediaadv_tb (title),
status TEXT,
priority TEXT,
dateadded TEXT,
dateexpiration TEXT,
amountpaid REAL,
arearofcoverage TEXT
);
Error :-
contracts_tb
mediaadv_tb
I believe that your issue is because the foreign keys defined that reference the media_typeref and the media_titleref columns are invalid as they do not have, or are part of a, UNIQUE indexes (no indexes). SQLite Foreign Key Support - 3. Required and Suggested Database Indexes
The referenced id columns, as they are INTEGER PRIMARY KEY, are implicitly UNIQUE indexes.
Furthermore the two columns (typeref and titleref) themself aren't even needed as the media_idref column would be used to identify the reference and thus would hold the respective values. Copying those values into the contracts table would be contrary to normalisation and may even create major headaches (e.g. if a value changed you'd have to find all other uses and also change them).
As such I'd suggest that the contracts_tb be created using :-
DROP TABLE IF EXISTS contracts_tb;
CREATE TABLE IF NOT EXISTS contracts_tb (
contract_id INTEGER PRIMARY KEY,
customer_idref INTEGER REFERENCES customer_tb (cus_id),
media_idref INTEGER REFERENCES mediaadv_tb (med_id),
status TEXT,
priority TEXT,
dateadded TEXT,
dateexpiration TEXT,
amountpaid REAL,
arearofcoverage TEXT
);
Re comment :-
What i'm making is a Java NetBeans SQLite database program, where by
using the Contracts frame, whevenr one makes a new contract, there
will be a combobox that restricts the user to only put the existing
ids or names that is referred in the contracts_tb then provides the
choices. Is it possible sir?
Yes.
More specifically:-
Assume that you have customers Fred, Bert and Harry (id's 1,2 and 3 respectively). And that you have mediaadv's M1, M2 and M3 (id's 10,11 and 12 (not 1,2 and 3 to help distinguish between mediaadv and customers)).
Additionally I'll assume the suggested contracts_tb table as opposed to the original in the question (i.e. 2 columns dropped as suggested)
The when inserting a new contract, you present a list (combobox) of the customers e.g.
Fred
Bert
Harry
(this list could be generated from a query such as:-
SELECT cus_id,firstname FROM customer_tb; i.e. all existing customers)
If you wanted Fred James Bloggs then you could use :-
SELECT cus_id,firstname||' '||middlename||'lastname' AS fullname FROM customer_tb;.
Likewise a list of the existing mediaadv could be generated from a query such as:-
SELECT med_id, description FROM mediaadv_tb; e.g.
so the combobox would have:-
M1
M2
M3
Now if the contract were for Bert (id 2) and M1 (id 10) then you build SQL something like :-
INSERT INTO contracts_tb VALUES(null,2,10,'the_status','the_priority','yyyy-mm-dd','yyyy-mm-dd',500,'the_coverage');
1st value is null i.e. no value, so as contract_id is an alias of the rowid it will be generated.
2 is the id of the customer (hence why cus_id was in the query as you need the id as it's the value you are going to store)
10 is the id of the mediaadv (again hence why med_id was in the query as you need the id as it's the value you are going to store).
the other values are what they should be.
Note the above use of INSERT requires that all columns be given. You can skip columns by specifying a list of the columns e.g. INSERT INTO contracts_tb (customer_idref,media_idref) VALUES(2,10);
As a customer with an cus_id of 2 (Bert) exists then the constraint that customer_idref is an existing id in the customer_tb is good/met and there is no conflict.
Likewise as there is a row in mediaadv_tb that has an med_id of 10 this constraint is good/met and there is no conflict.
However say the SQL were :-
INSERT INTO contracts_tb VALUES(null,2,100,'the_status','the_priority','yyyy-mm-dd','yyyy-mm-dd',500,'the_coverage');
Then as there is no med_id of 100 the constraint saying that media_idref must reference a value of 100 (in this instance) in the mediaadv_tb, column med_id, then the constraint will not be met and the insert will fail.
So again Yes, I believe that what you want is feasible.
Note a foreign key is only a constraint it doesn't bind/associate columns or join tables.

H2 database: referring to a table in root schema from a foreign key constraint

Given a table in root schema:
CREATE TABLE user (
username VARCHAR(50),
password VARCHAR(50));
and a table in Quiz schema:
CREATE TABLE Quiz.Results (
username VARCHAR(50),
points INT,
FOREIGN KEY (username) REFERENCES user(username));
I'm unable to actually create the foreign key, because the database claims the table user does not actually exist. Neither can I subsequently add the foreign key:
ALTER TABLE QUIZ.RESULTS
ADD FOREIGN KEY (username) REFERENCES user (username)
Both tables are, of course, stored in the same database.
Since this is just a piece of homework, I'm more than happy to simply skip adding a foreign key. But I'm curious if this is indeed a limitation in H2, a bug, or if it works as intended.
Can I somehow refer to table user outside the quiz schema?
You would need to explicitly set the schema name if you refer to a table in a different schema. The default schema name for H2 is public. Example:
CREATE TABLE user (
username VARCHAR(50),
password VARCHAR(50));
create schema quiz;
CREATE TABLE Quiz.Results (
username VARCHAR(50),
points INT,
FOREIGN KEY (username)
REFERENCES public.user(username));
To create the foreign key constraint later, use:
ALTER TABLE QUIZ.RESULTS
ADD FOREIGN KEY (username)
REFERENCES public.user(username) ;
yes very much possible. You need to use corresponding Schema name for both tables.
suppose your defualt schema name is DefaultSchema then your query will be
ALTER TABLE QUIZ.RESULTS
ADD FOREIGN KEY (username) REFERENCES DefaultSchema.user (username)

SQLite database update

Problem Description
I have SQLite database in which I have keep two tables, In first Table Companies I keep company ID which is unique for that company, Name of company, Websites and Emails. Every company can have several addresses, so I create second Table Addresses and keep there company ID which is not unique in this case and other information.
For Example:
If in Companies table I have record like this
7785413 MyComp http://www.mycomp.com mycomp#email.com
In the Addresses table I can have records like this
7785413 0 Address1 +64841518549 +9985212848
7785413 1 Address2 +64841542359 +9985212848
As there is no unique columns in my second Table, I want to know how I can update records?
In first case I call
database.insertOrThrow(DATABASE_TABLE_NAME, null, values);
function and if my IDs are same function throws an exception I catch it and update record. I can't do same thing in second Table as there is no unique columns and function will not throw an exception. Which is the best way to do that ?
Tables
CREATE TABLE Companies (ID TEXT UNIQUE, Name TEXT, Websites TEXT, Emails TEXT)
CREATE TABLE Addresses (ID TEXT, Position NUMERIC, Address TEXT, Tel TEXT, Mob TEXT)
You need to specify the primary key as a constraint on table creation.
CREATE TABLE Addresses(ID TEXT, Position NUMERIC, Address TEXT, Tel TEXT, Mob TEXT, PRIMARY KEY (ID , Position ));

What is the primary key of an audit table managed by Hibernate Envers?

I am using Hibernate Envers to audit some entities. I manually created the associated audit tables. However, I am having trouble determining what an audit table's primary key should be. For example, consider a fictional table designed to store customers:
CREATE TABLE CUSTOMER
(
CUSTOMER_ID INTEGER,
CUSTOMER_NAME VARCHAR(100),
PRIMARY KEY (CUSTOMER_ID)
)
And you create the audit table:
CREATE TABLE CUSTOMER_REVISION
(
REVISION_ID INTEGER,
REVISION_TYPE_ID INTEGER,
CUSTOMER_ID INTEGER,
CUSTOMER_NAME VARCHAR(100),
PRIMARY KEY (???)
)
Here were the options I considered:
Primary key: REVISION_ID
This cannot be the primary key because multiple entities of the same class may be modified during the same revision.
Primary key: (REVISION_ID, CUSTOMER_ID)
This seems more likely, but I'm not sure if Envers will insert multiple records per customer per revision.
Primary key: (REVISION_ID, REVISION_TYPE_ID, CUSTOMER_ID)
This seems like overkill, but it may be possible that Envers will insert different types of records (add, modify or delete) per customer per revision.
Primary key: A new column
Perhaps the primary key must simply be another column containing a synthetic primary key.
What is the true primary key of an audit table managed by Hibernate Envers?
Judging by the examples in the documentation, it appears that the primary key in my example would be (REVISION_ID, CUSTOMER_ID). Here is the example in the documentation:
create table Address (
id integer generated by default as identity (start with 1),
flatNumber integer,
houseNumber integer,
streetName varchar(255),
primary key (id)
);
create table Address_AUD (
id integer not null,
REV integer not null,
flatNumber integer,
houseNumber integer,
streetName varchar(255),
REVTYPE tinyint,
***primary key (id, REV)***
);
The primary key of audit table is the combination of original id(id) and revision number(rev) of the audit table.
As the official documentation there can be at most one historic entry for a given entity instance at a given revision, which simply means unique combination of above two column.

Mysql. Query to insert data into a table related to another

I'm new in sql. I have doubt. Let's say that I have two tables. One is called user and the other order.
User
User_Id - name - email
Order
Order_id - product - User_id
What query should I use to insert a new order in the order table with the User_id field related to the User_id from the user table(an existing user)
Eg:
Order_id - product - User_id
1a - plate - 1
2a - car - 3
3a - bike - 1
If you have name or email of the user, you can try this. If USER has a composite primary key on user_id and name columns, it will work without any issues. Better to keep foreign key relationship on user_id column of ORDER also.
Example has NAME column, you can try with email column also.
INSERT INTO ORDER (ORDER_ID, PRODUCT, USER_ID)
VALUES (1a, 'plate', (SELECT USER_ID FROM USER WHERE NAME = 'Existing user'));
EDIT:
I have worked on Oracle sql only. After your comment, found this link.
http://dev.mysql.com/doc/refman/5.0/en/example-auto-increment.html
If you have defined AUTO_INCREMENT on order_id column, you don't need to pass value.
INSERT INTO ORDER (PRODUCT, USER_ID)
VALUES ('plate', (SELECT USER_ID FROM USER WHERE NAME = 'Existing user'));
you use the standard sql insert.
insert into Order (Order_id, product, User_id) values ("3a", "bike", 1)
If your tables are set up property, there will be a foreign key restriction on the User_id field, so if you try to insert an order but don't reference a valid person, mysql will throw an error.
INSERT order (Order_id, product, User_id) VALUES (1a,plate,1)
The foreign key has nothing to do with the sql used in the insert it should have been set up when the table was created or via an ALTER TABLE query after it was created.
Relationships between tables are defined when you create the table, using the FOREIGN KEY.
CREATE TABLE Users(
UserId int primary key
);
CREATE TABLE Orders(
OrderId int primary key,
UserId int FOREIGN KEY REFERENCES Users(UserId)
);
Then you do a simple insert like:
INSERT INTO ORDER(OrderId, UserId) VALUES(1, 5);
Take a look at the documentation for foreign key constraints here: http://dev.mysql.com/doc/refman/5.5/en/innodb-foreign-key-constraints.html.

Categories