check Primary key in sqldf package - sqldf

While creating a database using the sqldf package, I created a table named Company Master. I want to link it to different tables but before doing that i wanted to check if the format is correct it or not. In MySQL it tells immediately if the format is correct or not when enter is pressed. Is there any way in rstudio to do the same before proceeding so as to not come back if the process has gone wrong.
library(sqldf)
first <- dbConnect(SQLite(), dbname= "DATA.sqlite")
dbSendQuery(conn = first,
"CREATE TABLE COMPANY MASTER
(
CompId INTEGER,
CompName TEXT,
Address TEXT,
DirectorName TEXT,
EmployeeNo INTEGER,
PRIMARY KEY(CompName)
)")

Related

In Cassandra does creating a tables with multiple columns take more space compared to multiple tables?

I have 6 tables in my database each consisting of approximate 12-15 columns and they have relationship with its id to main_table. I have to migrate my database to cassandra so I have a question should I create one main_table with consisting multiple columns or different table as in my mysql database.
Will creatimg multiple column take more space or multiple table will take more space
Your line of questioning is flawed. It is a common mistake for DBAs who only have a background in traditional relational databases to view data as normalised tables.
When you switch to NoSQL, you are doing it because you are trying to solve a problem that traditional RDBMS can't. A paradigm shift is required since you can't just migrate relational tables the way they are, otherwise you're back to where you started.
The principal philosophy of data modelling in Cassandra is that you need to design a CQL table for each application query. It is a one-to-one mapping between app queries and CQL tables. The crucial point is you need to start with the app queries, not the tables.
Let us say that you have an application that stores information about users which include usernames, email addresses, first/last name, phone numbers, etc. If you have an app query like "get the email address for username X", it means that you need a table of email addresses and the schema would look something like:
CREATE TABLE emails_by_username (
username text,
email text,
firstname text,
lastname text,
...
PRIMARY KEY(username)
)
You would then query this table with:
SELECT email FROM emails_by_username WHERE username = ?
Another example is where you have an app query like "get the first and last names for a user where email address is Y". You need a table of users partitioned by email instead:
CREATE TABLE users_by_email (
email text,
firstname text,
lastname text,
...
PRIMARY KEY(email)
)
You would query the table with:
SELECT firstname, lastname FROM users_by_email WHERE email = ?
Hopefully with these examples you can see that the disk space consumption is completely irrelevant. What is important is that you design your tables so they are optimised for the application queries. Cheers!

Cassandra - How to make table for friend accept or not

I want to make table like this,
CREATE TABLE friendInvite {
user text,
invitee text,
accepted boolean
PRIMARY(invitee, user)
}
and expected queries are
1. SELECT * FROM friendInvite WHERE invitee="me" and accepted=false
2. UPDATE friendInvite SET accepted=true WHERE invitee="me" and user="you"
I think the query #1 is not good at performance because of accepted condition.
How can i handle this on Cassandra?
I cannot imagine using secondary index for accepted. because it will be updated to true if invitee accepts the offer. Is it ok If i use secondary index for accepted column?
I would create 2 tables.
CREATE TABLE friendInvites {
user text,
invitee text,
PRIMARY(invitee, user)
}
This table holds open friend requests and serves your query #1:
1. SELECT * FROM friendInvite WHERE invitee="me"
Then i would create a second table, where you store the accepted friend requests:
CREATE TABLE acceptedRequests {
user text,
invitee text,
PRIMARY(user, invitee)
}
When you accept a request, the entry has to be removed from friendInvites and inserted into acceptedRequests
Maybe you can use a materialized view that allow your first query to be executed without performance loss :
Your table :
CREATE TABLE friendInvite (
user text,
invitee text,
accepted boolean
PRIMARY KEY (invitee, user)
);
The materialized view :
CREATE MATERIALIZED VIEW friendInviteByInviteeAndAccepted
AS
SELECT *
FROM friendInvite
WHERE invitee IS NOT NULL
AND user IS NOT NULL
AND accepted IS NOT NULL
PRIMARY KEY ((invitee, accepted), user)
WITH CLUSTERING ORDER BY (user ASC);
You can perform updates on the first table friendInvite and you can read your datas from the materialized view. When you update datas in table, Cassandra will automaticly update the materialized view.
This feature is available in cassandra >= 3.0

Delete from denormalized table by id?

I'm trying to delete from records from cassandra using only part of the primary key, this is because I'm trying to delete all videos for certain tags for a given video, however I no longer have the original tags
Schema
Original Killr Video Table
CREATE TABLE videos (
videoid uuid,
userid uuid,
name varchar,
description varchar,
location text,
location_type int,
preview_thumbnails map<text,text>,
tags set<varchar>,
metadata set <frozen<video_metadata>>,
added_date timestamp,
PRIMARY KEY (videoid)
);
Denormalized Video by tag
CREATE TABLE videos_by_tag (
tag text,
videoid uuid,
added_date timestamp,
name text,
preview_image_location text,
tagged_date timestamp,
PRIMARY KEY (tag, videoid)
);
I tried
DELETE FROM videos_by_tag WHERE videoid='SOMEUUID';
but cassandra complaints that I'm missing the tag part of the PK, I know this however how could I ever delete records from this sort of denormalized table if I no longer know the full PK ?
Maybe you can use materialized view.
In this case, you have a master table like :
CREATE TABLE videos (
tag text,
videoid uuid,
added_date timestamp,
name text,
preview_image_location text,
tagged_date timestamp,
PRIMARY KEY (videoid)
);
You are able to delete with yours delete statement :
DELETE FROM videos WHERE videoid='SOMEUUID';
If you want to read data with others criteria , create materialized view :
CREATE MATERIALIZED VIEW
AS
SELECT *
FROM videos_by_tags
WHERE videoid IS NOT NULL
AND tag IS NOT NULL
PRIMARY KEY (tag, videoid);
In this case if you insert, update or delete data in master table (videos).
Associated materialized view will be affected too. So wirte operations must be perform on one table and read on many tables.
When I first created that schema, I was thinking in terms of managing records via application logic. By that, using application workflows to manage the data stored in the database. When you upload a new video, the videos table will get a record and so will videos_by_user and videos_by_tag. When updating, again all three may need to be updated.
In the case of delete, I would expect the application to present the user with a "Delete video?" option. Once that action is taken, the video_id is known and you can use it to delete from any index table. That's following a application workflow data model. Same with tags. If you delete a tag, update videos and videos_by_tag. Preferably with a batch statement to ensure both updates happen.
If you have orphan records of previously deleted videos, then my advice would be to use something like a Spark job to clean things up. Even with a relational DB that would take a fairly interesting sub-query to find all records in tags that have video_ids that don't have a parent record.

How could I create relationships between tables in JetBrains' DataGrip?

I am using DataGrip by JetBrains in my work. It is ok, but I don't know how to create relationships between tables like in this picture:
It's a two step procedure. In the first step, you must modify your table to add foreign key constraint definitions. In the second step, you can display the table diagram.
First, right click on the name of your table in DataGrip, then choose Modify Table. You will see four tabs: Columns, Keys, Indices, and Foreign Keys. Choose the Columns tab. Right click on the column name you want to become a foreign key, and choose New Foreign Key. The window will switch to its Foreign Keys tab with some information filled in. Fill in your "target table". You may also have to write in the target column name in the SQL statement's REFERENCES phrase. Review all the information now in the Modify Table window, and, when satisfied, click "Execute".
Second, right click again on the name of your table in DataGrip, and this time choose Diagrams > Show Visualisation. You should now see a diagram displaying the relations between your original table and the referenced tables.
In DataGrip Help, you can look at the Working with the Database Tool Window page for its Modifying the definition of a table, column, index, or a primary or foreign key section. There is a very short procedure description, there.
Wikipedia has an example in its Defining foreign keys article section that may be useful to you while working in DataGrip's Modify Table window.
I did this procedure in DataGrip 2017.1.3, and I don't know whether other versions vary.
Generally: from the context menu or by pressing Ctrl+Alt+U.
If you have found this picture, one more step was to go deeper in the website and you would get to this page:
https://www.jetbrains.com/datagrip/features/other.html
And there is an explanation how to do it.
Try this small SQL script which creates 3 tables. I think you will find this work well.
CREATE TABLE product (
category INT NOT NULL, id INT NOT NULL,
price DECIMAL,
PRIMARY KEY(category, id)
);
CREATE TABLE customer (
id INT NOT NULL,
PRIMARY KEY (id)
);
CREATE TABLE product_order (
no INT NOT NULL AUTO_INCREMENT,
product_category INT NOT NULL,
product_id INT NOT NULL,
customer_id INT NOT NULL,
PRIMARY KEY(no),
INDEX (product_category, product_id),
INDEX (customer_id),
FOREIGN KEY (product_category, product_id)
REFERENCES product(category, id)
ON UPDATE CASCADE ON DELETE RESTRICT,
FOREIGN KEY (customer_id)
REFERENCES customer(id)
) ;

DB2 backup and restore db table preserving foreign key constraints

I'm currently having issue going through a test backup and restore of database table on my development machine for db2. Was never entirely successful. Although I was able to restore all data after a drop and re-create of the table, I wasn't able to reset the foreign key constraint as I got SQL error complaining that keys don't match. Here's my exact steps, I'm sure not entirely the right way to do it, but it does eventually restore the 5423 rows of data:
The process
export to /export/home/dale/comments.ixf of ixf messages /export/home/dale/msg.txt select * from .comments
Note: step 1 exports 5423 rows of data to a location
drop table .comments
import from /export/home/dale/comments.ixf of ixf create into .comments
Note: step 3 here creates the table but does not insert any data rows
load client from /export/home/dale/comments.ixf of ixf modified by identityoverride replace into .comments
Note: up until this step, I'm able to insert the 5423 rows of data in the recreated db table
alter table .comments add FOREIGN KEY (comments_id) REFERENCES .news (article_key)
Note: here alter table fails as db2 complaints that some comments_id does not match article_key
Could anyone help with my problem here? Thanks in advance
The error means that some of the rows you IMPORT into the Comments table refer to rows that do not exist in the News table.
You might not be forming the constraint correctly. The column name "comment_id" sounds like the primary key to the Comments table. You want the foreign key, which matches the primary key of the News table. It might also be called "article_key" or "article_id".
ALTER TABLE Comments
ADD FOREIGN KEY( article_key)
REFERENCES News( article_key);
If "comment_id" is really not the primary key of the "Comments" table, then the problem comes from not backing up and restoring both the News and Comments table at the same time.
You can either EXPORT and IMPORT the News table along with the Comments table, or remove the Comments that refer to missing News rows with something like this
DELETE FROM Comments
WHERE comments_id NOT IN (
SELECT article_key
FROM News
)
Before you do this, you might want to try listing the Comments which would be deleted by the above query
SELECT *
FROM Comments
WHERE comments_id NOT IN (
SELECT article_key
FROM News
)
I found a solution to my problem as well as my comments above,
user980717 resolved my first problem where I set the wrong column as the foreign key
For my 2nd issue, i.e. "SQL0668N Operation not allowed for reason code "1" on table "tablename". SQLSTATE=57016", I need to run the following command "set integrity for niwps.comments immediate checked" to ensure data satisfied all constraints defined in the table. And thanks to all who took the effort in helping me with my problems. Cheers

Resources