Keeping some choices in the Table for the Field of Type Dropdown - mysql

i am having a Table named
Attributes
which has
id form_id label size sequence_no Type
1 1 Name 200 1 Text
2 1 Age 150 2 Number
3 1 Address 300 3 Textarea
4 1 Gender 200 4 Dropdown
I am having the doubt how can i keep the Choices of the Field of type "Dropdown" in the Table
Eg. For Gender the choices will Male , Female..
Please give me the suggestions...

You simply need a child table like this:
id value_id sequence_no label
4 100 1 Male
4 101 2 Female
Note where id = 4 is all the values for the #4 question.

Sounds like you're trying to build an automagic form that is built from a database?
I would suggest having that table be something like elements and have another table for options which links back to the original id
i.e.
options
id element_id value text
1 4 1 Male
2 4 2 Female
That way for specific types you will be know to look up the options.
You may want another field to specify if its a multiselect or perhaps you could make it another Type.

edit :
you can add another type to this table call dropdown_choice,
and add all the choices of this dropdown to this table,
need to keep in another field the master id of this dropdown choice.

Related

Creating a form in access that adds fields to more than table

So I have 3 tables, and I'd like to be able to create form that will add a new row to table 1, and then use that data to add rows on table 3 with values from table 1 and 2. I've included a brief overview of the table structure. Please let me know if I can clarify anything.
Table 1 - Things
Table 2 - TypesofThings
Table 3 - ThingType
Example of records in the tables:
Table1:
ID - 1 Name: "This"
ID - 2 Name: "That"
Table2: (This table is predefined types of things capped at 15)
`ID - 1 TypeName: Yellow`
`ID - 2 TypeName: Red`
`ID - 3 TypeName: Green`
Table3: This is a "transaction" table that matches the field from table 1 to table 2
`ID: 1 Table1_ID:1 Table2_ID:1`
`ID: 2 Table1_ID:1 Table2_ID:2`
`ID: 3 Table1_ID:1 Table2_ID:3`
`ID: 4 Table1_ID:2 Table2_ID:2`
Example Form of what I'd like to be able to do:
Thing Name: "whatever"
[x] yellow
[] red
[x] green
This submission would add a row to table 1 with the name whatever, and then using the values from table 2 and 2 rows to table 3 like:
id:1 table1_id:1 table2_id:1
id:2 table1_id:1 table2_id:3
I have created the form that will add new rows, but I can't figure out how to do the rest. If you guys could point me in the right direction it would be greatly appreciated!
Looks like many-to-many relationship. Conventional data entry structure:
single form bound to table 3 with comboboxes to select items from tables 1 and 2
main form bound to table 2 and subform bound to table 3 with combobox to select items from table 1
main form bound to table 1 and subform bound to table 3 with combobox to select items from table 2
If you want to be able to add items to table that is source for combobox 'on-the-fly' during data entry, look into the combobox NotInList event. MS NotInList event

How to work with dynamic forms on backend?

