我是 Ruby 的新手,我有包含这些主键的表:
当然,这些模型还有其他非主键,例如 customer_id、connection_id 或日期或 user_id 等,但这些对于关系并不重要,因为这些只是数据,或者我对这些没有任何问题。
这些是我的模型:
#models
class transaction_type < ActiveRecord::Base
has_many :transaction_headers, :foreign_key=>'transaction_type'
has_many :transaction_details, :foreign_key=>'transaction_type'
has_many :tickers, :through=>:transaction_details
end
class transaction_header < ActiveRecord::Base
belongs_to: transaction_types, :foreign_key=>'transaction_type'
has_many :transaction_details
has_many :tickers, :through=>:transaction_details
end
class transaction_detail < ActiveRecord::Base
belongs_to: transaction_headers
has_many :tickers
end
class ticker < ActiveRecord::Base
end
Run Code Online (Sandbox Code Playgroud)
我需要与每个对应的主键建立关系。 transaction_type到transaction_detail和transaction_header很容易,但是如何在transaction_header和transaction_detail之间以及transaction_detail …
我需要创建一个数据库设计,其中bar或事件所有者(admin-table因为他们拥有配置文件)向用户发送警报.
我想创建一个"ALERT"表,但很难决定使用哪个主键.我想添加一个至少包含admin_ID(PFK),user_ID(PFK)的复合键,我想在主键上添加日期和时间戳,以指示管理员可以发送许多警报(通知),但只有1某个时间点.
但是,从这个线程:时间戳作为复合主键的一部分?,我了解到我不应该使用时间戳.
顺便说一句,还没有确定我们将在这一点上使用什么数据库软件.
我有时会倾向于快速移动到自动增量键.我从来没有注意到它的问题(在Access中),但是根据我的阅读,这可能并不总是最有意义的事情,因此我向专业人士提出这个问题.
我只有一次机会以正确的方式做到这一点,使后端基本正确.
你对此有何看法?
MySQL 引擎:InnoDB
我有一张桌子,我需要两个字符串作为主键。这样做的原因是因为我希望 ON DUPLICATE KEY UPDATE 依赖于这两个字符串。我怎么做?提前泰。
我有一个包含3个主键的表.它们是custom_id,patient_idpatient,service_provider_type_idservice_provider_type.我目前的映射如下
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="beans.ServiceProvider" table="service_provider" catalog="myglukose" optimistic-lock="version">
<id name="customId" type="string">
<column name="custom_id" not-null="true"/>
</id>
<many-to-one name="patient" class="beans.Patient" fetch="select">
<column name="patient_idpatient" not-null="true"/>
</many-to-one>
<many-to-one name="serviceProviderType" class="beans.ServiceProviderType" fetch="select">
<column name="service_provider_type_idservice_provider_type" not-null="true"/>
</many-to-one>
<property name="dateCreated" type="timestamp">
<column name="date_created" length="19" />
</property>
<property name="lastUpdated" type="timestamp">
<column name="last_updated" length="19" not-null="true" />
</property>
</class>
</hibernate-mapping>
Run Code Online (Sandbox Code Playgroud)
我的SQL代码
CREATE TABLE IF NOT EXISTS `xxx`.`service_provider` (
`custom_id` VARCHAR(45) NOT NULL,
`patient_idpatient` INT …Run Code Online (Sandbox Code Playgroud) java mysql hibernate hibernate-mapping composite-primary-key
我有一个没有主键的数据库视图。它有一组唯一标识视图中的行的列,但其中三列可以是null. 我尝试使用基于这四列的复合主键创建一个实体,但是当从视图中检索数据时,我收到此错误:
The primary key read from the row ... during the execution of the query was detected to be null. Primary keys must not contain null.
Run Code Online (Sandbox Code Playgroud)
我可以做些什么,例如,在定义视图时添加自动生成的列?
我从这里扩展我的问题:使用 JPA/Hibernate 使用三个表定义 CompositeKey?。在此示例中,我希望创建复合键来创建 PRODUCT_ID、CATEGORY_ID、STOCK_ID 的唯一组合。
我开发了下面的代码,但不确定如何将记录保存到数据库中。
Stock.java
@Entity
public class Stock implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = IDENTITY)
@Column(name = "STOCK_ID", unique = true, nullable = false)
private Integer stockId;
@Column(name = "STOCK_CODE", unique = true, nullable = false, length = 10)
private String stockCode;
@Column(name = "STOCK_NAME", unique = true, nullable = false, length = 20)
private String stockName;
// Owner of the relationship
@OneToMany(fetch = FetchType.LAZY, …Run Code Online (Sandbox Code Playgroud) CREATE TABLE [dbo].[INVS_ITEM_LOCATIONS]
([DEPARTMENT_CODE] [varchar](3) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
[IM_INV_NO] [numeric](10, 0) NOT NULL,
[LOCATION_CODE] [varchar](2) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
[CURR_QTY] [numeric](10, 0) NOT NULL CONSTRAINT [DF__INVS_ITEM__CURR___1352D76D] DEFAULT ((0)),
[DO_QTY] [numeric](10, 0) NOT NULL CONSTRAINT [DF__INVS_ITEM__DO_QT__1446FBA6] DEFAULT ((0)),
[ALLOC_QTY] [numeric](10, 0) NOT NULL CONSTRAINT [DF__INVS_ITEM__ALLOC__153B1FDF] DEFAULT ((0)),
[YOB_QTY] [numeric](10, 0) NOT NULL CONSTRAINT [DF__INVS_ITEM__YOB_Q__162F4418] DEFAULT ((0)),
[FOC_QTY] [numeric](10, 0) NULL CONSTRAINT [DF__INVS_ITEM__FOC_Q__17236851] DEFAULT ((0)),
[USER_CREATED] [varchar](25) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
[DATE_CREATED] [datetime] NOT NULL,
[USER_MODIFIED] [varchar](25) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[DATE_MODIFIED] …Run Code Online (Sandbox Code Playgroud) 当使用带有时间戳的linq-to-sql作为复合主键的一部分时,我收到此错误:
"服务器无法生成'Timestamp'类型的主键列."
我猜这可能是因为时间戳只是一个行版本因此也许它必须在插入后创建?要么...
我想设计一个看起来像这样的表:
avaya nchar(6) NOT NULL,
startDate datetime NOT NULL,
endDate datetime NOT NULL,
sup_assigned nvarchar(255) NOT NULL,
myID int NOT NULL IDENTITY (1, 1)
但我想表停止任何插入/更新,其中avaya,startDate和sup_assigned是相同的任何记录或avaya,endDate和sup_assigned在任何记录一样.
我该怎么做才能做到这一点?
我考虑过转换包含startDateinto为hex 的3列的集合,然后添加它们创建Key1列,与包含the endDate和列的其他3列相同Key2.然后设置Key1并Key2作为复合键.
但我得到这个错误:
我应该如何使用MS SQL 2005执行此操作?
Cakephp v3现在能够支持复合主键. http://book.cakephp.org/3.0/en/quickstart.html
快速入门指南中给出的一个例子;
CREATE TABLE users (
id INT AUTO_INCREMENT PRIMARY KEY,
email VARCHAR(255) NOT NULL,
password VARCHAR(255) NOT NULL,
created DATETIME,
modified DATETIME
);
CREATE TABLE bookmarks (
id INT AUTO_INCREMENT PRIMARY KEY,
user_id INT NOT NULL,
title VARCHAR(50),
description TEXT,
url TEXT,
created DATETIME,
modified DATETIME,
FOREIGN KEY user_key (user_id) REFERENCES users(id)
);
CREATE TABLE tags (
id INT AUTO_INCREMENT PRIMARY KEY,
title VARCHAR(255),
created DATETIME,
modified DATETIME,
UNIQUE KEY (title)
);
CREATE TABLE bookmarks_tags (
bookmark_id …Run Code Online (Sandbox Code Playgroud) mysql database-design cakephp primary-key composite-primary-key
java ×3
mysql ×3
primary-key ×3
jpa ×2
sql-server ×2
activerecord ×1
cakephp ×1
database ×1
hex ×1
hibernate ×1
innodb ×1
jpa-2.2 ×1
linq-to-sql ×1
sql ×1
timestamp ×1