Is it possible to create a sqlite table with a column having multiple value? - sqlite

For example, Assume i have a table with employee list.
The employee id is the primary key, i have other details of employees such as name, age etc.
The employee can have multiple phone numbers. So, it becomes a multivalued attribute.
We don't know how many phones(contact numbers) the employees has.
Is it possible to have multivalued attribute in sqlite3?
Or is there is any method to accommodate this?
Thanks.

You can accomplish this with following ideas:
create 1 column to store all phone numbers (which makes querying based on numbers difficult, not recommended)
you can create x columns for each number (phone1, phone2, ...) (you need to decide what'll be the maximum number of phone numbers for each user)
You can create separate table for phone numbers and link this table with employee table with foreign key. This allows you to store varying list of phone numbers for each employee (requires new table).
Option 3 seems to be most flexible, however in most cases I've seen there was option 2 implemented (usually people have limited number of phone numbers).

You also could create a serializable custom class to store multiple contact numbers. You can then serialize the custom class into a byte[] and then extract an ASCII string from the byte[]. You'll be able to store this string of multiple contact numbers, but you will have to deserialize the data after reading it back in from the database ( reversing the process below.)
public class Contacts : ISerializable
{
}
if (Contacts is ISerializable)
{
string contactNumbers = String.Empty;
using (var stream = new MemoryStream())
{
var formatter = new BinaryFormatter();
formatter.Serialize(stream, Contacts);
stream.Close();
var bytes = stream.GetBuffer();
contactNumbers = System.Text.Encoding.ASCII.GetString(bytes));
}
//Perform the update for this column or add to your insert query.
}

This is the wrong approach in relational database terms. Instead, you should create a separate table to hold employee phone numbers. The table will have columns employee_id and phone_number. Each employee may have 0 or more records in this table, depending on how many phone numbers you have on file.
You may also want to include a description column so that you can have information about what each phone number is.
Storing multiple values in a single column is a first-order no-no in relational databases. Please don't do it.

Multivalued fields are considered very bad practice in DB Design as it shows a lack of "normal form". Instead you should:
Create a new EmployeePhoneNumber table with 3 columns. EmployeePhoneNumberId, EmployeeId, PhoneNumber and PhoneType.
Add a constraint to restrict the Type column to be one of (Mobile, Home ..).
Now you can add any number of phone numbers, by type, to the new table. query it by employeeId and type, if needed.
This option is not only more flexible but it will save db storage size as you will only allocate storage for phone numbers that actually exist.

Related

Using "OR" and selection by another table model in DynamoDB

I will explain my question with a concrete example. I have single DynamoDB table (for this example). Table is consisting by the two models:
- user: {
firstname
lastname
placeId
typeId
}
// List of favourites for each users
- userFavourites {
userId
favouriteId
favouriteType
}
I would like to effectively find users, by the following rule:
placeId = 'XXX' OR typeId = 'YYY' or user have any favourite with favouriteId: 'ZZZ' and favouriteType: "Dog" OR user have any favourite with favouriteType: "Cat"
I'm using onetable for communication with dynamo: https://doc.onetable.io/start/quick-tour/
Is it possible to do this kind of selection in DynamoDB (with multiple OR and selection by items from another model in same table) and everything together in one rule?
To be efficient with your reads you must do a GetItem or Query which means you have to provide the partition key for the item, that means you cannot do an OR with the native APIs.
You can however do an OR using PartiQL ExecuteStatement where you can say:
SELECT * FROM MYTABLE WHERE PARTITIONKEY IN [1,2,3]
Again this is only useful when it's the partition key.
If you are looking for OR on multiple different values then I suggest perhaps using a more suitable database with more flexible query capability, as to do so with DynamoDB would resul in a full table Scan each time you need a single row/item.

DynamoDB - query based on multiple columns

I have a requirement to find all users in a table that have same Id, Email or Phone.
Right now the data looks like this:
Id //hash
Market //sort
Email //gsi
Phone //gsi
I want to be able to do a query and say:
Get all items that have matching Id, email or phone.
From the docs it seems that you can only do a single query based on keys or one index. And it seems that even if I was to combine phone and email into one column and GSI that column I would still be limited to a begin with filter expression, is this correct? Are there any alternatives?
it seems that you can only do a single query based on keys or one index
Yes.
if I was to combine phone and email into one [GSI] I would still be limited to a begin with filter expression, is this correct?
Essentially, yes. Query constraints apply equally to indexes and the table keys. You must specify one-and-only-one Partition Key value, and optionally a range of Sort Key values.
Are there any alternatives?
Overload the Partition Key and denormalise the data. Redefine the Partition Key column (renamed PK) to hold Id, Email and Phone values. Each record is (fully or partially) repeated 3 times, each time with a different PK type.
PK Market Id More fields
Id-1 A Id-1 foo
zaphod#42.com A Id-1 # foo or blank
13015552572 A Id-1 # foo or blank
Querying PK = <something> AND Market > "" will return any matching id, email or phone number value.
If justified by your query patterns, repeat all fields 3x. Alternatively, use a hit on a truncated email/phone record to identify the Id, then query other fields using the Id.
There are different flavours of this pattern. For instance, you could also overload the Sort Key column (renamed to SK) with the Id value for Email and Phone records, which would permit multiple Ids per email/phone.

