Sqlite - automatic deletion from multiple tables - sqlite

I suppose it's already answered, but I tried to formulate my search phrases in various ways and failed to find an answer.
I have an SQLite database, where one of the tables is named Student. Suppose that one of the students, let's say Peter, also appears in multiple other tables "relations", like "likes to eat ..", "has courses ...", "lives in ..". Please note that those are separate tables with their own columns. So, let's say, Peter appears in 100 tables.
Now, I want to delete student Peter. I delete him from Student table, but his name in the other 99 tables is still hanging, whereas I want it to disappear from those tables as well.
The question: is there a way in SQLite to do such a deletion automatically? In a described case, do I have to manually delete Peter from all 100 tables "by hand", or can SQLite track those deletes and automatically delete Peter from the other 99 tables if he is deleted from Students? Maybe via a "foreign key" or something like this..
Another way of formulating the question: I added Peter to Students, now is it possible to add Peter to another tables in such a way that if he's deleted from Student he will be deleted from the other tables as well?

You need foreign key with on delete cascade action.
A qiuck lookup for sqlite doc i've found this:
https://sqlite.org/foreignkeys.html
see section 4.3

Related

Creating SQite-tables while running a program

I have seen several members on this forum warn about creating tables in a database while the app is running. Instead it is encouraged to create the tables while programming, and only fill the tables with data during runtime.
E.g while creating a note-app it would be convenient to let the user specify a name for a single note, and let this note be created as a table in a database. This by creating the table at the time the user creates the note, and letting the name of the note be the name of the table. Why would this be a bad practise? Have I misunderstood?
It would be highly inconvenient for both you and the user of such an app to create a table for every note the user might want to add. It's just not the way it works. A table should contain rows of information of the same type, such as a note for example, and each note should be added as a row/record in the said table. The table should be called notes for example, and if you want a name for each note, it can be a column in the notes table called name.
An analogy would be, if you are taking notes manually (without an electronic device that is), would you have one notebook with you and just add notes on different pages as you need to, or would you carry around a bag full of notebooks so that whenever you want to add a new note, you would add each note in a separate notebook?
The notebooks being equivalent to database tables in this analogy, and the pages of the said notebook being equivalent to rows in a database table.
I can't think of a reason for creating tables during runtime really. The database structure should be "set in stone" so to speak, and during runtime you should only manipulate the data in the database, which is adding, deleting, or updating rows/records in already existing tables. Creating tables during runtime is a big no-no.
I probably don't understand your question correctly, but it seems to me, that what you really want to do is to create a new row in a table I the database?

Moving data in Access 2010

I'm a bit inexperienced but have a managed to learn how to use my database (access2010) but now I need to remove old files. In the database I have a primary table with multiple tables which stores additional information such as my notes.
I can't seem to figure out how to remove old files based on an input date.
I want to remove all files and the data stored in the dependent tables completely from year 2011 and back after backing up the database.
I've tried a delete query, and I've tried to simply copy and past inside the tables. I know there has to be a way to do this without deleting individual files.
When I run a delete query I get invalid key errors and when I delete files from my primary table, I get errors indicating there are associated data stored in the other tables.
Since I can't seem to delete all data across all tables for a certain date range, can anyone point out what I might be doing wrong?
You will need to JOIN the additional tables to your "primary" table, include a WHERE clause to only delete those matching your date range.
For an example, see this answer https://dba.stackexchange.com/a/102738

You cannot add or change a record because a related record is required in table

