"where" or "like" clause is better for using index - mysql

I have a table with columns of type SET e.g SET('abc','def','ghi') wich store the data like "abc,ghi" and I index these columns. So when I want to find "def" or "ghi" I have to use LIKE "%def%" but I read about "%" that if you use it as first character mysql doesn't use index for search. Waht should I do? should I change the type to enum and store each value in separate row with an ID like this:
+---------+
| column |
+---------+
| abc |
| abc,ghi |
| abc,def |
| ghi,def |
+---------+
change to:
+----+--------+
| ID | column |
+----+--------+
| 1 | abc |
| 2 | abc |
| 2 | ghi |
| 3 | abc |
| 3 | def |
| 4 | ghi |
| 4 | def |
+-------------+
or is there any thing to manipulate index to store each word separately?

The correct function to find an item in a set is FIND_IN_SET. Sets are stored as bit maps, not as strings, and FIND_IN_SET will not have to convert it to a string before matching like LIKE would. But it still won't be able to use an index.
Your second schema is the proper way to normalize the data. You can put an index on the column column, and queries that look for a value will be efficient. Whether to use an ENUM or VARCHAR for this column is a subject of intense debate within the database community.

Related

What Should be database structure to create excel sheet like view? mean should tables store in json format or create tables for cells,rows and columns

Hi Everyone,
i want to create a board and all board will contain groups and each groups have tables(Rows and Columns), so should i save tables(Rows and Columns) as a json format or create separate table for rows,columns and cells etc?
i watn to create a system like monday.com
Depends on what kind of database you are using, if its a document database then JSON is the natural way, in a SQL database, the better way would be to have a table representing the rows, and having a separate table representing column mapping, this will give you the flexibility to add columns at will.
For example:-
Row Table
| id | details |
|----|---------------|
| 1 | row_1_details |
| 2 | row_2_details |
Column Table
| id | column_name | column_value | row_id |
|----|-------------|--------------|--------|
| 1 | col_1 | skjdjks | 1 |
| 2 | col_2 | jslkds | 1 |
| 3 | col_1 | dhkshd | 2 |

How to update specific value without updating a whole value in MySQL

I have a table like this
+-----+------------------+
| id | name |
+-----+------------------+
| 1 | John;Black;Mike |
+-----+------------------+
| 2 | White;Mike;John |
+-----+------------------+
| 3 | Jacob;Mike |
+-----+------------------+
| 4 | Will;Mason;Mike |
+-----+------------------+
as result of
SELECT * FROM people WHERE name LIKE '%Mike%';
Is there any query on how to update specific name Mike to Michael without updating a whole value. like John;Black;Mike to John,Black,Michael in all rows automatically.
You could use replace
update people
set name = replace( name, 'Mike', 'Michael')
where name LIKE '%Mike%';
anyway you should avoid storing comma separated value .. you should think to a proper normalized table for this data ..

get first 3 alphanumeric characters (only numbers or letters)

I have a table which holds a field, title, I need to get first 3 alphanumeric characters of each title. Some of the values of title have ",',\t,\n, or whitespace prepended - this should be ignored.
+--------+-----------------------------------------+---------------------+
| id | title | desired output |
+--------+-----------------------------------------+---------------------+
| 1 | "abcd" | abc |
| 2 | 'lostworld | los |
| 3 | \tsonof | son |
| 4 | 12amrt | 12a |
+--------+-----------------------------------------+---------------------+
desired output is the output I am looking for. If anyone can suggest generic query which can handle all cases that would be great.
Looking for solution using MySQL only.
Your best bet is to use a regex user-defined function.
The built-in regexp functions only support matching; not string replacing like you want here

How to split CSVs from one column to rows in a new table in MSSQL 2008 R2

Imagine the following (very bad) table design in MSSQL2008R2:
Table "Posts":
| Id (PK, int) | DatasourceId (PK, int) | QuotedPostIds (nvarchar(255)) | [...]
| 1 | 1 | | [...]
| 2 | 1 | 1 | [...]
| 2 | 2 | 1 | [...]
[...]
| 102322 | 2 | 123;45345;4356;76757 | [...]
So, the column QuotedPostIds contains a semicolon-separated list of self-referencing PostIds (Kids, don't do that at home!). Since this design is ugly as a hell, I'd like to extract the values from the QuotedPostIds table to a new n:m relationship table like this:
Desired new table "QuotedPosts":
| QuotingPostId (int) | QuotedPostId (int) | DatasourceId (int) |
| 2 | 1 | 1 |
| 2 | 1 | 2 |
[...]
| 102322 | 123 | 2 |
| 102322 | 45345 | 2 |
| 102322 | 4356 | 2 |
| 102322 | 76757 | 2 |
The primary key for this table could either be a combination of QuotingPostId, QuotedPostId and DatasourceID or an additional artificial key generated by the database.
It is worth noticing that the current Posts table contains about 6,300,000 rows but only about 285,000 of those have a value set in the QuotedPostIds column. Therefore, it might be a good idea to pre-filter those rows. In any case, I'd like to perform the normalization using internal MSSQL functionality only, if possible.
I already read other posts regarding this topic which mostly dealt with split functions but neither could I find out how exactly to create the new table and also copying the appropriate value from the Datasource column, nor how to filter the rows to touch accordingly.
Thank you!
€dit: I thought it through and finally solved the problem using an external C# program instead of internal MSSQL functionality. Since it seems that it could have been done using Mikael Eriksson's suggestion, I will mark his post as an answer.
From comments you say you have a string split function that you you don't know how to use with a table.
The answer is to use cross apply something like this.
select P.Id,
S.Value
from Posts as P
cross apply dbo.Split(';', P.QuotedPostIds) as S

How to make a field of a DataBase as array?

I want to have a column in a database that can contain multiple entries. Is it possible to have to define the type of the column as an array (fixed-sized array or some dynamic collection) so that it can store multiple entries.
If you require various values to be stored together, in a single field, then you will likely be best off storing them as a delimiter-separated string of values:
+----------------------------------+
| PRODUCTS |
+----------+-----------------------+
| Product | Colors |
+----------+-----------------------+
| Notebook | blue,red,green,orange |
+----------+-----------------------+
This is usually not what youw want though. Generally-speaking, the idea solution is to create relationships between tables. For instance:
+---------------+
| PRODUCT |
+----+----------+
| ID | Product |
+----+----------+
| 1 | Notebook |
+---------------+
+---------------+
| COLORS |
+----+----------+
| ID | Color |
+----+----------+
| 1 | Blue |
+---------------+
| 2 | Red |
+---------------+
| 3 | Green |
+---------------+
+---------------------+
| PRODUCTCOLORS |
+-----------+---------+
| ProductID | ColorID |
+-----------+---------+
| 1 | 1 | Notebook, Blue
+-----------+---------+
| 1 | 3 | Notebook, Green
+-----------+---------+
yes, in a typical relational design, you would have a 1:N (1-to-many) relationship between 1 table and another. each row in the first table represents a collection, each row in the second table is an element in a collection and references the first table.
a comma-separated list, serialize, or a url-encoded string is also a good solution as the other answers point out...
No, but what server side language are you using?
If using PHP you can use
$serializedArray = serialize($myArray);
And then insert that value into the db. To get it back out use unserialize();
This is pretty much the same answer as above (have a delimited string), but you could also save the text in that column as XML. Depending on the database you are using, that could be easy or tedious.
As pointed out above, is you obviously lose any aspect of being able to manage the data integrity from your DB layer (easily).