Using Mysql Set Field Type - mysql

I want to implement a badge modification to my website, i figured that I would have one new column in my mysql database to store number variables in them, and then i can display badges based on what number is in the mysql column for each user, should i use the SET field type to accomplish this?

I would suggest you create two tables:
badges, containing the details of each badge;
userbadges, containing a pair of foreign keys - that of a user and that of a badge owned by that user.
This has the advantage over SET of enabling you to add/delete/change badges without altering your schema and also to reference badges from other tables with minimum duplication.

Related

SQL: How can I add multiple items inside a column?

Suppose I have a table called users. Inside users there are three columns: username, password and images. In the 'images' column the user should be able to upload images from the program as BLOBs and reaccess them as needed. So there should be multiple image items inside one row in the 'images' column per each user. The program will be in java jdbc.
This seems like a good time to use a different table. For example, adding a new table UserImages with columns of UserImageID, Username, Image with a foreign key on the Username and primary key on UserImageID lets you create a many-to-one mapping of images to users, which is what you're looking for.
Instead, if you put multiple (and of variable length) entries inside a single entry, then that defeats the purpose of database rdbms design. For more information, take a look at the first normal form of database. https://en.wikipedia.org/wiki/First_normal_form

Whether a table which stores data about single item should grow horizontally or vertically?

Suppose I have a user table that stores the data of single user.
Initially we know nothing about the user, so there is nothing in the table(may be only single column like id which is of no use in this case ).
We do not know what are the details we are going to have about the user and we do not know in which order we are getting the details. Details about user will be obtained gradually in any order.
My question is ,For Example, if I got the name of user, how should I enter it in the table?
I have two options
1) Alter the table structure and add a column called username and store the data there. For all new detail, this process is repeated. So all data will be in one row.
2)Alter the table structure and add to columns key and value. Give name as a key and store the name of user as its value. Thus for each detail about the user,a new row is inserted as key value pairs.
First method makes the table grow horizontally.
Second one make it grow vertically.
which one is good on the basis of good design methods and ease of querying?
If you expect the metadata associated with a user could become arbitrarily large, then adding columns probably isn't the best approach. So this would leave your suggestion to simply add key/value pairs for each new feature associated with a user. There is a third option, which I don't like for so many reasons, which would be to store JSON containing key/value pairs in a single column of the user table. We currently use this approach sporadically, but we handle the JSON manipulation in our Java app layer, which is relatively painless. From a pure database point of view, this isn't so desirable.
So I would vote for your second option of using key/value pairs, because it would scale well. Note that this does not imply that your user table would only have a single column. You might know that a certain number of user attributes will always be there, e.g. username, hashed password, etc., and these columns could be added at the beginning.
Building on what others have already said, you could use a hybrid approach as well. If there are any predefined columns (username, firstname, lastname, password, etc.), you could put those in a table with defined fields, and then link a second table with key/value pairs for additional data.

How to use "central columns" in phpmyadmin?

