I have a table like this in Cassandra-
CREATE TABLE DATA_HOLDER (USER_ID TEXT, RECORD_NAME TEXT, RECORD_VALUE BLOB, PRIMARY KEY (USER_ID, RECORD_NAME));
I want to count distinct USER_ID in my above table? Is there any way I can do that?
My Cassandra version is:
[cqlsh 4.1.1 | Cassandra 2.0.10.71 | DSE 4.5.2 | CQL spec 3.1.1 | Thrift protocol 19.39.0]
The select expression is defined as:
selection_list
| DISTINCT selection_list
so you can:
SELECT DISTINCT USER_ID FROM DATA_HOLDER;
Related
I'm trying to build a MySQL database gradually, by generation using JPA and Eclipse-Link. Along the way, I've changed some relationships #ManyToOne, #OneToOne etc.
I now have a situation where I have some spurious foreign keys: the tables don't exist, but the referenced tables still do. I think the original tables were cross-reference tables generated by EclipseLink but are no longer around.
The issue is, I cannot delete these referenced tables. I get an error like this:
mysql> drop table PRODUCTDO;
ERROR 3730 (HY000): Cannot drop table 'PRODUCTDO' referenced by a foreign key constraint 'PRODUCTDO_DISTRIBUTIONCENTERPRODUCTDO_ProductDo_ID' on table 'PRODUCTDO_DISTRIBUTIONCENTERPRODUCTDO'.
If I run:
SET foreign_key_checks = 0;
then I can delete the table, but the constraint still remains. Even if I drop the database and create it again, the constraint is still there:
mysql> SELECT TABLE_NAME,COLUMN_NAME,CONSTRAINT_NAME, REFERENCED_TABLE_NAME,REFERENCED_COLUMN_NAME FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE WHERE REFERENCED_TABLE_SCHEMA = 'SCO';
+---------------------------------------+------------------------------+----------------------------------------------------+-----------------------------+------------------------+
| TABLE_NAME | COLUMN_NAME | CONSTRAINT_NAME | REFERENCED_TABLE_NAME | REFERENCED_COLUMN_NAME |
+---------------------------------------+------------------------------+----------------------------------------------------+-----------------------------+------------------------+
| PRODUCTDO_DISTRIBUTIONCENTERPRODUCTDO | ProductDo_ID | PRODUCTDO_DISTRIBUTIONCENTERPRODUCTDO_ProductDo_ID | PRODUCTDO | ID |
| PRODUCTDO_DISTRIBUTIONCENTERPRODUCTDO | distributionCenterProduct_ID | PRDCTDDSTRBTIONCENTERPRODUCTDOdstrbtnCntrProductID | DISTRIBUTIONCENTERPRODUCTDO | ID |
+---------------------------------------+------------------------------+----------------------------------------------------+-----------------------------+------------------------+
2 rows in set (0.01 sec)
How can I get rid of these zombie constraints?
In the end, I had to recreate the table and foreign key then I was able to delete it.
I have a table called COMPANIES that contains info on different companies. How do make a query that gets the company state(COMPANY_STATE) and the company name(COMPANY) count of the most used state (COMPANY_STATE) which has the highest number of different COMPANIES in it. So say Ohio has the most companies in it with 50. How to I query the database to get the state
| COMPANY_STATE | COUNT |
+---------------+-------+
| OH | 50 |
+---------------+-------+
Can i do this via query or do i have to have my java program actually do the work?
Table.sql
CREATE TABLE COMPANIES (
ID INT NOT NULL AUTO_INCREMENT,
COMPANY varchar(255) NOT NULL,
COMPANY_CODE char(10) NOT NULL,
COMPANY_ADDRESS varchar(255),
COMPANY_STATE char(2) NOT NULL,
COMPANY_WORKFORCE INT,
PRIMARY KEY (ID)
)
SELECT COMPANY_STATE, COUNT(ID) FROM COMPANIES
GROUP BY COMPANY_STATE
ORDER BY COUNT(ID) DESC
LIMIT 1
Following is my Cassandra table structure.
Advertisement
AdvertisementId | Ad_Language | Ad_Caption | Others
----------------------------------------------------------------------
A01(UUID) | EN_US (text)| englishCaption (text) | Other Info(text)
A01(UUID) | FR_CA (text)| French Caption (text) | Other Info (text)
Primary key is (AdvertisementId, Ad_Language);
I am using java to integrate with Cassandra. There is a Java API call to fetch List<advertisements>
Is there a possiblity to fetch the rows like
Query : select * from ad_details orderBy advertisementId; (Unfortunately I cannot specify a col_name that will be used in WHERE or In clause)
I cannot have advertisement Id as cluster key as I need to maintain the UUID as partition key of the composite primary key in Cassandra.
The following query works: Select * from ad_details where advertisementId=xxx orderBy language ASC;
Can someone please help me in carrying out the orderBy advertisementId?
You can not order by a partition key when using the MurMur3partitioner or RandomPartitioner. If you are using an ordered partitioner the results will be in the order of the type specified for the partition key when creating the table.
You can't order by primary key unless you are using IN.
If you are not limiting your search with "where" you probably need to redesign your table as it is not an efficient design, when table gets big you can't query it in efficient way.
From below table I want to extract the column values (C_Number) with the latest TimeStamp buy comparing with current system timestamp from db2 table? Please help.
Example: In Table "Computer" there are 3 columns i.e
C_Number | C_Data | TimeStamp
------------------------------------------------------------------------------
12-DFHK | Yes | 2013-08-14 07:33:05.29
13-DFCC | Yes | 2013-08-18 07:45:05.29
Form the above table how can i extract the Column "C_Number" values with latest Timestamp(in this above table latest timestamp is "2013-08-18 07:45:05.29" ) by comparing with current system time.
SELECT C_Number FROM Computer
WHERE TimeStamp = (SELECT MAX(TimeStamp) FROM Computer);
one more efficient way to achive your purpose is the following:
SELECT C_Number
FROM Computer
ORDER BY TimeStamp DESC
FETCH FIRST ROW ONLY ;
I am running a MySQL 5.1 server and Hibernate 3.5.1 / JPA2 for ORM. Everthing seems fine until I drop some tables manually. From then on, unit tests fail with Hibernate no longer creating certain tables. Changing the jdbc url from
url=jdbc:mysql://localhost:3306/dbjava?createDatabaseIfNotExist=true
to
url=jdbc:mysql://localhost:3306/dbjavanew?createDatabaseIfNotExist=true
solves the problem ... until I perform some table drops :-( Since I am going to use MySQL for production, this must not happen at all.
Dropping dbjava does not help too. Any suggestions?
--- ADDITIONAL INFO:
It's getting really weired. From mysql console:
mysql> use dbjava;
Database changed
mysql> show tables;
+---------------------+
| Tables_in_dbjava |
+---------------------+
| aa |
| ba |
| da |
| ea |
| hibernate_sequences |
| ia |
| ka |
| la |
+---------------------+
8 rows in set (0.00 sec)
mysql> create table `cA` (id integer not null, comment varchar(255), name varchar(255), id_d integer, id_f integer, primary key (id)) ENGINE=InnoDB;
ERROR 1050 (42S01): Table 'ca' already exists
Uh? Why does Table 'ca' exist?!?!? Actually, it once existed ... since then, it was dropped, the whole db was dropped several times ... why does it still exist?
Even worse:
mysql> drop table cA;
mysql> ERROR 1051 (42S02): Unknown table 'ca'
Totally confused ...
However, I just realised something: my table names in hibernate use lower and upper case (camel case) notation. mysql responses lower case only. Would anybody confirm that mysql does not recognize case-sensitiveness in the year 2010 ?!?
Which dialect are you using? Newer versions of Hibernate (can't remember exact numbers) have InnoDB dialect fix: instead of type=InnoDb it uses engine=InnoDb when creating tables -- which is the only legitimate syntax in MySQL 5.1. Just to remind, MySQL 5.0 accepted both type and engine keywords.