database-design

One Address Table for Many entities?

元气小坏坏 提交于 2020-01-03 04:34:27
问题 Conceptual stage question: I have several Tables ( Person , Institution , Factory ) each has many kinds of Addresses ( Mailing , Physical ) Is there a way to create a single Address table that contains all the addresses of all the Entities? I'd rather not have a PersonAddress and FactoryAddress etc set of tables. Is there another option? The amount of data will only be several thousand addresses at most, so light in impact. 回答1: My proposal relies on the principle that one entity (person,

MYSQL and RDBMS [closed]

南楼画角 提交于 2020-01-03 04:26:11
问题 As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance. Closed 7 years ago . I'm having difficulties to answer this question. Can someone help me? Discuss the benefits of MYSQL and explain why it is gaining

one field with different data types [SQLAlchemy]

好久不见. 提交于 2020-01-03 03:54:07
问题 I have a value that can be integer, float or string, and I created different columns: #declarative class MyClass(Base): #id and other Columns _value_str = Column(String(100)) _value_int = Column(Integer) _value_float = Column(Float) def __init__(self,...,value): self._value_str = value if isinstance(value,(str,unicode)) else None self._value_int = value if isinstance(value,int) else None self._value_float = value if isinstance(value,float) else None and i would like to do something like this:

SQL Syntax: Create a single table with column names created from data stored over multiple tables

百般思念 提交于 2020-01-03 03:04:50
问题 I have created the SQL script below that creates four tables and inserts data into the tables to demonstrate my question (I hope this helps!). This problem arose because originally this data was stored in a single table of 400 columns and it got very unmanageable so I wanted to find a better way to store the parameters: use master GO create database sptest go use sptest go CREATE TABLE JobFiles ( [Id] int PRIMARY KEY IDENTITY(0,1), [JobName] nvarchar(256) DEFAULT '', [CreateDate] DateTime NOT

Unique constraints across a many to many relationship table

和自甴很熟 提交于 2020-01-03 02:23:10
问题 Is there any way to add a constraint to ensure that an entry of X in one column can then only allow an entry of Y in another column? Say I have two tables, stripped down to minimal columns, tbl_1 has a pk. tbl_2 has 2 columns - a pk and a text string. These tables are joined by a 3rd relationship table because they're many to many and it uses pk from tbl1 and tbl2. t1_pk t2_pk | t2_str t1fk | t2fk x 1 AAA x 1 y 2 BBB x 2 z 3 AAA y 3 4 BBB y 4 z 1 z 2 All entries above are allowed, but now I'm

Foreign key of serial type - ensure always populated manually

懵懂的女人 提交于 2020-01-02 23:15:34
问题 I have two tables: countries and regions. CREATE TABLE Countries( id SERIAL, name VARCHAR(40) NOT NULL, PRIMARY KEY(id) ) CREATE TABLE Regions( id SERIAL, countryId SERIAL, name VARCHAR(40) NOT NULL, PRIMARY KEY(id ), FOREIGN KEY(countryId) REFERENCES Countries(id) ) When I insert into regions, I would hope that if I fail to mention countryId , I would be stopped, however, countryId is automatically incremented. Is there any way I can be stopped automatically to make this insertion? Following

separate table for picture items with image field

走远了吗. 提交于 2020-01-02 19:22:23
问题 I store different items (notes, articles, pictures, files) in a single table (there are many metadata in common for all the item types - for example, categories, tags, rating, statistics etc.). My first design was like this: table Items , plus another "detail" table for each of the item types ( NoteItems , ArticleItems , PictureItems etc.). To retrieve a single item, tables must be joined one-to-one (SELECT * FROM Items INNER JOIN PictureItems ON Items.Id = PictureItems.Id WHERE Items.Id = N)

How do I properly design a database? Foreign Keys vs Secondary Keys?

雨燕双飞 提交于 2020-01-02 18:21:29
问题 here are some generic tables, I am trying to fully understand how to properly setup databases tables. Are these setup correctly? I want to be able to lookup a user's Items and Item Details as fast as possible. FYI for this example ItemDetailsX do not share the same data fields. I am a little bit stuck on Foreign Keys and Secondary keys. When do you use a Secondary Key vs a Foreign Key? tbl_Users 1:* tbl_Item //relationship tbl_Item 1:1 tbl_ItemDetail1 & tbl_ItemDetail2 // relationship tbl

Sorting by ratings in a database - Where to put this SQL? (PHP/MySQL)

ε祈祈猫儿з 提交于 2020-01-02 17:02:32
问题 OK - I'll get straight to the point - here's the PHP code in question: <h2>Highest Rated:</h2> <?php // Our query base $query = $this->db->query("SELECT * FROM code ORDER BY rating DESC"); foreach($query->result() as $row) { ?> <h3><?php echo $row->title." ID: ";echo $row->id; ?></h3> <p class="author"><?php $query2 = $this->db->query("SELECT email FROM users WHERE id = ".$row->author); echo $query2->row('email');?></p> <?php echo ($this->bbcode->Parse($row->code)); ?> <?php } ?> Sorry it's a

Mysql: enum confusion

為{幸葍}努か 提交于 2020-01-02 10:07:16
问题 I have an employee table, employee has interests, so the table can be designed like this: create table emp( id int(10) not null auto_increment, name varchar(30), interest varchar(50), primary key(id) ); or this: create table emp( id int(10) not null auto_increment, name varchar(30), interest enum('football','basketball','music','table tennis','volleyball'), primary key(id) ); The number of interests can be about 50. How should i design the table? Should i use enum or others ? Edit: Thanks for