foreign-keys

mysql inserting into 2 tables at once that have primary key and foreign key

◇◆丶佛笑我妖孽 提交于 2019-12-06 10:43:52
问题 I have two tables books table ID (PK) Title Author Genre ISBN Price publisher Year bookAwards table ID (FK) -- referencing Books.ID Awardname Year ID of bookAwards is foreign key of ID in books table. How can I insert into books table at the same time into bookAwards table? When I am trying to insert into books table it gives the error that foreign key causes? I want to insert into books table the values and then an awardname with a year into bookAwards? Any help will be much appreciated. 回答1

Laravel 5.3 Eloquent transactions and foreign key restrictions

狂风中的少年 提交于 2019-12-06 10:35:49
Am working on bigger project where we have multiple schemas in one Postgres DB. We have created foreign keys between schemas. Here is an example > We have company schema and user schema. Company schema has company_users table which have foreign key restriction on user.users table CREATE TABLE company.company_user ( id serial NOT NULL, company_id integer NOT NULL, user_id integer NOT NULL, created_at timestamp(0) without time zone, updated_at timestamp(0) without time zone, deleted_at timestamp(0) without time zone, CONSTRAINT company_user_pkey PRIMARY KEY (id), CONSTRAINT company_user_company

web sql database foreign key support

允我心安 提交于 2019-12-06 10:31:32
问题 I'm testing this statement in Safari 5.0.5, but I get an error before FOREIGN: CREATE TABLE IF NOT EXISTS Idea ( id INTEGER PRIMARY KEY, title TEXT NOT NULL, content TEXT NOT NULL, created TIMESTAMP NOT NULL, sketchID INTEGER, categoryID INTEGER NOT NULL, FOREIGN KEY (sketchID) REFERENCES (Sketch), FOREIGN KEY (categoryID) REFERENCES (Category)); I get the following error message: SQLStatementError 1 [DATABASE] near "(": syntax error Where is the error in this SQL statement? 回答1: (Adding my

Refactor SQLite Table by splitting it in two and link with foreign keys

北城余情 提交于 2019-12-06 09:57:01
问题 I'm working on a SQLite Database. The database is already filled, but I want to refactor it. Here is a sample of what I need to do: I currently have one table: CREATE TABLE Cars (ID INTEGER PRIMARY KEY, Name VARCHAR(32), TopSpeed FLOAT, EngineCap FLOAT); I want to split this into two tables: CREATE TABLE Vehicles (ID INTEGER PRIMARY KEY, Name VARCHAR(32), TopSpeed FLOAT); CREATE TABLE Cars (ID INTEGER PRIMARY KEY, VehicleID INTEGER CONSTRAINT FK_Cars REFERENCES [Vehicles](ID), EngineCap FLOAT

Create Foreign Key SQL

半世苍凉 提交于 2019-12-06 08:33:57
How do I create a foreign key from table tGeoAnswers column 'locationId' to table tLocations column 'id'? ALTER TABLE tGeoAnswers ADD FK_Answer_Location FOREIGN KEY (locationId) REFERENCES tLocations(id) I am trying this code that I found but I get the following error: The definition for column 'FK_Answer_Location' must include a data type ALTER TABLE tGeoAnswers ADD CONSTRAINT FK_Answer_Location ... Otherwise it assumes you're adding a column called FK_Answer_Location . Assuming MsSql Server/T-SQL, use ALTER TABLE : ALTER TABLE tGeoAnswers ADD CONSTRAINT FK_Answer_Location FOREIGN KEY

Add inline model to django admin site

℡╲_俬逩灬. 提交于 2019-12-06 08:26:41
I have this two models: class Rule(models.Model): name = models.CharField(max_length=200) class Channel(models.Model): id = models.CharField(max_length=9, primary_key=True) name = models.CharField(max_length=100) rule = models.ForeignKey(Rule, related_name='channels', blank=True) And I have to be able to add channels to rule in admin site within RuleAdmin interface. So I created this two admin models: class ChannelAdmin(admin.TabularInline): model = Channel class RuleAdmin(admin.ModelAdmin): model = Rule inlines = [ChannelAdmin] But when I start my server I got this errors: ERRORS: <class

oracle sql: not able to add foreign key to table -> invalid identifier?

[亡魂溺海] 提交于 2019-12-06 08:11:31
First off, I'm a real newbie to db and sql. However, I have to tables, PERSON and SPECIES and I want to add a foreign key to the table SPECIES. When trying to add the foreign key I always get the error message "ORA-900904: invalid identifier". I just can't figure out what I've done wrong and why it doesn't work?!?! This was my approach: PERSON table and primary key create table person ( name varchar2 (30), firstname varchar2 (30), persid number (8) not null ) ; alter table person add constraint person_pk primary key (persid) ; SPECIES table and primary key create table species ( speciesnamelat

Entity Framework one-to-zero-or-one foreign key association

时光毁灭记忆、已成空白 提交于 2019-12-06 07:26:48
I am in the process of changing the back-end of an existing application to use Entity Framework Code First. I've used the built-in tool in Visual Studio 2015 to generate POCO classes based on my existing database. This worked perfectly for the most part, except for two classes, with a one-to-zero-or-one relationship. These are my (simplified) classes: public class Login { [Key] [DatabaseGenerated(DatabaseGeneratedOption.Identity)] public int Id { get; set; } public int TeamMemberId { get; set; } public virtual TeamMember TeamMember { get; set; } } public class TeamMember { [Key]

Get Relations of a Table

天涯浪子 提交于 2019-12-06 07:18:46
I want to get relations (or foreign keys) of a Sql Server Table in my c# code. How can i do this? Thank you This will get all the foreign keys that dbo.YourTableName references: SELECT FK = OBJECT_NAME(pt.constraint_object_id), Referencing_col = pc.name, Referenced_col = rc.name FROM sys.foreign_key_columns AS pt INNER JOIN sys.columns AS pc ON pt.parent_object_id = pc.[object_id] AND pt.parent_column_id = pc.column_id INNER JOIN sys.columns AS rc ON pt.referenced_column_id = rc.column_id AND pt.referenced_object_id = rc.[object_id] WHERE pt.parent_object_id = OBJECT_ID('dbo.YourTableName');

MySQL ERROR: 1005 Can't create table 'myTable' (errno : 150)

一个人想着一个人 提交于 2019-12-06 07:11:09
I've read a number of posts about this error, but none of the solutions have managed to solve the problem (assuming I've tried them correctly). This is the code that causes the error: CREATE TABLE season ( id smallint unsigned NOT NULL auto_increment, title varchar(25) NOT NULL, PRIMARY KEY (id) ); CREATE INDEX seasonId ON season(id); DROP TABLE IF EXISTS event; CREATE TABLE event ( id smallint unsigned NOT NULL auto_increment, title varchar(255) NOT NULL, season_id smallint NOT NULL, PRIMARY KEY (id), FOREIGN KEY (season_id) REFERENCES season(id) ON UPDATE RESTRICT ON DELETE RESTRICT ); So