PMA has the tools for adding the central columns. As far as I understand, it's used with foreign constraints. I have two tables: TableA and TableB.
Structure of TableA: id_of_A, name_of_A_value, ...
Structure of TableB: id_of_B, foreign_id_of_A,... and foreign constraint from foreign_id_of_A to A-table.id_of_A.
And it's very difficult to select needed foreign_id_of_A while insert new row into TableB because only value of id_of_A is visible. Could central columns help me with this problem? How central column works?
Central Column
You can add/remove columns to the list as per your requirement. These columns in the central list will be available to use while you create a new column for a table or create a table itself. You can select a column from central list while creating a new column, it will save you from writing the same column definition over again or from writing different names for similar column.
To allow the usage of this functionality:
set up $cfg['Servers'][$i]['pmadb'] and the phpMyAdmin configuration
storage
put the table name in $cfg['Servers'][$i]['central_columns'] (e.g. pma__central_columns)
This feature can be disabled by setting the configuration to false.
Perhaps the feature that you are looking for is the "Display Column" under "Relation View" in the table structure view. This allows you to choose what column is shown in the PHPMyAdmin interface regardless of what is stored in the database foreign key constraint.
If the line is not shown than you need to enable the feature in PHPMyAdmin. See the FAQ Entry and relevant configuration option entries: $cfg["servers"][$i]["relation"] and $cfg["servers"][$i]["table_info"] (in the previous link, at the time of posting (I don't have enough reputation to post more than 2 links)).
Essentially you create a couple of tables in a database that PHPMyAdmin can use to store additional data and then tell it which tables these are. Once this is done PHPMyAdmin will let you choose which columns to display (on mouse over of the linked column, and while editing data). If you installed it on a Linux distribution via a package manager it should have already created these tables for you (or at least asked you if you wanted to), if it did not, re-installing the package may help. I don't have any experience with installing it on Windows or Mac OS X, but I would presume that there is some automated method of creating these tables for those platforms.
(On Debian and Debian-based Linux Distributions (such as Ubuntu), the extra database is configured with db-config-common. If it was not configured at install time, dpkg-reconfigure phpmyadmin will allow you to set up the database (please note that this might destroy some user/system preferences in PHPMyAdmin).)
I've found here is an exhaustive description:
As the name suggests, the Central columns feature enables to maintain a central list of columns per database to avoid similar name for the same data element and bring consistency of data type for the same data element. You can use the central list of columns to add an element to any table structure in that database which will save from writing similar column name and column definition.
To add a column to central list, go to table structure page, check the columns you want to include and then simply click on “Add to central columns”. If you want to add all unique columns from more than one table from a database then go to database structure page, check the tables you want to include and then select “Add columns to central list”.
To remove a column from central list, go to Table structure page, check the columns you want to remove and then simply click on “Remove from central columns”. If you want to remove all columns from more than one tables from a database then go to database structure page, check the tables you want to include and then select “Remove columns from central list”.
To view and manage the central list, select the database you want to manage central columns for then from the top menu click on “Central columns”. You will be taken to a page where you will have options to edit, delete or add new columns to central list.
http://lees.geo.msu.edu/phpmyadmin/doc/html/faq.html
May be it's useful for foreign constraints modelling, but absolutly useless in usage. I'm still looking for a solution for adding value into one table by the ID from another table so that it was understandable for people, not for computers. If you know how to do that, let me know.

How to store a set of values from another table in a field in MySQL

I'm running on a version of MySQL that does not support foreign key relationships.
Suppose I have two tables, one holds a number of users while the other one holds a number of topics. I want each of the topics to have a field which holds a set of user id's who participated in that topic. I read the type SET's documentation and it says it must be values from predefined values. So how should I go about doing this?
You don't want an extra field, this is a very inefficient way of storing such things. You want a new table.
In your third table (called, say, topicUsers). you would have just two fields: userId and topicId. Then you can look at this table and join data from the others as needed.
This is called normalisation

Alter MediaWiki's user table

How can I change the default MySQL table "user" , that's used to store all members?
is it more of a database question? in mysql you could issue command, for example:
ALTER TABLE user ADD COLUMN user_birthday DATE;
check the actual name of user table. MW uses table prefix option which may not be empty.
The actual statement will depend on what column you want to add of course.
However, for the better portability of your wiki (e.g. easier upgrade) you might want to create a new table instead for the user profile that would have user_id as FOREIGN_KEY
Also there is a field user_options which stores name=value pairs of extra data. - you can make use of that if you don't care about searching your DB against the new "field".
Do not.
If you need to alter the user table (and related) server-side, use the appropriate maintenance script: createAndPromote.php is among the most used, there are others for specific functions.
You didn't specify your use case, but chances are that you're going to break MediaWiki in unexpected ways if you try to manually alter the table.
Here is some information about the "user" table.