|
TagUrIt posted:I'm toying around with writing something to visually represent a lot of data, and I figure a database might be a good way to do the actual storage of the data. Having never working with databases before, though, I have a few questions: It sounds like you understand the entities and relationships and rules that you are modeling, and are trying to figure out the design of the tables and keys. Is that correct? By "multiple FKs in the same field" or "one [column] having an arbitrary number of FKs in it", do you mean columns that can have foreign key relationships to more than one table? It's certainly possible to do that, but I'm not sure what would be modeled by that. But in your example of "multiple FKs in the same field" of having 2 co-valedictorians -- I don't think that would be multiple foreign keys on one column referencing one column in two other tables. If one of the rules of your data model, and I'm just making this up, is that schools may have one, or possibly two, valedictorians each year, then you may be better off representing it as code:
code:
quote:2. Is there a way to make a "Last Updated" field for every row that updates anytime something in that row is changed? Rows get updated individually and periodically; it'd be nice if I could find which entries are potentially the most outdated. Well, the simplest way is to add a last_updated_date column to your table and update it with INSERT and UPDATE triggers. quote:3. Do you guys have any preferred gui editors/admin things? I was poking around with OpenOffice Database, but I couldn't figure out how to do FKs, which is probably due to my lack of sleep. Really I would recommend doing your DDL by writing SQL, and not by using some fancy-pants citified gui. Foreign keys are simple to write. edit: Just to carry this on a bit more, the example that I posted for #1 is probably not quite right, because it does not enforce that valedictorians of a school have to attend that school. For simplicity let's assume that students are enrolled in one school per year only. Then it might look like this: code:
Bad Titty Puker fucked around with this message at 11:07 on Mar 29, 2009 |
# ? Mar 29, 2009 10:47 |
|
|
# ? May 16, 2024 05:31 |
|
I had thought, well, the "valedictorian" field can have as many FKs to the Student table as it needs, but I like the idea of having that data as a table of its own. And since I can set a tight upper bound on the number of valedictorians, I can simply make X fields in the Val's table for FKs to the student table, with all but 1 of them being nullable. Thanks for the help!
|
# ? Mar 29, 2009 16:54 |
|
I'm a bit new when it comes to foreign keys, and when trying to make queries to extract data from related tables I'm a bit lost. What I have right now is 3 tables: division, employees, and intersection. Division corresponds to academic department, employee is employee, and intersection is a table that contains information from two of the other tables, as well as additional data.code:
If I were to query the division number 2, how would I be able to output all information for all employees in that department? Some divisions have up to 20 employees, it's not unique for each employee. As far as I can gather, it should be something like select * from DIV, EMP, INT where DIVNUM = X but that doesn't seem right, and describe does not state whether or not any of these are foreign keys. Also, how would I go about putting in an employee number and determining in which divisions they are enrolled, also while outputting all relevant information? Some have multiple divisions, so I'd imagine it'd be something like select * from DIV, EMP, INT where EMPNUM = X but again, due to lack of knowledge about the foreign keys, I'm uncertain if this is correct (or if it would be in any context).
|
# ? Mar 30, 2009 18:07 |
|
You need to learn about Joins.code:
|
# ? Mar 30, 2009 18:31 |
|
FeloniousDrunk posted:Looking to reduce my app to a single query, but I have one difficulty. Say I have a table "users": Sorry for my potential retardedness, but couldn't you do something like this: SELECT users.user, users.ID FROM users WHERE users.id IN (SELECT id FROM status GROUP BY id HAVING MAX(status) = 1) or SELECT users.user, users.ID FROM users INNER JOIN (SELECT id FROM status GROUP BY id HAVING MAX(status) = 1) as Results ON users.id = results.id
|
# ? Mar 30, 2009 21:52 |
|
Sorry for the late response, but how would the MINUS keyword perform for FeloniusDrunk's query?code:
|
# ? Mar 30, 2009 22:50 |
|
I'm developing drop-down filters for a company's internal video library. The two variables to filter by are $client and $cat. They have tables as follows (simplified):code:
Say someone chooses 'client2' from the dropdown, and 'cat3' from the category dropdown (so $client=2 and $cat=6). What kind of query do I need to run that is going to retrieve rows from the clips table, where clips are in category 6 (matched from clips2cat) and belong to clientid 2? I've been trying to use Unions but I'm getting lost. Sorry if this doesn't make any sense at all.
|
# ? Apr 2, 2009 12:21 |
|
Your base concept is clips. Select from clips join clients on client id, join clips2cat on clip id, where clip id is 6 and client id is 2. The feature of SQL you want to be looking up is "joins", not unions.
|
# ? Apr 2, 2009 12:43 |
|
Triple Tech posted:Great, I get it now. Here's what I ended up with: code:
|
# ? Apr 2, 2009 14:15 |
|
FYI, in production code, you never want to select star and interpolate variables like that. Explicitly select the columns you want (for both speed and future proofing) and learn how to use bind variables for injecting values into where clauses.
|
# ? Apr 2, 2009 14:21 |
|
EndOfRadio posted:Great, I get it now. Here's what I ended up with: Close, but your FROM clause seems to have leaked into your WHERE. code:
|
# ? Apr 2, 2009 15:56 |
|
roadhead posted:Close, but your FROM clause seems to have leaked into your WHERE. aren't they 2 ways of doing the same thing? The 2nd way is preferable, and follows later SQL syntax changes. But the first is still perfectly valid (although hard to follow, a bitch to trouble shoot, and difficult to change?)
|
# ? Apr 2, 2009 16:08 |
|
Roundboy posted:aren't they 2 ways of doing the same thing? The 2nd way is preferable, and follows later SQL syntax changes. But the first is still perfectly valid (although hard to follow, a bitch to trouble shoot, and difficult to change?) While valid, the older way precludes you from doing outer joins. It is important to understand, but it is equally important to never use it.
|
# ? Apr 2, 2009 16:40 |
|
Jethro posted:You need to learn about Joins. Thank you! I'm just getting familiar with the types of joins, so this looks very close to what I need. This is probably because I am using embedded SQL, but my output looks like DIVNUM EMPNUM DIVLOCATION DIVPHONE EMPNUM EMPNAME EMPRANK EMPYEARSTART when it should be DIVNUM DIVNAME DIVLOCATION DIVPHONE EMPNUM EMPNAME EMPRANK EMPYEARSTART Is this because of the join statement, or the way the rest of my program is structured (C++)? Also, I'm having a few other problems with this program - I'm not quite sure how to bind a variable so it'll be usable. I'm trying to let the user input a number to query, but any time I put the variable name into the select statement, it refuses it (I've tried numReq, :numReq, numReq.arr, :numReq.arr but none work). Here's all the relevant code. It's really lovely but I'd rather get the basics working before tightening it up further because it's driving me nuts. code:
code:
quote:The query is: select * from INT inner join EMP on INT.IEMPNUM = EMP.EMPNUM inner join DIV on INT.IDIVNUM = DIV.DIVNUM where DIV.DIVNUM = :numReq I've spent hours Googling binding variables and that specific error message and I've found no clear explanations or examples.
|
# ? Apr 2, 2009 17:16 |
|
Whilst farting I posted:I've spent hours Googling binding variables and that specific error message and I've found no clear explanations or examples. You should always refer to Oracle's documentation first. You can download it for free from Oracle's site - I keep a local copy for 9.2/10.2/11.1. In this case, you would be interested in the Pro*C/C++ Programmer's Guide. A link to the relevant page follows: http://download.oracle.com/docs/cd/B19306_01/appdev.102/b14407/pc_04dat.htm#i27299
|
# ? Apr 2, 2009 17:27 |
|
var1ety posted:You should always refer to Oracle's documentation first. You can download it for free from Oracle's site - I keep a local copy for 9.2/10.2/11.1. In this case, you would be interested in the Pro*C/C++ Programmer's Guide. A link to the relevant page follows: I checked that, but it just left me more confused. According to their documentation, what I wrote should work quote:EXEC SQL SELECT ename, sal INTO :ename, :sal FROM emp That's the given example, and although I do reference the select statement via :sqlStatement, what's contained in it is the same format.
|
# ? Apr 2, 2009 17:33 |
|
Whilst farting I posted:I checked that, but it just left me more confused. According to their documentation, what I wrote should work Do you have an "int empno" declared in your routine?
|
# ? Apr 2, 2009 17:37 |
|
That was their example select statement. What I have iscode:
quote:select * from INT inner join EMP on INT.IEMPNUM = EMP.EMPNUM inner join DIV on INT.IDIVNUM = DIV.DIVNUM where DIV.DIVNUM = :numReq and numReq is declared in the beginning in the declare section.
|
# ? Apr 2, 2009 17:41 |
|
Whilst farting I posted:That was their example select statement. What I have is You probably meant "int" instead of "unsigned int" (or long/short, as appropriate). The documentation describes valid C => Oracle mappings.
|
# ? Apr 2, 2009 17:44 |
|
Whilst farting I posted:Thank you! I'm just getting familiar with the types of joins, so this looks very close to what I need. This is probably because I am using embedded SQL, but my output looks like Triple Tech posted:FYI, in production code, you never want to select star[...]. Explicitly select the columns you want (for both speed and future proofing). quote:Also, I'm having a few other problems with this program - I'm not quite sure how to bind a variable so it'll be usable. I'm trying to let the user input a number to query, but any time I put the variable name into the select statement, it refuses it (I've tried numReq, :numReq, numReq.arr, :numReq.arr but none work). Here's all the relevant code. It's really lovely but I'd rather get the basics working before tightening it up further because it's driving me nuts. E:F,b.
|
# ? Apr 2, 2009 17:46 |
|
var1ety posted:You probably meant "int" instead of "unsigned int" (or long/short, as appropriate). The documentation describes valid C => Oracle mappings. Just int didn't work, either. I'll take a closer look at the examples given. Jethro posted:Triple Tech's advice to EndOfRadio applies to you too: I know select * is not that great of a practice, but I'm not sure how to condense the select statement without just not using the intersection relation altogether and not using a join. It seems like a join is the most simplistic way to go.
|
# ? Apr 2, 2009 17:56 |
|
Whilst farting I posted:Just int didn't work, either. I'll take a closer look at the examples given. It's probably because of building the string dynamically at run-time. It looks like in this case you have to use USING to pass the host variables to Oracle. http://download.oracle.com/docs/cd/B19306_01/appdev.102/b14407/pc_13dyn.htm#i2337 code:
|
# ? Apr 2, 2009 18:05 |
|
Whilst farting I posted:I know select * is not that great of a practice, but I'm not sure how to condense the select statement without just not using the intersection relation altogether and not using a join. It seems like a join is the most simplistic way to go. Not sure what you're worried about. Instead of saying select a.*, b.*, just say select a.wanted, a.wanted1, b.wanted. The select clause of the select statement has nothing to do with the joinagenessosity.
|
# ? Apr 2, 2009 18:28 |
|
So apparently Oracle decides to poo poo its pants for no reason? Here's the structure of the 3 relations (with a variable I'm using for embedded sql at the top code:
Here are two separate statements I'm using. Either one runs or the other depending on user input. Whenever I run the second one, it works fine. Oracle, for whatever reason, does not like the first one. code:
|
# ? Apr 3, 2009 00:09 |
|
INT is a reserved word in SQL-92, but that doesn't explain why the second query worked. Might be worth trying to put it in double-quotes and alias it to a different name. Might also be worth a try to write it with ANSI joins (INNER JOIN). code:
Column names such as "EMPNUM" and "IEMPNUM" ought to use the same column name to represent the same data element across the schema. If you have any influence over the schema you might well want to get rid of those loving awful all-uppercase and cryptically-abbreviated table and column names.
|
# ? Apr 3, 2009 01:03 |
|
INT is INTS, and EMPS is EMP. quote:select * from EMP, INTS, DIVS where EMP.EMPNUM = :numReq and EMP.EMPNUM = INTS.IEMPNUM and INTS.IDIVNUM = DIVS.DIVNUM I've been kicking around the idea of trying to do this with a character array instead of an unsigned int, but I have no idea how that'd work, especially with the cursor - the numbers I need to input are 3 digits (003, etc).
|
# ? Apr 3, 2009 01:33 |
|
Whilst farting I posted:ORA-01722: invalid number You are either not passing a valid number into your bind, or you're querying a text column which has a value which cannot be cast into a number. For example, emps.empnum is defined as varchar and you're trying to query using a numerical bind.
|
# ? Apr 3, 2009 01:35 |
|
var1ety posted:You are either not passing a valid number into your bind, or you're querying a text column which has a value which cannot be cast into a number. For example, emps.empnum is defined as varchar and you're trying to query using a numerical bind. code:
|
# ? Apr 3, 2009 01:41 |
|
Whilst farting I posted:
Are any of these defined as varchar and participating in a join with a numerical column? code:
|
# ? Apr 3, 2009 02:23 |
|
Every single one of those are declared as unsigned ints. That's why I'm so confused and frustrated, and willing to say "gently caress it" and implement a character array to take care of it, even though I have no idea how to do that. The digits being entered are 3 digit and when googling I saw some posts saying that might make them considered strings or something?? But when I throw in a cout statement to make sure my numbers are still the same as taken in by c-in, they're fine for comparison purposes (014 goes to 14, etc) Edit: For a test, I drastically shortened the statement. select * from EMP, INTS, DIVS where EMP.EMPNUM = 014 And STILL I get errors. Here's info on EMPNUM if this helps code:
Whilst farting I fucked around with this message at 02:37 on Apr 3, 2009 |
# ? Apr 3, 2009 02:33 |
|
Are the other columns I asked about NUMBER also?
|
# ? Apr 3, 2009 02:59 |
|
var1ety posted:Are the other columns I asked about NUMBER also? Yeah. In the embedded C++ they're unsigned ints, but the relations themselves, they're numbers.
|
# ? Apr 3, 2009 03:21 |
|
Welp, nothing wrong with the statement after all. Turns out it was my FETCH that had problems. Whoops.
|
# ? Apr 3, 2009 17:12 |
|
I am writing the following statement:code:
code:
cannibustacap fucked around with this message at 06:01 on Apr 4, 2009 |
# ? Apr 4, 2009 05:55 |
|
You can't normally update or delete in MySQL by referencing a copy of the same table. You can work around it by wrapping the subquery in an extra subquery as depicted here: http://www.xaprb.com/blog/2006/06/23/how-to-select-from-an-update-target-in-mysql/
|
# ? Apr 4, 2009 14:26 |
|
When Daylight Saving Time began here recently, I ran into a problem with my PHP/MySQL website. Certain things (posted messages, moves taken in games, etc.) have a DATETIME variable associated with them to say when they happened. When someone, for example, views a posted message, the time it was posted is displayed at the top. The intention was always to have this time displayed as UTC (the application isn't very advanced and currently doesn't serve users times converted appropriately for their time zones). When DST started here, the times started to be shifted ahead by one hour, which isn't what was wanted. I initially assumed the problem was originating with PHP, but following some posts in the PHP general help thread and some experimentation, I realised the problem is with the way I'm recording the times when communicating with MySQL. At present, whenever I insert or update a record with a datetime value, I just use the NOW() command to record the time. As in, code:
code:
code:
|
# ? Apr 7, 2009 13:10 |
|
UTC_TIMESTAMP()?
|
# ? Apr 7, 2009 18:49 |
|
Here's a weird one...in SQL 2008, I'm trying to export data from one database to another database. The table structures are exactly the same. In SQL 2005, I would right-click on the database, choose Tasks -> Export, choose the source and destination, and tell SQL to append the data from the source table to the destination table. It would crank for a while (the source table has about 20 million rows, and the destination table has about 600 million rows), and all would be well. In SQL 2008, it seems that something has changed. I go through the exact same process, but after I tell SQL to append to the destination table, it decides that a datetime column in the source table is actually a datetime2 column. The table structure hasn't changed since 2003, and I've double checked anyways to make sure no one screwed it up...it's definitely a datetime column. Anyways, SQL attempts to convert the "datetime2" values into datetimes, which takes freaking forever. So, why does SQL keep picking up that source column as a datetime2 and not a datetime? Is there a bug or something that I don't know about in the import/export wizard?
|
# ? Apr 7, 2009 19:49 |
|
Jethro posted:UTC_TIMESTAMP()? Ah, thanks, that's just what I needed.
|
# ? Apr 7, 2009 23:20 |
|
|
# ? May 16, 2024 05:31 |
|
I am trying to import a few thousand row CSV file (from Excel) into mysql and everything works fine except the few lines that have a comma inside them. There are about 150 of these and it's a pain to manually change them. Is there anyway to get mysql's load function to respect "Smith, Jr." as one entity without the quotes? That's how excel exports it and I don't think that I can format it any other way. Thanks!
|
# ? Apr 10, 2009 02:27 |