Jump to content
Sign in to follow this  
CiVQ

SQLite primary key unique for multiple tables

Recommended Posts

CiVQ

Hello, dear experts, here is the question:

Say, there are two tables.

When inserting data into any of the tables, how can I enforce SQLite, that the resulting rowid (primary key) become unique for both tables?

Example:

tblFirst:

ID: 1

ID: 3

ID: 4

tblSecond:

ID: 2

ID: 5

 

So, I thought to ask the experts, to widen our common knowledge.

 

Thanks in advance.

BR.

CiV.


I know that my code is ugly. But it works. Mostly.

Share this post


Link to post
Share on other sites
kylomas

Hi CiVQ,

I'm hardly an expert but I'm not sure that this makes any sense.  Can you give an example of what you are trying to do?

kylomas


Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Share this post


Link to post
Share on other sites
CiVQ

Well.

This is merely a hypothetical question (yet), but I think it's not far-far away from mother Earth, so bear with me.

There are multiple types of vehicles, and there are owners of them.

I want to create a multi-multi relation between the (various types of) vehicles and their owners, because:

-there could be more owners of a vehicle

-an owner colud have multiple vehicles

-and an owner colud have multiple types of vehicles (cars, airplanes, etc.)

Creating only one table for various types of vehicles is not convenient, because the various vehicle types have different attributes.

Enforcing SQLite to maintain the rowids across multiple vehicles tables helps to prevent confusion, which specific vehicle is in question.

Here is a quick and dirty schema of my db:

tblOwners:

ID INTEGER PRIMARY KEY

Name NVARCHAR

...

tblCars:

ID INTEGER PRIMARY KEY

Manufacturer NVARCHAR

Type NVARCHAR

License NVARCHAR

...

tblAirplanes:

ID INTEGER PRIMARY KEY

Wingspan FLOAT

MaxAltitude INTEGER

...

tblOwners-Vehicles

OwnerID INTEGER

VehicleID INTEGER

 

BR.

CiV.


I know that my code is ugly. But it works. Mostly.

Share this post


Link to post
Share on other sites
kylomas

CiV,

First, thank you for the explanation.

Secondly, maintaining uniqueness of primary keys among multiple tables is NOT the solution to your problem.  Consider this:

tblVehicleType

VehicleType_ID INTEGER PRIMARY KEY

VehicleType_Name char

tblVehicleAttributes

VehicleAttributes_ID INTEGER PRIMARY KEY (make this a foriegn key to tblVehicleType.VehicleType_ID)

VehicleAttribute_#1....

VehicleAttribute_#2....

VehicleAttribute_#3....

etc...

 

This is how I might organize vehicle attributes.

 

You may receive better ("expert") help if you can give some sample data.

Good Luck,

kylomas

Edited by kylomas

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Share this post


Link to post
Share on other sites
jchd

CiVQ,

The skeleton you posted doesn't work as you have no way to determine whether a given VehicleId is a bike, a car, a truck, a plane, whatever. You need to store a VehicleTypeId as well. The drawback anyway is that you can't parametrize a schema name in SQL, so in many operations you'll have to first get the type then query the (bike, car, truck, plane, ...) table depending on type.

So the choice is: either you create a unique Vehicles table with all the possible columns for storing vehicle charateristics, either you create multiple tables but loose the ability to query the owner --> vehicle()s in single query (which is a serious inconvenience).

There is an intermediate solution: place characteristics common to all types in the vehicles table (planes, cars, boats, trucks all have a max speed, a registration ID, an insurance reference, a fuel type, etc.). Eventually allow some columns holding Null when there is no point to store anything for a characteristic (e.g. fuel for a soarer). Then put highly specific extra characteristics in separate tables and a reference to that in the common row.

This way you can still query the most basic characteristics for a given vehicle or for all the vehicles of a given owner, all with a single fast query. Only when you need all detail data for a specific vehicle you need to go thru a series of queries to grab them.

The final choice depends on you precise context and needs over the long term.


This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Share this post


Link to post
Share on other sites
CiVQ

Sorry for the late reply. I was involved in family business :)

@jchd: So, taking the common fields in one table, and the specific ones to different tables. Seems reasonable. :D

Thanks for the replies, I've learned from them.

 

By the way, I've run into another problem in SQLite land:

Is it possible at all to create a view involving multi-multi table relations?

Various sites claim that it can't be done.

The only workaround should be to programmatically check the fields and extract data.

(programmatically = involving lotsa SQL queries and evaluating them by the program)

Because I've seen this: http://probertson.com/articles/2009/11/30/multi-table-insert-one-statement-air-sqlite/

It makes a lot easier to insert data into multiple tables, but not in multi-multi related tables (connected via a "helper" table)?

BR.

CiV.


I know that my code is ugly. But it works. Mostly.

Share this post


Link to post
Share on other sites
jchd

Of course it's possible! Views are nothing else than select queries which are relaunched on the fly each time the view is accessed.

So all that is possible with select queries is possible with views.

After looking at your linked page I must say this isn't the way inserts are commonly done even if it is indeed possible to use triggers to break down columns destinated to different tables and inserting new rows there. You can complexify this scheme ad nauseam.

In practice and given that serious DBs often have dozens to hundreds of related tables it's in fact easier to perform the various insertions from application code, even if that means reading back the last inserted ID (for foreign tables).

One good reason for that is that the input you're processing doesn't always include all the data needed for insertion.

Let me take a relatively basic example: say you import new (or update old) customers' coordinates from, say, a .CSV. You have few tables: countries, addresses, clients.

If at least one customer resides in a country for which you dont have the relevant fields in the CSV (countryISOcode, zipRegExppattern, countryname, (currency, EU_VAT, EU_membership, ...) then you just can't fetch all these required information (which you typically get from the Net) from your SQL trigger to be able to insert the new country so that the address can refer to the country foreign key.

There are more good reasons why this way isn't used much but they are a bit too technical to dissect here.

In short:

select ... from tableA ....

if failure then insert ... into tableA

id= _sqlite_lastinsertrowid

...


This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Share this post


Link to post
Share on other sites
CiVQ

I got it. You directed me to the right track, thank you!


I know that my code is ugly. But it works. Mostly.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×