Is it possible to make a view in Couchbase for something like id=A OR name=A ?
Thanks.
Cheers,
Yann
No it's not possible to do this on two separate fields within a view. If your query has to be something like:
Select all users where user.id = X or user.name = X
Then you'll need two separate views, one for each field that you want to filter on, then you'll need to combine the results in your application layer.
You can do an OR operation on a single field by passing an array of keys to the view, assuming you have a view which emits the name field then you could pass ["Yann","Peter"] as keys and that field would be matched on either value A OR value B (you can pass more than 2 keys).
Hope that helps!
Related
Here is a View called viewwithcommonfield :
SELECT
`schematopologytest01`.`talpha`.`CommonField` AS `CommonField_tAlpha`,
`schematopologytest01`.`tbeta`.`CommonField` AS `CommonField_tBeta`
FROM
(`schematopologytest01`.`talpha`
JOIN `schematopologytest01`.`tbeta`)
When I execute
SHOW FULL fields FROM viewwithcommonfield IN SchemaTopologyTest01
I get this:
How do I map the fields back to specific tables? Can I write a view against the tables in information_schema?
Here are the table structures that are referenced in the view. The tables share a common field called CommonField:
No, there is no metadata available to map views of a column back to the original column in a base table. That would require multiple tables, because any given expression in the select-list may reference multiple columns from different tables.
Consider:
SELECT CONCAT(
`schematopologytest01`.`talpha`.`AlphaFieldA`,
`schematopologytest01`.`tbeta`.`BetaFieldE`) AS `ConcatenatedField`
FROM `schematopologytest01`.`talpha`
JOIN `schematopologytest01`.`tbeta` ON ...
Which table and column would ConcatenatedField list as its origin? It would have to be stored in two rows of another INFORMATION_SCHEMA table.
There are also select-list expressions possible in a view that don't reference any base table:
CREATE VIEW ViewNow AS SELECT NOW() AS `now`;
What about columns that are themselves scalar subqueries? Or references to stored functions? Or an aggregate function like COUNT() or SUM() where the value is not found in any base table?
Many views do not derive their data from base tables deterministically at all. Edit: What I mean is that it's not always possible to know which rows or columns are the source of data in a view, because they results are combined in some ways. It's probably more clear to say that reversing the query to get the original data is not always possible, depending on the query.
It's not possible to update those views. But if there were metadata about where the data "came from," there would have to be something in the metadata to indicate that. It would be impractical because it would be complex, and have little value.
I need to store some data of list type into a MYSQL database.Is it possible to store it in MYSQL,if yes then what should be the data type of the field that will hold this data?
Thanks in advance.
No, no, no, no, no! Never store multiple values in a single column!
Seperate the values and store each of them in another record.
For instance if you have users and want to store a list of the roles each user has then you can do it like this
users table
-----------
id
name
...
roles table
-----------
id
name
user_roles table
----------------
user_id
role_id
You can use JSON datatype.
MySQL provides JSON as column data type and provides some functions to work with JSON data.
Look at the documentation
NOTE: you must use 5.7+ version
As of MySQL 5.7.8, MySQL supports a native JSON data type that enables efficient access to data in JSON
You can store it as a STRING, using VARCHAR data type column.
There is no specific data type in mysql designed to store specifically lists.
Other approach is to implode the list and store each member in a different row, then when queried, you can recreate the list, or array or JSON, in your preferred language.
you can use SET datatype and this will store predefined data set in the mysql and not in the table and let you select value from the list up to 64 individual items
https://dev.mysql.com/doc/refman/8.0/en/set.html
You could create a table with two fields - the first being the list name and the second being the list items. This table would be like a join between the list and its items. Each list name would appear in the table as many times as there are items in the list. Each list item would appear in the table as many times as the number of lists it belongs to.
For example, say you want to store data about teams. Let's say that any person can be a member of more than one team. If Bob is on Team Blue and on Team Orange then there would be two records that include Bob - one where he's paired with Team Blue and one where he's paired with Team Orange. And if there are 10 people on Team Blue then there would be 10 records where Team Blue is mentioned - one for each member of Team Blue.
I think this solution does not follow the rules of normalization but it would be a practical, workable solution, wouldn't it?
I have an Activity Feed Module where i am storing Activity that's happening in a Company that has multiple members. The Activities are usually specific to USERs based on their Role. So i want to store the Users whom the activity should be shown to inside the table column so that while fetching the activities i do not have to query the users associated with that activity which would slow down the Data retrieval process.
since MySQL does not have array data type which is the best way to do it?
Right now i have
activities(id, category, action, date)
activity_users(id, activity_id, user_id)
Is This easier to query the activities which are based on the particular type of users that to add a column users and story an array [1,3,5,8] and try to query that from the activities table
Yes, you can use serialize() / unserialize() but you cant fire query on that fields the if need anything specific from that serialized data ( you ll have to fetch all , unserialize and then can perform search ) OR you can save data comma separated ( you can perform any query on that ) OR you can get seprate field for each data
If your code in <?php you can use json_encode()/json_decode(), serialize()/unserialize() and write string as normal. But it will be hard to build right condition for select this kind of data. If u need to find somthing use %LIKE% - not always helps. I suggest you to create separate fields for each kind of data.
I'm trying to do it like this:
Every single user can choose fields (like structures on MySQL) where this fields can handle their respective value, it's like doing a DB inside a DB.
But how can I do it using a single table?
(not talking about user accounts etc where I should be able to use a pointer to his own "structure")
Do something like: varchar Key where register something like "Name:asd" where PHP explode : to get the respective structure ('name' in this case) and the respective value? ('asd')
Use BLOB? can someone turn the light on for me? I don't know how to do something where works better than my current explanation...
I know my text is confuse and sorry for any bad english.
EDIT:
Also, they could add multiple keys/"structures" where accepts a new value
And they are not able to see the Database or Tables, they still normal users
My server does not support Postogre
In my opinion you should create two tables.
with the user info
with 3 fields (userid, key and value)
Each user has 1 record in the first table. Each user can have 0 or more records in the second table. This will ensure you can still search the data and that users can easily add more key/value pairs when needed.
Don't start building a database in a database. In this case, since the user makes the field by himself there is no relation between the fields as I understand? In that case it would make sense to take a look at the NoSQL databases since they seem to fit very good for this kind of situations.
Another thing to check is something like:
http://www.postgresql.org/docs/8.4/static/hstore.html
Do not try to build tables like: records, fields, field types etc. That's a bad practice and should not be needed.
For a more specific answer on your wishes we need a bit more info about the data the user is storing.
While i think the rational answer to this question is the one given by PeeHaa, if you really want the data to fit into one table you could try saving a serialized PHP array in one of the fields. Check out serialize and unserialize
Generates a storable representation of a value
This is useful for storing or passing PHP values around without losing
their type and structure.
This method is discouraged as it is not at all scalable.
Use a table with key-value pairs. So three columns:
user id
key ("name")
value ("asd")
Add an index on user id, so that you can query a user's attributes easily. If you wanted to query all users with the same properties, then you could add a second index on key and/or value.
Hope you are using a programming language also to get the data and present them.
You can have a single table which has a varchar field. Then you store the serialized data of the field structure and their value in that field. When you want to get the structure, query the data and De-serialize that varchar field data.
As per my knowledge every programming language supports serialization and De-serialization.
Edited : This is not a scalable option.
I have simple problem. In my table have columns ID, NAME, CONTENT, TIMESTAMP. If i use session.query(table).all() result is array of table class objects. So i have no problem with modify one or more objects and update, or use this objects in associations. But i need only columns ID and NAME. If use session.query(table.id, table.name) i get as result tuple with id and name. Using this tuple in update it's - maybe - easy, but code it's.. ugly. As same in associations. And there's is unnecessary database load if i want only ID to make association.
Until now i use simple code to get array of table class objects querying only specified columns:
for row in session.query(table.id, table.name).all():
data.append(table(id=row.id, name=row.name))
It's make updates easy, but using in associations is complex. For example, i get 10 rows in array of table objects from my code. I want association 1 row to table2.
If i use:
session.add(table2(name = 'test', association = data[1])) and commit,
sqlalchemy want to create new table2 row, new association table row and new table row (this first, associated to table2).
It's there any way to get result as array of table objects, not tuples, with specified columns, wheres returning objects is same if make query(table)?
Sorry for my English anyway, i never used it to describe complex problem, until now.
If you want to reduce the number of columns loaded from db with .query(Table), thus reducing the overhead, you might want to try defering the unneeded columns when defining the relationship. Say id and name are essential for your updates and info is not, you can tell the mapper to defer info:
mapper(Class, table, properties={
"info":column_property(table.c.info, deferred=True),
... other properties ....
})
So, when you do session.query(Class), info won't get picked on select. It will be called only if somewhere in the code you have obj.info, that is, when you explicitly call for this attribute.