I want to implement angularjs-form-builder in my app which is built on Ruby on Rails/MySQL - I'm already searching for last few days, but I can't find any information how people work with something like this.
I was thinking about creating few tables:
Structure
1. Form table
ID NAME
1 People
2. Form fields table
ID FORM_ID FIELD_TITLE FIELD_TYPE FIELD_REQUIRED
1 1 First name textfield 1
2 1 Gender radio 0
3. Options table
ID FIELD_ID OPTION_TITLE
1 2 Male
2 2 Female
Data
2. Row table
ID CREATED_AT
1 2014-05-27 09:21
2 2014-05-27 09:21
1. Fields Data table
ID ROW_ID FIELD_ID FIELD_VALUE
1 1 1 Tom
2 2 1 Michael
3 1 2 2
4 2 2 NULL
(In the Fields Data Table ID 3 -> FIELD_VALUE 2 - I mean it would save ID from Options Table
The most important for me is posibility to work with data in this forms as with normal table, like MS Excel or Podio.
Do you have any idea, any known implementation or any other source to get some information what is the best way to implement it? My structure of tables is just idea I think there are exists better implementation, but I can't find it.
Thank you a lot!
David
I have dealt with a similar situation.
Here is what I did!
Use 3 tables:
Form
Fields
Values
Schema for the tables could be:
Form :
id: integer
name: varchar
Fields :
id: integer
name: varchar
data_type: varchar
field_options: varchar (Here you can store the option values in comma separated format)
form_id: integer
Values :
id: integer
form_id: integer
field_id: integer
value: text/varchar

mysql sort category according to parent id

This is the table structure
id parent_id name
1 0 BMW
2 0 Mercedez
3 0 Porsche
4 1 3 Series
5 2 E60
6 1 5 Series
7 3 Cayenne
How can i show the table as
BMW
3 Series
5 Series
Mercedez
E60
Porsche
Cayenne
The above table shows ascending id followed by parent_id associated with that id, then go to second id and so on. I need in a single query, is it possible to do as such?
Try this:
SELECT
name,
CASE WHEN parent_id = 0 THEN id ELSE parent_id END AS Sort
FROM
cars
ORDER BY
Sort,
id
http://sqlfiddle.com/#!2/9b05f/3
EDIT:
Given that this answer keeps getting upvotes, I revisited the question and found a flaw. If, for some reason, the parent has a higher ID than the child, the ordering gets messed up. The above query only works if the parent ID is a lower number than all the children.
To demonstrate the problem, imagine the table looked like this:
id parent_id name
8 0 BMW
2 0 Mercedez
3 0 Porsche
4 8 3 Series
5 2 E60
6 8 5 Series
7 3 Cayenne
Notice now that BMW has an id of 8 instead of 1. The result will look like this:
Mercedez
E60
Porsche
Cayenne
3 Series
5 Series
BMW
Notice above that BMW shows up at the bottom of the list, after its children! This is because the secondary sorting orders by id, and if the parent ID happens to be higher than any children, the parent may not show up on top of the children.
This query will solve that problem:
SELECT
name
FROM
cars
ORDER BY
CASE WHEN parent_id = 0 THEN id ELSE parent_id END, -- another way of writing it: IFNULL(NULLIF(parent_id, 0), id)
parent_id,
id
http://sqlfiddle.com/#!2/6d6d73/3
To explain what's going on here, you first order by the parent row's id field and the children rows' parent_id field. If you order by that, all the children will be grouped up with their parent, and the overall list will be ordered by the parent's id field.
But that doesn't set the sorting within the families, so the parent could show up anywhere within the family (the parent could show up at the top, or it could be in the middle, or it could be last).
That's where the other two sorting fields come in. The second field sorts by parent_id, and the parent row's parent_id field is always 0. Safely assuming that your ID fields are always positive, this means that the parent record will always show up on top within the family. The rest of the children will all have the same value for parent_id, so the third sorting field orders the children within the family by their id field. This could also be changed to name, depending on how you want the children sorted.

Autoincrement in mysql without unique ids

I have a table with main-ids and user-ids. Each user-id has a set of their own unique main-ids, but multiple user-ids can have the same main-id. Is there anyway to increment a main-id for a specific user without having to do 2 queries?
If you mean like this:
User ID Main ID
1 1
1 2
1 3
2 1
2 2
2 3
Then you're going to need to make an INSERT trigger that finds the next MainID for that user and stores that.

Relational Database Design (MySQL)

I have a table User that stores user information - such as name, date of birth, locations, etc.
I have also created a link table called User_Options - for the purpose of storing multi-value attributes - this basically stores the checkbox selections.
I have a front-end form for the user to fill in and create their user profile. Here are the tables I have created to generate the checkbox options:
Table User_Attributes
=====================
id attribute_name
---------------------
1 Hobbies
2 Music
Table User_Attribute_Options
======================================
id user_attribute_id option_name
--------------------------------------
1 1 Reading
2 1 Sports
3 1 Travelling
4 2 Rock
5 2 Pop
6 2 Dance
So, on the front-end form there are two sets of checkbox options - one set for Hobbies and one set for Music.
And here are the User tables:
Table User
========================
id name age
------------------------
1 John 25
2 Mark 32
Table User_Options
==================================================
id user_id user_attribute_id value
--------------------------------------------------
1 1 1 1
2 1 1 2
3 1 2 4
4 1 2 5
5 2 1 2
6 2 2 4
(in the above table 'user_attribute_id' is the ID of the parent attribute and 'value' is the ID of the attribute option).
So I'm not sure that I've done all this correctly, or efficiently. I know there is a method of storing hierarchical data in the same table but I prefer to keep things separate.
My main concern is with the User_Options table - the idea behind this is that there only needs to be one link table that stores multi-value attributes, rather than have a table for each and every multi-value attribute.
The only thing I can see that I'd change is that in the association table, User_Options, you have an id that doesn't seem to serve a purpose. The primary key for that table would be all three columns, and I don't think you'd be referring to the options a user has by an id--you'd be getting them by user_id/user_attribute_id. For example, give me all the user options where user is 1 and user attribute id is 2. Having those records uniquely keyed with an additional field seems extraneous.
I think otherwise the general shape of the tables and their relationships looks right to me.
There's nothing wrong with how you've done it.
It's possible to make things more extensible at the price of more linked table references (and in the composition of your queries). It's also possible to make things flatter, and less extensible and flexible, but your queries will be faster.
But, as is usually the case, there's more than one way to do it.