|
Does anyone have any recommended reading regarding database design best practices? I'm beginning the foundation of a new project I'm working on and I have complete freedom in how I design the database for this application. I've designed databases before but generally just built things with lots of surrogate keys, no foreign constraints, etc. I want to build this database with best practices in mind. For example, I don't know whether to use natural/business keys vs surrogate keys, simple keys vs compound/composite keys (identifying vs non-identifying relationships), etc. kiwid fucked around with this message at 20:30 on Oct 30, 2014 |
# ¿ Oct 30, 2014 19:31 |
|
|
# ¿ May 6, 2024 06:54 |
|
I have an application where there are several admins managing the administrative tables. I want to start tracking what admins made what changes at what time. What is the best way to do this? I take it I could store everything in one table? user_id, current_timestamp, etc. But how do I specify what change is taking place? Anyone have any examples? Another way I was thinking would be to use soft deletes on all the tables I want to track, but that would mean putting these columns on every table (deleted_at, deleted_by, updated_at, updated_by) and I wouldn't really be able to tell what the changes were. kiwid fucked around with this message at 19:00 on Nov 5, 2014 |
# ¿ Nov 5, 2014 18:57 |
|
Awesome, I'll check out triggers. Thanks.
|
# ¿ Nov 6, 2014 02:14 |
|
If you have to store a test result, and this test result could be either a few word sentence (varchar), or a count (integer), or a percentage (decimal?), but never be all three, only 1 of the three. How would you store it?
|
# ¿ Nov 6, 2014 22:54 |
|
PhantomOfTheCopier posted:Which database? MySQL edit: Here is how I'm currently doing it. Is this a good way to do it? code:
code:
code:
kiwid fucked around with this message at 03:28 on Nov 7, 2014 |
# ¿ Nov 7, 2014 01:44 |
|
More questions! Looking at these two schema's, which one is the proper way to build the relationships? Each category can have many commodities and each commodity can be in many categories. Each facility can have multiple categories and commodities but does not have access to all categories or all commodities. Each category can have many facilities. Each commodity can have many facilities. An example of a category is "Food" or "Seed". An example of a commodity is "Corn", "Soybeans", or "Wheat". Soybeans can both be a food or a seed for example, hence the category_has_commodity table. Some facilities can only process seed, not food, but will still have access to the soybeans commodity. Some facilities can process food and seed, and multiple commodities. kiwid fucked around with this message at 22:25 on Nov 7, 2014 |
# ¿ Nov 7, 2014 22:17 |
|
Some facilities have access to some categories (maybe all but never none), and some commodities (maybe all but never none). If a facility has access to a category, they do not necessarily have access to all commodities in that category, and vice versa, if a facility has access to a commodity, they may not have access to all categories that that commodity belongs to. Does that mean I should be using the second schema?
|
# ¿ Nov 7, 2014 22:54 |
|
Like this? I didn't even realize you could do that, I thought you had to join two tables to one many to many. Didn't realize you can do a many-to-many for three tables.
|
# ¿ Nov 8, 2014 01:16 |
|
PhantomOfTheCopier posted:I don't know that I can answer without sounding utterly biased. First, I knew you were going to say MySQL... probably based on the wording in the original question. In all honesty, you might as well just make a single varchar(random number) like every other MySQL user and call it good, because everything is likely to be handled, ultimately, in the application, so why bother with all that noise in the database if it doesn't gain you anything. Definitely isn't a home work question This is more of a personal learning project that happened to get put into Protoduction. The application is actually quite large and it's going to be expanded really soon. What' I've shown in this thread is really only a small fraction of the app and database. What I'm doing now is basically refactoring the application since I've learnt better programming practices and I'm trying to learn better database design so that in a couple months, when the planned expansion to this application starts being defined, I have a better base to work off of. I think your third approach sounds interesting and I would be searching for values that are out of the min and max specifications so thanks for giving me these options to look into. Curious though, why do you have a bias against MySQL? I assume you're a MSSQL guy? I really don't know much about other database platforms, is there a reason MySQL shouldn't be used? kiwid fucked around with this message at 17:35 on Nov 9, 2014 |
# ¿ Nov 9, 2014 17:33 |
|
Hmm, I never realized that. Why is it the default in a LAMP/LEMP stack and why is it so popular?
|
# ¿ Nov 10, 2014 21:48 |
|
We do have MSSQL in the office but it's 2005. This place tends to severely lag behind in purchasing software and licensing. So it looks like I'll start looking into PostgreSQL.
|
# ¿ Nov 11, 2014 14:18 |
|
I'm creating a internal webapp that tracks a couple different types of service requests that users fill out. The difference between the 3 different types of service requests is that they all share a lot of duplicate fields and then have a some fields that are specific to that type. For example: request type 1 fields customer name customer code customer address ... a bunch of other customer fields sample type sampled date sample count request type 2 fields customer name customer code customer address ... a bunch of other customer fields flight date pilot flight reason request type 3 fields customer name customer code customer address ... a bunch of other customer fields allocated personal items purchased total Currently, I've created separate tables for each service request type but now I'm thinking I should just track everything in one table and have a one-to-one relationship with 3 other tables that each contain the additional fields that are not duplicated. Is this a better way to do this?
|
# ¿ Mar 3, 2015 19:53 |
|
Jethro posted:One service request table with three supplemental tables is better than three service request tables, but if it's just a handful of extra fields, then you could also just have one table with all the fields and then just leave them empty for the service requests that don't need them. Thanks. Yeah the example I used I left out a lot of details but there can be between 10-15 additional fields so I'll just go with the 1 main table and 3 additional tables.
|
# ¿ Mar 4, 2015 22:49 |
|
Does MySQL allow a unique index between several keys that still allows NULL? For example: Columns: user_id NOT NULL account_id NOT NULL type_id NULL unique('user_id', 'account_id', 'type_id') This would enforce that there can only be one user with an account where type = whatever However, since type can be NULL, there still needs to allow for a user to own multiple accounts where type is NULL. Is this possible? Edit: Oh nevermind, apparently this is baked in already. I was getting confused because I tried looking it up before implementing it and found this bug reported in 2005 and had no idea what happened. kiwid fucked around with this message at 20:58 on Oct 28, 2015 |
# ¿ Oct 28, 2015 20:50 |
|
Database design question. I have two entities, a users table, and an accounts table. A user can "own" many accounts and an account can belong to many "owners". A user can "manage" many accounts but an account can only have one "manager" which is a user. A user can be the "sales lead" for many accounts but an account can only have one "sales lead" which is a user. A user can be the "consultant" for many accounts but an account can only have one "consultant" which is a user. One way to design this would be a manager_id, sales_id, consultant_id field on the account table and then a many-to-many table with users for "owners". Another way would be just one many-to-many table with users and accounts and then an (enum) "type" field on the pivot table. How would you guys design this?
|
# ¿ Nov 24, 2015 21:09 |
|
Is there a way to add a "dynamic" column to a SELECT query based on another column? For example, I have a simple table with a field called TicketNumber and a bunch of other fields that don't matter for the example. The TicketNumber field has data like this: L32342 M3232 N2132 L2132 A3232 L33265 The first letter of the ticket represents the branch it belongs to. No other fields in the table relate the branch, there is no branch_id or anything else. I want to build a query that selects the Branch name and some other fields. Can I dynamically select a Branch field that looks at the first letter of the ticket number and sort of does a lookup in a key-value array or something? I can do this in the software but I'd rather do it in the query. edit: It's MySQL. edit2: There is no branches table to join with which is why I'm resorting to this. kiwid fucked around with this message at 16:15 on Jul 17, 2017 |
# ¿ Jul 17, 2017 16:05 |
|
Thanks, I ended up just saying gently caress it and created a branch table. Overkill for this application but oh well.
|
# ¿ Jul 18, 2017 00:58 |
|
With MySQL, is it possible to group by just certain groups and have everything else grouped as "Other"? Example: I just want group 1 and 2 on their own and everything else grouped together. Table: group | quantity -------------------- group1 | 5 group1 | 2 group3 | 4 group2 | 1 group1 | 6 group3 | 8 group4 | 5 group5 | 1 I want a result set like this where I sum the quantities: group1 | 13 group2 | 1 other | 18 Possible with one query?
|
# ¿ Oct 23, 2017 19:24 |
|
Is there a way to do like a conditional group by (MySQL/MariaDB) on only certain values then group everything else into one row? For example, I have a table of data like this: code:
code:
Unrelated------------------------------------------ What gui tool is this?
|
# ¿ Feb 15, 2018 21:53 |
|
kumba posted:I know in MSSQL you can group by a case statement, can you do that in MYSQL? That works perfect, thanks! Next question, I'm also using a group by with rollup. Is there a way to choose what name is on that rollup row? Here is my actual query: code:
|
# ¿ Feb 15, 2018 22:17 |
|
kumba posted:Try this: hmm, that didn't seem to affect anything. That's ok though, I've worked around the problem. Thanks. Unrelated question, I've been mulling the idea of switching from MySQL to Postgres. What is a good replacement GUI application for SQLyog?
|
# ¿ Feb 16, 2018 19:25 |
|
I keep being told I should stop using MySQL and use a "real" database. What do people mean by this? I always ask for more clarification and then conversation stops.
|
# ¿ Jul 17, 2018 18:15 |
|
pangstrom posted:They're probably either telling you to use PostgreSQL or a noSQL and you will know for sure which if you ask them which they use. I believe they're referring to MSSQL, Oracle, and Postgres. MSSQL and Oracle are two I'll likely never be interested in because I work primarily in PHP and Python. Should I however invest time into learning Postgres over MySQL? kiwid fucked around with this message at 18:54 on Jul 17, 2018 |
# ¿ Jul 17, 2018 18:42 |
|
Ruggan posted:Doesn’t MSSQL integrate just fine with PHP and Python? I think there are drivers for both. Yeah but then I'd have to run a Windows Server.
|
# ¿ Jul 18, 2018 00:15 |
|
karms posted:Kinda poo poo that he did not put a date on it, but oh well. 2013 https://github.com/grimoire-ca/bliki/blob/master/wiki/mysql/choose-something-else.md
|
# ¿ Jul 23, 2018 21:41 |
|
What is the best GUIs for postgres out there?
|
# ¿ Jul 25, 2018 14:42 |
|
Shy posted:DataGrip is nice. I've been eyeing this for a while. Guess I'll check it out.
|
# ¿ Jul 25, 2018 14:53 |
|
There must be something easy I'm missing about grouping data with non-aggregate columns. My data looks like this: code:
code:
|
# ¿ Nov 18, 2020 21:16 |
|
min and max are the perfect solutions for my problem, I knew it'd be something simple. Much better than what I was doing which was select distinct employee_number, first, last then joining via a subquery in the from.
|
# ¿ Nov 18, 2020 21:53 |
|
Is there any decent courses out there under $100 that teach the ins and outs of SQL Server Management Studio?
|
# ¿ Dec 15, 2020 04:09 |
|
Been a while since I've had to write any SQL since being spoiled with an ORM. I need to join a specific related row in a one-to-many relationship. For example: orders code:
code:
code:
|
# ¿ May 25, 2021 14:37 |
|
TheFluff posted:If I'm understanding you correctly you can just use a left join and put the filter on instructions.type in the join clause instead of in the where clause, like so: Ah yes, this is exactly what I was looking for. Thanks.
|
# ¿ May 25, 2021 16:02 |
|
kiwid posted:Been a while since I've had to write any SQL since being spoiled with an ORM. So this query has evolved a bit and now I need a result that looks like this: code:
kiwid fucked around with this message at 18:43 on Jun 1, 2021 |
# ¿ Jun 1, 2021 18:41 |
|
Sorry I should have mentioned that yes I'm using T-SQL. I will investigate the pivot option. I definitely can't group because there are a lot of non-aggregated columns. Thanks. edit: This is the current mess of a query that is joining the table multiple times: SQL code:
kiwid fucked around with this message at 01:51 on Jun 2, 2021 |
# ¿ Jun 2, 2021 01:46 |
|
Heavy_D posted:In the pivot approach, if you add something like: Hey thanks, PIVOT ended up being exactly what I needed. It took me a little to figure out the syntax but I've modified the query and it's working.
|
# ¿ Jun 4, 2021 05:24 |
|
Alright, so I've finally launched my application into production which is simply a web dashboard with a read only connection to our ERP software's database for reporting purposes. Unfortunately I'm seeing deadlocks now which didn't occur during development and so my select queries being cancelled. I've never experienced deadlocking before so I'm not sure how to come up with a solution here. It appears that when the ERP software is making update transactions, it'll lock the rows and won't allow me to issue a select statement. The reporting needs to be as "real-time" as possible and so an ETL process into a data warehouse wasn't the solution I determined a while back, at least in this instance. Here is the error I'm seeing: quote:[2021-06-07 15:44:27] production.ERROR: SQLSTATE[40001]: [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Transaction (Process ID 75) was deadlocked on lock resources with another process and has been chosen as the deadlock victim. Rerun the transaction. I started Googling and found this "NOLOCK" hint (https://www.mssqltips.com/sqlservertip/2470/understanding-the-sql-server-nolock-hint/). Just wondering if any of you have any feedback on using something like this? I understand the possibility of getting a "dirty read", but I believe that's acceptable considering the dashboard is polling an updated query every 60 seconds and would correct itself.
|
# ¿ Jun 7, 2021 18:05 |
|
Nth Doctor posted:Our app is built around accepting dirty reads as okay, and squeezing every ounce of performance out of the database server. For years, using (NOLOCK) was a near-mandatory standard. Several years back, we discovered SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED aka STILRU which is basically batch level NOLOCK and is the norm now. Thanks for this information. I'll implement NOLOCK as a quick fix then and I'll start to investigate the SNAPSHOT ISOLATION. I'll also look into the profiling. This database is 65GB so I'm sure there are some performance issues there but I'm not sure if it's server related or just lovely ERP software related. As a side note, is 65GB considered big these days?
|
# ¿ Jun 7, 2021 19:10 |
|
I'm building a view only dashboard that has related meta data on top of our main production database. I've created a separate database for the application with read/write, and I'm pulling in data from our production database using a view and a read only connection. I was considering adding foreign keys in my app's database relating the meta data to the data in the view but I started wondering if this would affect the production database in any way. Like it wouldn't prevent a delete or something on the production database would it?
|
# ¿ Nov 1, 2021 16:14 |
|
Does anyone know why when restoring a database via SSMS to a different database name, sometimes (seems to be based on the individual database) it doesn't auto-rename the files to restore as? For example, one of my databases when restoring I simply change the destination database name and the file names are automatically changed as well: However, with a different database this doesn't happen, I have to manually rename the files when restoring to a different destination database name:
|
# ¿ Apr 19, 2022 15:14 |
|
|
# ¿ May 6, 2024 06:54 |
|
Anyone know why the backups for one of my databases has suddenly shrunk by a lot? This is SQL Server and the option for auto shrink is turned off. I don't see any indication that the data has been affected in any way. Everything is still there.
|
# ¿ Apr 28, 2022 16:04 |