I'm fairly new to Access.
I have a DB table that needs to be normalized. I have some information about a person. These people are authorized to grant access to areas at our work site. Every person may be authorized several times to manage different areas, and of course different people can be authorized to manage different areas. My first try at it was to include the authorization and the areas together, but I realized that I was really repeating the data that way. After doing some study I decided that the best way to do this was to create 4 tables
tblPerson, tblPermission, tblArea, tblArea_Permission
The tblArea_Permission is a join table for the many-to-many relationship between tblPermission and tblArea (this is something that I just learned about). I seemingly set up the table relationships OK on the relationship tab. I also use a query for adding the records to the join table. When I try to do this, with a query that is getting the records from the tables, I get "You cannot add or change a record because a related record is required in table XXX." This would seem to be impossible.
I decided that I could probably live with the DB not enforcing referential integrity and took that away and used a combined primary key for the two records because every person with permission will control an area in only one combination. That seemed to work, but then I noticed that the records would randomly change. I decided that the DB must be corrupt. Parts of the DB seem to be working correctly, so I started with a new database and imported the tables and one form, then started to rebuild the new tables as described above. I got the same error.
Any help would be greatly appreciated. I've read through some different books, and used google, but nothing addresses this.
If a person is authorised to manage an area, you need a persons_area table:
PersonID ) Primary key
AreaID )
Which shows which areas the person can manage. I am not sure where the permissions table is coming from.
You will then not be able to add a record to person_areas table unless you have an ID in the area table and an ID in the persons table. If either of these IDs are missing, you will get the error above.
If you want more relevant comments on your DB design, you will need to post schemas.

SQLite Table with Multi-valued attributes

I would like to create a table that can store, say, the Title or Name of something in the first column, and then have associated people or objects in the next column. The problem is that there may be multiple people associated with the same Title or Name. If that first column is my primary key, I can't have duplicates for each row.
Name1 | Jim
| John
| Jill
Name2 | Mike
| Mary
Name3 | Jeff
Does this need to be done with intermediary tables, and if so, I'm fuzzy on how to actually code them (in sqlite). Do I just create them with foreign keys referencing the appropriate attribute in the main table? Any help would be appreciated.
Yes, in SQLite (or any relational database) you would model this by creating separate tables. It is a capital offense in the relational model to ever store two pieces of information in one column.
It's difficult to give a more precise answer to your question because you don't include any specifics, but most likely you will need one table to store the "things" you're interested in, one table to store information about people (one row per person), and a third "linking" table to associate people with "things".
This third table contains only the columns that make up the primary key of the "things" table and the columns that make up the primary key of the people table. The primary key of this table is made up of all the columns (probably two columns if "things" and people are each identified by a single column in the source table), and contains two foreign keys, one back to "things" and one to people.
One row is added to associate a person with a thing, but additional rows can be added to associate more people with the specified thing and more things with each person.
Another option is to use a DBMS specifically designed to support multiple values in a single field. There are several to choose from including at least one open source flavor. Look at d3, rocket software (U2 DBMS), Ladybridge (open qm) and search Google for multivalued DBMS for more.
This is well supported by MySQL 8.0.17+ by storing your values in a JSON array and adding a multi-valued index.

SQL Server Table Structure, StartDate and EndDate

I have a Tariffs table for international dialing Codes
with StartDate and EndDate
I'm using ASP.net Application to import excel offers to this table , Each offer contain about 10000 row, so it is a large table (about 3 millions row)
what is the faster scenario in SQL Server 2008 to create a stored-procedure or trigger to change the previous endDate for same tariff same prefix same destination and new rate on insert a new row,
and how to undo saving offer of 10000 rows and get back the table and update records to the previous state
Thank you,
The information in your question seems a bit jumbled, partially because of the ideas within it but also unhelpful grammer/whitespace (sorry to be so blunt but these things are helpful) but I'll try my best to answer.
In general, assume that a trigger is slower than a stored proc. They also add a higher level of complexity than many other things, like procs, so always be sure you really need one before using one.
But, I don't understand why you'd need a trigger if you're only inserting into one table. Triggers are usually used to implement a complex chain of logic. If it's a straight insert or update then keep simple and use a proc.
If it's just an insert, then the quickest way of all is a bulk insert.
Since you want to keep the previous state, my advice would be to create an archive/audit table (basically a duplicate, with possibly some extra fields like WhenInserted etc), on insert move (i.e. insert in the new table and then delete from the original) the existing rows into the archive and then you can do a bulk insert for the new rows.
But you use the word "change", so it's difficult to know what you really want. Hope that helps.

Resources