database-design

How to use Unique Composite Key

痴心易碎 提交于 2020-01-11 02:49:13
问题 I have a table Item(ItemName*, ItemSize*, Price, Notes) I was making composite key of (ItemName,ItemSize) to uniquely identify item. And now after reading some answers on stackoverflow suggesting the use of UNIQUE i revised it as Item(ItemID*, ItemName, ItemSize, Price, Notes) But How to apply UNIQUE constraint on ItemName and ItemSize please correct if there is something wrong in question 回答1: ALTER TABLE Items ADD UNIQUE INDEX(ItemName, ItemSize); and here's an article explaining how to

SQLite composite key (2 foreign keys) Link table

拜拜、爱过 提交于 2020-01-10 08:49:08
问题 I've read the rather cool styled BNF grammar for the SQLite create table statement found here: http://www.sqlite.org/lang_createtable.html I was wondering how I'd go about creating a link table between these I have one table, lets say, houses, and another electrical_items. I want to create a link table to have the house_id and the item_id as a composite key, but I'm not sure how I'd go about doing it, it doesn't appear to allow a primary key to be a foreign key ? N.B I want a third field pap

UK Vat change from 17.5 to 15% - How will this affect your code?

扶醉桌前 提交于 2020-01-10 07:27:15
问题 The UK VAT system is changing from 17.5% to 15%. What strategies have you used in your code to store the VAT, and how will the change affect your applications. Do you store a history of vats so you can calculate old prices, or are old invoices stored in a separate table? Is it a simple config setting, or did you bodge it? What's the ideal way to store VAT? 回答1: Don't calculate it. Store it! HMRC are very fussy about getting paid the right amount of VAT. The rounding of VAT calculations is

Foreign Keys - What do they do for me?

风格不统一 提交于 2020-01-10 02:49:31
问题 I'm building a small application and setting up foreign key relationships between tables. However I'm confused as to WHY I really need this? What is the advantage - does it assist me when writing my queries that I don't have to perform any joins? Here's an example snippet of my database: +-------------------+ | USERS | +-------------------+ | user_id | | username | | create_date | +-------------------+ +-------------------+ | PROJECTS | +-------------------+ | project_id | | creator | | name

Maintaining subclass integrity in a relational database

北城余情 提交于 2020-01-09 19:30:12
问题 Let's say I have a table that represents a super class, students . And then I have N tables that represent subclasses of that object ( athletes , musicians , etc). How can I express a constraint such that a student must be modeled in one (not more, not less) subclass? Clarifications regarding comments: This is being maintained manually, not through an ORM package. The project this relates to sits atop SQL Server (but it would be nice to see a generic solution) This may not have been the best

Designing 1:1 and 1:m relationships in SQL Server

故事扮演 提交于 2020-01-09 19:10:34
问题 In SQL Server 2008, how does one design a 1:1 and 1:m relationship? 回答1: Any relationship requires that the "parent" table (the one side) have a Primary (or unique) Key (PK), that uniquely identifies each row, and the "child" table (the other side) have a Foreign Key column or columns, that must be populated with values that are the same as some existing value[s] of the Primary Key in the parent table. If you want a one to many (1-M) relationship then the Foreign Key should be an ordinary

Designing 1:1 and 1:m relationships in SQL Server

不问归期 提交于 2020-01-09 19:09:37
问题 In SQL Server 2008, how does one design a 1:1 and 1:m relationship? 回答1: Any relationship requires that the "parent" table (the one side) have a Primary (or unique) Key (PK), that uniquely identifies each row, and the "child" table (the other side) have a Foreign Key column or columns, that must be populated with values that are the same as some existing value[s] of the Primary Key in the parent table. If you want a one to many (1-M) relationship then the Foreign Key should be an ordinary

How do we implement an IS-A Relationship?

断了今生、忘了曾经 提交于 2020-01-09 12:18:06
问题 We implement an One-to-Many relationship by adding one Table's PK, as FK to the other Table. We implement a Many-to-Many relationship by adding 2 Table's PKs to a third Table. How do we implement an IS-A Relationship ? The Entities are TECHNICIAN and ADMINISTRATIVE which both are EMPLOYEE. I could just use an extra field in the Table EMPLOYEE(id, name, surname, role , ...AdminFields..., ...TechFields...) but i would like to explore the IS-A option. EDIT: I did as Donnie suggested, but without

UNIQUE constraint controlled by a bit column

最后都变了- 提交于 2020-01-09 11:10:53
问题 I have a table, something like FieldsOnForms( FieldID int (FK_Fields) FormID int (FK_Forms) isDeleted bit ) The pair (FieldID,FormID) should be unique, BUT only if the row is not deleted (isDeleted=0). Is it possible to define such a constraint in SQLServer 2008? (without using triggers) P.S. Setting (FieldID, FormID, isDeleted) to be unique adds the possibility to mark one row as deleted, but i would like to have the chance to set n rows (per FieldID,FormID) to isDeleted = 1, and to have

Database Design Question - Categories / Subcategories

谁都会走 提交于 2020-01-09 07:11:25
问题 I have a question for how I would design a few tables in my database. I have a table to track Categories and one for Subcategories: TABLE Category CategoryID INT Description NVARCHAR(500) TABLE Subcategory SubcategoryID INT CategoryID INT Description NVARCHAR(500) A category might be something like Electronics, and its Subcategories might be DVD Players, Televisions, etc. I have another table that is going to be referencing the Category/Subcategory. Does it need to reference the SubcategoryID