database-agnostic

Should Many to Many Tables Have a Primary Key?

青春壹個敷衍的年華 提交于 2019-12-03 04:23:21
If I have two objects that have a many-to-many relationship, I would typically model them in my database schema with a many-to-many table to relate the two. But should that many-to-many table (or "join table") have a primary key of its own (integer auto-incremented)? For example, I might have tables A and B, each with an ID, and a table called A_B that has a foreign key tuple of (A_ID, B_ID). But should A_B have a primary key auto-incremented ID column of its own, or not? What are the advantages and disadvantages of adding it? I personally like natural keys for many-to-many joins. But what

Can there be a database-agnostic SQL query to fetch top N rows?

最后都变了- 提交于 2019-12-02 19:19:24
问题 We want to be able to select top N rows using a SQL Query. The target database could be Oracle or MySQL. Is there an elegant approach to this? (Needless to say, we're dealing with sorted data here.) 回答1: To get the top 5 scorers from this table: CREATE TABLE people (id int, name string, score int) try this SQL: SELECT id, name, score FROM people p WHERE (SELECT COUNT(*) FROM people p2 WHERE p2.score > p.score ) <=4 I believe this should work in most places. 回答2: No. The syntax is different.

Number of times a nested query is executed

你说的曾经没有我的故事 提交于 2019-12-02 04:49:17
How many times will this nested sub-query be executed? SELECT CID, CNAME FROM CUSTOMER WHERE EXISTS ( SELECT CID FROM RENTALS WHERE CUSTOMER.CID = RENTALS.CID AND PICKUP = 'CARY' ) This is a theoretical question i.e. it is found in my book. The provided answer was 6, but I don't understand why this is so. Ok, I think there is some problem with the book itself. I will go through the book and maybe, later ask the question. As others point out, this correlated subquery can be rewritten as a join, but that is not entirely the complete story because the execution plan for an untransformed EXISTS is

Slick generic AND driver agnostic

回眸只為那壹抹淺笑 提交于 2019-12-01 01:08:15
Basically what I want to achieve is a combination of: Slick 3.0.0 database agnostism and Slick 3 reusable generic repository I tried a lot, actually, but I can't get this to work at all. abstract class BaseModel[T <: slick.lifted.AbstractTable[_]](query: TableQuery[T], val driver: JdbcProfile, val dbTableName: String) { lazy val all: TableQuery[T] = TableQuery[T] import driver.api._ def createTable = all.schema.create def dropTable = all.schema.create abstract class BaseTable[B](val tag: Tag) extends Table[B](tag, dbTableName) { def id = column[Long]("id", O.PrimaryKey, O.AutoInc) } } Now here

When to use database views and when not?

大憨熊 提交于 2019-11-28 20:15:46
This question is about database views, not materialized-views. Pros: Query simplification. Avoid repeat the same joins on multiples queries. Avoid magic numbers. Cons: Hiding real queries (may be you are repeating joins). What else? Pros: Allows you to change the underlying data structures without affecting the queries applications are using (as long as your view can hide the data structures) Security. Grant access on a view to the users who should be able to see the columns returned from it. Views are pretty awesome when you don't entirely trust the party sending queries to your database. A

Fast Relational method of storing tree data (for instance threaded comments on articles)

主宰稳场 提交于 2019-11-28 18:24:32
I have a cms which stores comments against articles. These comments can be both threaded and non threaded. Although technically they are the same just with the reply column left blank when it's not threaded. My application works on sqlLite, MySQL and pgsql so I need fairly standard SQL. I currently have a comment table comment_id article_id user_id comment timestamp thread (this is the reply column) My question is to figure out how to best represent the threaded comments in the database. Perhaps in a separate table that supports the tree set without the content and a simple table to hold the

Database cluster and load balancing

不打扰是莪最后的温柔 提交于 2019-11-28 13:17:14
问题 What is database clustering? If you allow the same database to be on 2 different servers how do they keep the data between synchronized. And how does this differ from load balancing from a database server perspective? 回答1: Database clustering is a bit of an ambiguous term, some vendors consider a cluster having two or more servers share the same storage, some others call a cluster a set of replicated servers. Replication defines the method by which a set of servers remain synchronized without

When to use database views and when not?

不问归期 提交于 2019-11-27 12:47:46
问题 This question is about database views, not materialized-views. Pros: Query simplification. Avoid repeat the same joins on multiples queries. Avoid magic numbers. Cons: Hiding real queries (may be you are repeating joins). What else? 回答1: Pros: Allows you to change the underlying data structures without affecting the queries applications are using (as long as your view can hide the data structures) 回答2: Security. Grant access on a view to the users who should be able to see the columns

Fast Relational method of storing tree data (for instance threaded comments on articles)

房东的猫 提交于 2019-11-27 11:32:43
问题 I have a cms which stores comments against articles. These comments can be both threaded and non threaded. Although technically they are the same just with the reply column left blank when it's not threaded. My application works on sqlLite, MySQL and pgsql so I need fairly standard SQL. I currently have a comment table comment_id article_id user_id comment timestamp thread (this is the reply column) My question is to figure out how to best represent the threaded comments in the database.

Function that creates a timestamp in c#

梦想与她 提交于 2019-11-27 06:13:28
I was wondering, is there a way to create a timestamp in c# from a datetime? I need a millisecond precision value that also works in Compact Framework(saying that since DateTime.ToBinary() does not exist in CF). My problem is that i want to store this value in a database agnostic way so i can sortby it later and find out which value is greater from another etc. I always use something like the following: public static String GetTimestamp(this DateTime value) { return value.ToString("yyyyMMddHHmmssfff"); } This will give you a string like 200905211035131468, as the string goes from highest order