sql

Database Design - NULL Foreign Keys

Hello, and thanks for reading. I'm making a DB for a puppy shop. I have a table for puppies and a table for owners. A puppy can have one owner, owners can own more than one puppy, but not all puppies are owned. What's a good way to handle this situation? Do I use a FK in the puppy table that is NULL if the puppy doesn't have an owne...

SQL Query for lowest time rows

I have a table with one DateTime column. I want to find the rows with lowest time which their times are more than a variable myTime. How can I do that? What kind of index will increase the performance? ...

Need help with small Solr problem

I have a String field where I store two-word area names. Ex: New York Thing is, whenever I try to query that field (area:New York) no results come up, even though it is stored exactly as New York. Why is this? The results DO come up if I search like this: area:"New York" but they wont come up if searching like this: area:New. Any id...

SQL Server (T-SQL) Insert if Don't Exist.

Question How can I alter the SQL code below to ensure that the inserts into "dbo.tblBootStrapperInstruments" only occur if they don't already exist? Code INSERT INTO dbo.tblBootStrapperInstruments (CCY, Instrument, Tenor) SELECT CCY,Instrument,Gridpoint FROM dbo.Feed_Murex_Intraday_DF mx WHERE NOT EXISTS (SELECT * FROM dbo.tblBootStr...

ignore insert of rows that violate duplicate key index

I perform an insert as follows: INSERT INTO foo (a,b,c) SELECT x,y,z FROM fubar WHERE ... However, if some of the rows that are being inserted violate the duplicate key index on foo, I want the database to ignore those rows, and not insert them and continue inserting the other rows. The DB in question is Informix 11.5. Curre...

SQL Server query to select local maximums

I have this data. I need to get the lowest $ full rows for each person. Amount Date Name $123 Jun 1 Peter $120 Jun 5 Peter $123 Jun 5 Paul $100 Jun 1 Paul $220 Jun 3 Paul The result of the SQl Server query should be: $120 Jun 5 Peter $100 Jun 1 Paul ...

How does a strongly typed DataContext work?

This is an in-depth continuation of my question from earlier this morning, which I'm still stumped about. I'm using a strongly typed DataContext for my application and although it emits a warning, it magically works. How does it do this? Here's the code one would typically use to connect to a database using LINQ-to-SQL. class MyDatabas...

What is the best way to track when table(s) are updated in SQL?

In the past I have just added an field to each table and updated it with GETDATE() on every update/insert. The problem is now I have to keep track of delete too. I was thinking of just having a table that I would update when anything changed and add a trigger to all of the other tables. Ideas??? Thanks! ...

Efficient SQL query for two update and search queries

I have a query like this: SELECT TOP 1 ID, DATA, OTHERINF FROM MYTABLE WHERE DATE = @DATE and after reading the row data and using it I want to update that retrieved row and change one of it's columns (in another transaction). But as you see here i searched for that row twice. Is there anyway that I keep remember the row and do the u...

add column while copying data in sql

I'm using SqlBulkCopy to bulk insert some records from one table into another table. The query is using a SqlDataReader to get the data. The one difference that matters (besides column order which is handled in the mappings) between the tables is that the destination table has a date column into which the current date needs to be added...

Is there an index on the primary key of a table?

Does having a primary key column mean there is an index on that column? If so, what kind of index is it? ...

How to configure a Firebird Database to run in memory

I'm running a software called Fishbowl inventory and it is running on a firebird database (Windows server 2003) at this time the fishbowl software is running extremely slow when more then one user accesses the software. I'm thinking I maybe able to speed up the application by forcing the database to run "In Memory". However I can not fin...

Please help on a complicated sql query

MySQL 5.0.45 Table A has the following fields (columns): 1. transcation_id 2. client_name 3. item_id 4. ..... Now I need to find how many transactions each client has made order by # of transactions. The result should be like: Tom 7 transactions Jack 5 transactions Mike 2 transactions If a client has no transactions his n...

Can a Superkey include things that aren't part of the Primary key?

Can a Superkey include things that aren't part of the Primary key? ...

Why does SSIS Lookup task output a DT_TEXT type even though the value returned through the lookup is a VARCHAR(50)?

Running SQL Server Integration Services (64-bit install of SQL Server 2008 Developer) on a 64 bit version of Windows 7 Professional. I have a simple SSIS package which imports data from a delimited text file, does a lookup to a SQL Server table, joining on a key field (int) and returns the UserName (varchar(50)) from that table. Sample...

Help us fix this sql statement.

SELECT DISTINCT u.UserID, UserLastName, UserFirstName, UserName, Notified, MAX (CycleNumber) as CycleNumber, (CycleCurrentStep) as CycleCurrentStep, MAX (CycleDateReported) as CycleDateReported, max (cycleid) FROM [User] u left join Cycle c on (u.UserID = c.UserID) join UserDivSection us on (u.UserID = us.UserID and us.DivSection...

Firebird database tuning for multiprocessor

I'm running a software called Fishbowl inventory and it is running on a firebird database (Windows server 2003). At this time the fishbowl software is running extremely slow when more then one user accesses the software. I'm wondering if anyone could provide some information on tuning the database or best practices? We are currently runn...

SQL Server 2005 Unique constraint on two columns

How do you add a unique constraint in SQL Server 2005 to two columns? So lets say that I have: PK, A, B ... x1 1 1 x2 1 2 x3 2 1 x4 2 2 I should not be able to add another row 'x5' and have the values for A and B be 1,1 as they are already in the database in x1? Ok we managed to get it to work and thanks to OMG. Go to the t...

Transact-SQL: Detect Date boundaries across multiple rows.

I am creating a stored procedure that produces reports based on data in a SQL database and stores these reports in a separate database. The data being reported is the total time over a date range that a motor was running. I want to be able to detect if the timeframe I am reporting overlaps previous reports based on date times. For ins...

Distinct elements in django model

I'm a Django newbie and I wonder if there is a more efficient way (at a database level) of doing the following. I have the model: class Foo(models.Model): item=models.IntegerField() another_item=models.IntegerField() And want to get an iterable of all the distinct values of "item". This is what I have so far: distinct=set([...