In my tables for SQL, I have many tables each with an ID column I wish to keep incrementing, along with a main name table with an ID which will be the same value as the corresponding IDs in other tables for the relevant information. I wish to be able to make it so that, when I delete a row from the name table with a certain ID, all values across other tables with the same ID get deleted also. The issue appears to be that, if i create foreign key constraints from the name ID on the name table to the other IDs across other tables, it means these tables are no longer allowed to automatically increment.
Related
This ERD is part of my school work and something doesn't seem right. The ERD table 'Course' looks like its referencing 2 tables. Is the column titled 'Qual_Code' in table 'Course' from the 'Prerequisite' table, the 'Qualification' table or both? I don't think its both because you cannot have a single column with a foreign key that references two different tables.
Help because I have to write the SQL codes for this!
At the first glance, it appears that the column qual_code of the COURSE table is redundant. When writing the DDL code for this, you can include or exclude this column - see the following examples:
course table with qual_code column
course table without qual_code column
The PREREQUISITE table maps courses to qualifications. The PK constraint in this table will not allow NULLs in either of the columns ie if a row gets INSERTed, it must contain values for both course_num and qual_code.
I don't think its both because you cannot have a single column with a
foreign key that references two different tables.
It seems that you are misinterpreting this. The PK column of the QUALIFICATION table is referenced twice (FK columns reference a PK or UNIQUE column, not the other way round -> see the DDL code on DBfiddle).
I have multiple tables with a unique column name for each of their primary key such as: DeviceName, DeviceNumber, SwitchNumber, Etc.
There is another table which serves as an audit trail containing the changes from all tables, it lists the table name and the primary key value for each respective table as a reference i.e.
Table#2
TableName, InstanceNumber
I would like to use the information in table #2 to pull the respective records from each table in 'TableName' by referencing the 'InstanceNumber' attribute as the PK for each respective table without having to manually create a reference for each table's column name.
Is there a way I can do this? That is, create a query that references a 'general' column name to a table that points to the primary key column?
Select * from (TableName) where (PrimaryKeyColumn) = (InstanceNumber);
You can only do this using dynamic SQL -- in PL/SQL, that would be execute immediate.
Why not? Here is one reason. All the columns returned by a query need to be known when the query is compiled. That is, before any data is read. You are requesting a set of columns that depends on the table that is in the data. So, the columns are NOT known and the query cannot be compiled.
I created a new table called Expirations that has it's own unique id, with indentity incrementing, as it's primary key. I also want this table to pull in data from two other tables, so I created a column for the InsuranceId and the LicenseId, making both columns foreign keys to connect them to the data (aka ID) from their respective tables (Insurance and License).
Any idea why my data is not automatically filling in for my Expirations Table? I believe it was created as "many to one" for all of these columns. Not sure if that is correct either, as I want the Expiration table to list all insurance id's and licence id's.
Anyone know what I am doing wrong here?
Foreign keys don't mean that a table gets filled automatically.
Let's say you have a person table and a company table and a company_person table to show which person works in which company. Now you insert three companies and four persons. That doesn't mean that the company_person table gets filled with 3 x 4 = 12 records (i.e. all persons work in all companies). That would make no sense.
Foreign keys merely guarantee that you cannot insert invalid data (i.e. a person that doesn't exist or a company that doesn't exist) into the table in question. You must insert the records yourself.
Basically your expirations table is like a fact table and you want to load data from the dim table which is Insurancetable ( InsuranceId as primarykey) and Licencetable ( Licenceid as PK).
But if you do not have any combination of InsuranceId and licenseId how do you know which insuranceid belongs to which LicenseId.
If your expirations table is empty then you need to first do cross join between the insurancetable and licensetable which is cartesian result but you do not want to do that as it does not make sense in real world.
Hope my explanation helps.
I am designing a database with multiple tables like users, content, etc. I want to allot id to each row in the tables.
So what are the criteria to generate an unique id for each row in each table. And should I make the id in auto increment mode?
I have a SQL Server database where records are identified by GUIDs, meaning all rows have a primary key of type uniqueidentifier.
Example:
Table A
-- Table B
-- Table C
So, a record in Table A has a given GUID as a PK, and records in Tables B and C have columns which are FKs to the record in Table A. The records in Tables B and C cannot exist without the record in Table A.
Given a GUID and with no knowledge of any tables in which it appears as a PK or an FK, could I write a stored proc that simply vaporized everything related to that GUID? I basically want to say:
Hey SQL, I don't like this GUID anymore. Wipe it off the face of the Earth.
Here's my thinking:
Find any column in any table that is a nullable FK containing that GUID, and set the column value to NULL
Find any column in any table that is a non-nullable FK containing that GUID, and delete the row
Find any column in any table that is a PK containing that GUID, and delete the row
(In theory, I could also find any column of any type in any table containing that GUID and set the value to NULL. Since it's a GUID it can't really exist for any other reason than to (1) identify a row as a PK, or (2) reference that row as an FK. But why would a GUID appear in a column unless it was a PK or an FK?)
Is my thinking correct, here? I essentially want to purge all references to a logical object from a database. That object is identified by a perfectly unique value, therefore I just need to go all "scorched earth" on that value.
I can't be the first person to want to do this. Do I need to roll my own stored proc for it, or I am unaware of some existing functionality here?