According to "CQL for Cassandra 2.x Documentation", the author for storing a playlist, creates table as follows:
CREATE TABLE songs (
id uuid PRIMARY KEY,
title text,
album text,
artist text,
data blob
)
CREATE TABLE playlists (
id uuid,
song_order int,
song_id uuid,
title text,
album text,
artist text,
PRIMARY KEY (id, song_order ) );
But how does he get song_id? Does the app read the songs table to get the id and use that in this table when doing an insert? (I am trying to learn cassandra and all the examples on the net talks about having different tables to satisfy my query but no where they mention how should I take care of inserting the same data in all tables at a time).
Regards,
Seenu.
I'd imagine the app doing the insert would already know the song id. Think about the use case - a use is adding a song to a playlist. Think about the interface - the user searches for songs and these are displayed in some form of list view. The user selects a song to add to a play list. At that point, the app knows the id of the song. The apps puts the selected song into the appropriate slot in the playlist (I imagine that's where song_order comes in).
Related
Sorry for the title I really don't know what to call it.
I am building a illustration website with albums.
A illustration can be uploaded to a album.
In the photos table I have a processed table in it.
This way I can check if the illustration was processed or not, if yes then it can be searchable if not then the user must edit the processed illustration or illustrations.
I can select the photos in the specific album but I don't know how to filter with the processed illustration belonging to the viewing album.
Also I have a seperate table album_photos table.
The relation of a album and a photo is hasMany and belongsToMany.
This way I select all the photos but not filtered with processed or not.
$photos = Photo::whereIn('id', $album -> album_photos() -> pluck('photo_id'))->get();
And this way I select all the processed photos of the corresponding user.
$photosNotProcessed = Photo::where('user_id', Auth::user() -> id)->whereNull('processed')->get();
Now I want to select the photos in the viewing album that is not been processed.
Any ideas?
Thanks in advance.
i found you have 3 table photo, album and a table that call processed!
photo and album have many to many relation and photo with processed table has a relation too, if you want select processed photos that belog to a album, because of that you can add a where in you photo table:
Photo::where('user_id', Auth::user() -> id)->whereNull('processed')->whereIn('id', $album -> album_photos()) -> pluck('photo_id'))->get();;
i'm trying to get extra column as foreign key in laravel many to many relationship..here is my table structure
idcards
id,
name,
quality
id,
name
idcard_quality
id,
idcard_id
quality_id
related_id
in above idcard_quality table i want add extra foreign key related_id, i wanted to retrieve result like every idcard hasmany qualities and that quality is another idcard.. suppose one idcardcard has one normal quality and that normal quality is different idcard...
please help me
I think your table design should look like this:
idcards
id,
name
quality
id,
name,
related_id
idcard_quality
idcard_id
quality_id
This way when you retrieve the values of the parent/related id card on joining the tables.
Scenario: I have to create a database which has to contain 3 different tables holding information about people. Lets call them Members, Non_Members and Employees. Among the other information they may share , one is the telephone number. The phone numbers are unique, each in its respective table.
My problem: I want to make sure the phone number is always unique among these 3 tables. Is there a way to create a validation rule for that ? If not and I need to redesign the database, which would be the recommended way to do it.
Additional info: While the 3 tables hold the same information (Name , address etc.) its not required always required to fill them. So I am not sure if a generic table named Persons would work for my case.
Some ideas: I was wondering if and how I can use a query as a validation rule (that would make things easier). If I would end up creating a table called Phone numbers , how would the relations between the 4 tables would work in order to ensure that each of the 3 tables has a phone number.
ERD
I assume you are talking about a relational database.
I would go for a single person table with a "type" column (member, non_member, ...). That is much more flexible in the long run. It's easy to add new "person types" - what if you later want a "guest" type?
You would need to define as nullable to cater for the "not all information is required" part.
With just a single table, it's easy to make the phone number unique.
If you do need to make it unique across different tables, you need to put the phone numbers in their own table (where the number is unique) and the references that phone_number table from the other tables.
Edit
Here is an example of creating such a phone_number table:
create table phone_number
(
id integer primary key,
phone varchar(100) not null unique
);
create table member
(
id integer primary key,
name varchar(100),
... other columns
phone_number_id integer references phone_number
);
The tables non_member and employee would have the same structure (which is a strong sign that they should be a single entity)
Edit 2 (2016-01-08 20:12)
As sqlvogel correctly pointed out, putting the phone numbers into a single table doesn't prevent a phone number to be used by more than one person (I misunderstood the requirement so that no phone number should be stored more than once)
I have an app that houses product data via a Product model and table. Each product has specific state availability (multiple states) that I will need to filter and/or search by in the future. I am hoping to find someone who can tell me the most efficient way to store this data. As I see it, I have two options.
The first is to simply create 50 columns in my table, titled with each state name and containing a boolean value. I can then simply filter by = "avail in California" if product.ca. While this certainly works, it seems a bit cumbersome, especially when searching for multiple state availability.
The second option would be to simply have one column("states") that stores an array of available states and then filter by = "avail in California" if product.states.include? "CA". This seems like a better solution for two reasons. The first, it just allows for a cleaner DB table. Second, and more important, I can allow my user to search by simply saving the user's input as a variable(user_input) and then = "avail in California" if product.states.include? user_input. This solution does call for a little more work up front however when saving the product in the DB, since I won't be able to simply check off a boolean value.
I think option two makes the most sense, but am hoping for some advice as to why or why not. I have found a few similar questions, but they do not seem to explain which solution would be better, just how to accomplish each.
What should I do?
You should normalize unless you have a really good reason not to, and I don't see one in your overview.
To normalize, you should have the following tables:
product table, one record per product
state table, one record per state
product_state table, one entry for every product that is in a state
The product_state schema looks like this:
(product_state_id PK, product_id FK, state_id FK)
UNIQUE INDEX(product_id,state_id);
This allows you to have a product in zero or more states.
I assume that since you’re selling products, you will be charging taxes. There are different taxes by state, county, city. There are country taxes in some countries too.
So you need to abstract these entities into a common parent, usually called GeopoliticalArea, so that you can point a single foreign key (from, say, a tax rates table) at any subtype.
create table geopolitical_area (
id bigint primary key,
type text not null
);
create table country (
id bigint primary key references geopolitical_area(id),
name text not null unique
);
-- represents states/provinces:
create table region (
id bigint primary key references geopolitical_area(id),
name text not null,
country_id bigint references country(id),
unique (name, country_id)
);
insert into geopolitical_area values
(1, 'Country'),
(2, 'Region');
insert into country values
(1, 'United States of America');
insert into region values
(2, 'Alabama', 1);
I have been able to make 2 ActiveRecord tables, Profile and Bot. I have been unable to figure out how to link them properly.
There are thousands of Profiles with columns username, gender. A handful of Bots with columns botname, level
When a bot visits a profile two pieces of info need to be recorded. visited and response should be updated for that specific bot. visited is a boolean that will indicate that one particular bot has visited that one particular profile. the response is a string, again like the visited this is a response for one particular bot that was sent by one particular profile. I am thinking I need a 3rd table that joins these two tables.
I need to keep a record of every profile that every bot visits and the response that happens when it visits.
How can I create this relationship and how can I set/update the columns?
Thanks
I'm not completely certain of your requirements, so I will restate them:
Table Profile: id, username, gender (note that I changed the table names to singular)
Table Bot: id, botname, level
The "bots" somehow "visit" profiles. You need to track when a bot has visited a profile.
When a bot visits a profile, a "response" string is generated and that response string needs to be preserved. I'm assuming it needs to be preserved with the record of the visit.
I think your instincts about a join table are good. I don't think a boolean "visited" column works, however, because if you have a record of a visit, that's an indication that the profile was visited. If the record doesn't exist, then it wasn't visited.
Given this, I think your tables look like this:
profile
---------
profile_id integer autoincrement
username varchar(255)
gender ...
bot
---------
bot_id integer autoincrement
name varchar(255)
level ...
visit
---------
visit_id integer autoincrement
bot_id integer
profile_id integer
visit_time datetime
response varchar(255)
To maintain the integrity of your data, you'll want to set up foreign key constraints between this visit table and your profile and bot tables.
alter table visit
add constraint visit_profile_profile_id_fk
foreign key (profile_id)
references profile (profile_id);
alter table visit
add constraint visit_bot_bot_id_fk
foreign key (bot_id)
references profile (bot_id);
You'll need to decide if it's "legal" for a given bot to visit a particular profile more than once. If it's not, you should put a unique constraint on the combination of profile_id and bot_id in the visit table, and catch the duplicate key errors when your DBMS throws them at you (or otherwise handle dupes.)
I hope that helps.