Is it correct to dynamically add rows to a table (in sqlite), or should I use csv?

Just like when adding a new contact in the ios contact app, (like this) you can add multiple phone numbers, and it doesn't know how many there will be, so therefore they don't know how many rows they should initially add in the table of sqlite.
So how does that work, do they dynamically add rows to the table, or do they use csv?
(I'm using objective c)
You have one table for the contacts:
CREATE TABLE contacts(
id INTEGER PRIMARY KEY,
name VARCHAR(50)
);
and you link multiple rows in a separate table to the contacts
CREATE TABLE phone_numbers(
id INTEGER PRIMARY KEY,
phone_number VARCHAR(15),
contact_id INTEGER REFERENCES contacts(id)
);
When you create a contact, you add a row to contacts. When you add a phone number, you add it to phone numbers and refer to the row in contacts. You can add as many rows as you want to phone numbers for the same contact.

How to store Multiple radio button and check box values in sqlserver database using Asp.net?

I am making database using sqlserver2008. Which data type I should choose? In php I use ENUM and in value field I put like that 'Male','Female'. what is the way in sqlserver to create radio button and check box field? And how to insert these in sqldatabase?
I have two gender radio button with different ids so I apply check before insertion i.e.
connection.open();
string gen = "";
if(rdm.Checked == true)
{
string gen ="Male";
}
else if (rdf.Checked == true)
{
string gen = "Female";
}
and in insert value field I just put +gen+ but I receive error: Error 1 A local variable named 'gen' cannot be declared in this scope because it would give a different meaning to 'gen', which is already used in a 'parent or current' scope to denote something else
I usually store dotnet Enums as integers (smallint) in SQL server, sometimes when the database is queried directly by users I use varchar values for readability. Both can easily be converted back to Enums.
I will commonly create a code table in the database to represent my .net enums. Then the associated field in the main data table is just an int with a FK constraint to the code table.
This allows for compact storage and transmission of the data, ensures that no out of range values are entered into the database, and if anyone needs to query the db directly they can join to the code tables to find out what the integers mean.

SQLite - storing multiple values

how can I store and retrieve in SQLite database multiple values for the same row of the same column?
i.e. I have a product column and another column is stores, where I put in all the stores, where it is possible to get this product:
Product: iLamp;
Stores: River's; McWay; Lonnie's; ...
How can I implement this?
Thank you in advance.
If you're smart, you won't do this. Because when it comes time to figure out which stores stock the item, your queries will be hideously deformed. So will those of your stock control application when they try to insert and delete stores. What you'll end up with is what I like to call SQL gymnastics, spending more and more time trying to do SQL in the most bizarre way, simply due to a bad design choice.
Seriously, store these in different rows in the database, as Codd intended.
It's far easier (and faster in terms of the DBMS grunt) to combine multiple rows into a single semicolon-separated string than to break that string into elements.
A schema such as this would suffice:
Products:
ProdCode integer primary key
ProdDesc varchar(50)
Stores:
StoreCode integer primary key
StoreDesc varchar(50)
StockLevels:
StoreCode integer \
ProdCode integer / primary key
Count integer
like others have mentioned, you could store it as a comma separated string, and then put that in your database, but like ocdecio mentioned, the way you have your tables right now is bad a design. You should consider doing something like adding another table, like a PRODUCT_TO_STORE table that has two columns, one has the key of a product, and the other column has the key to a store. Then there is a relationship between products and stores, and helps normalize your data, which in most cases, is a good thing. Then when you need to find all the stores a product is in you could just perform a query on that PRODUCT_TO_STORE table.
I would use something simple like JSON. However this is bad db design since it is not normalized.
change the schema.
do not store multiple values in the same row's column.
add a new table where the multiple values can be stored in their own rows
bad table deisgn:
parents:
ParentID, values, other columns
good table design:
parent
parentID, other columns
child
parentID, childID, value
Either store as a comma-separated list for each store or add multiple rows one for each pair "Store"-Product".

Resources