I am using SQL Server 2008/2012.In my database I have a table which has encrypted column.
I am getting encryption key of a encrypted column by using this query -
SELECT DISTINCT key_name(encryptedcol) FROM encryptedTable;
my encrypted column name is encryptedcol.
Now I want to get the certificate name which is used to create the encrypted key (means I need the certificate name which is used to open the above symmetric key to decrypt data).
For example -
Here highlighted text is my certificate name , which is here hardcoded but I want to get this from a sql query.
You can use the system tables to determine the encryption hierarchy for a column. Assuming that the symmetric key was encrypted by a certificate, this tsql will work. If your symmetric key was encrypted by an asymmetric key, then substitute sys.certificates with sys.asymmetric_keys, which also has a thumbprint column that uniquely identifies it.
select
c.name
from sys.symmetric_keys sk
join sys.key_encryptions ke
on
sk.symmetric_key_id= ke.key_id
join sys.certificates c
on
ke.thumbprint=c.thumbprint
where
sk.name in
(SELECT DISTINCT key_name(encryptedssn) FROM HR.Employees)
Related
I've just realized that one of my tables, "pclass", has multiple instances of several foreign keys. In the Structure tab, #2-5 are the foreign keys. I have no idea why multiple instances are being generated.
Could they be generated by the JOINS? Please let me know if I need to provide other information.
$brother_id = htmlspecialchars($_GET["brother_id"]);
$selected = $brother_id;
$query_brotherId = "SELECT b.id, b.firstname, b.lastname, b.pname, b.country, b.street01, b.street02, b.city, usStates.abv AS us_state, b.intl_state, b.postalcode, b.zipcode, b.phone, b.email, pclass.id AS pclass_id, greekAlphabet.name AS pclass01, prepclass.name AS prepclass, pclassSuffix.name AS pclass02, semester.name AS pclass_sem, pclass.year AS pclass_year, b.bigbrother_id AS bbID, bb.firstname AS bbFirst, bb.lastname AS bbLast, b.status, b.comments
FROM brothers AS b
LEFT JOIN pclass ON b.pclass_id = pclass.id
LEFT JOIN prepclass ON pclass.prepclass_id = prepclass.id
LEFT JOIN greekAlphabet ON pclass.greekAlphabet_id = greekAlphabet.id
LEFT JOIN pclassSuffix ON pclass.suffix_id = pclassSuffix.id
LEFT JOIN semester ON pclass.semester_id = semester.id
LEFT JOIN usStates ON b.us_state = usStates.id
LEFT JOIN brothers AS bb ON b.bigbrother_id = bb.id
WHERE b.id = $brother_id";
$result_brotherId = mysqli_query($link, $query_brotherId);
First your question:
Could they be generated by the JOINS?
No. Foreign Keys are generated by data definition statements like CREATE TABLE, ALTER TABLE and so on.
I have no idea why multiple instances are being generated.
The person who created the database must have thought they will be useful. Or if you created the database via some sql-tool (don't know) the tool created the foreign keys because it got told there is a relation between those fields.
Why it is probably not bad to have the keys:
Foreign Keys are created to display the relations between your different table.
Also they enforce a specific behaviour when you are doing actions which could disrupt the integrity of your data. You can change this behaviour in your last screenshot.
For each foreign key you can give a name which will be shown in error messages when you try to act against the constraing. And you can define how the foreign key acts if you change or delete the parent field.
For example
You have the following tables displaying which tool belongs to which person.
persons
personid
firstname
lastname
...
tools
toolid
personid (foreign key to persons)
name
....
So in the tools table you have a foreign key to the persons table, this field defines the owner of the tool.
Now let's define some use cases
Assumption: For some reason Peter is no longer able to wield any tools, so he no longer fits into the database.
What should happen to his tools? It depends what your database displays!
your database displays anyone who ever owned a tool.
This means, even if the person actually doesn't even exist anymore, the data should still remain. You would actually enforce this behaviour otherwise, but it would work in our current case to show what the foreign key can do.
So the action we choose for ON DELETE is RESTICT. (It also is the default action)
Now let's try to call: DELETE FROM persons WHERE firstname = 'Peter'
Result: the foreign key constraint will prompt you an error message. There are relations which depend on this entry in the persons table.
The database displays persons and some tools, tools don't have to have an owner
In this case we again want to delete the person Peter. His tools can remain in the database, instead of the personid they will get a null value into this field.
So we choose the action ON DELETE: SET NULL
This one is pretty straight forward. Important: the field with the foreign key must not have a NOT NULL constraint.
The database displays the people and the tools in a building or something..
So if Peter and his tools leave the building, we don't care about them anymore.
The action for ON DELETE: CASCADE.
If you now enter the DELETE-statement, the foreign key will take care of deleting all the other entries (the tools) connected to Peter.
I've an endpoint /user which creates an unique UUID for a user. It inserts the data(phoneno, gender, age) into the table(cassandra table) and then forwards the same data to another server along with the user_id just created, having MYSQL as the DB.
Now in my MySQL the table is as follow.
id(varchar)
phone no
age
gender
etc.
But I've read that using VARCHAR as PK is a very bad solution. Hence I modified my table as follow:-
id(interger auto increment)
user_id (varchar unique)
phone no
age
gender
etc.
I have another endpoint /recharge, which contains the user_id (UUID), recharge_amount, operator, etc..
My recharge table is as follow:-
user_id FK
amount
operator
Now the problem arises that whenever I'll receive the data for /recharge I need to get the respective id of the user from the Users table to reference it in the recharge table, which is an extra operation. ie for every insert, there will be an extra read operation.
Can I reference/use the unique key as my FK in the recharge table. If no, then what can be the possible solution?
Yes, you can use unique key as foreign key.
To use a column as FK in other table it has to be a PK or a Unique Key.
I have database with following 3 tables:
Customers.CustomerId is integer and PK
Addresses.AddressId is integer and PK
CustomerAddresses.Customer_CustomerId is integer and FK to Customers
CustomerAddresses.Address_AddressId is integer and FK to Addresses
Customers.EntityId is GUID
Addresses.EntityId is GUID
I use SQL Server Integration Services.
I have also XML file with pairs of addressEntityID and customerEntityId. These are are EntityIDs (GUID), that need to be mapped to IDs (integer) and inserted into CustomerAddresses.
How to map this relationship and import to CustomerAddresses?
I want to do something like, for each row, select CustomerId from Customers where EntityId = {0} and then use this value to insert. Can this be done?
And before you suggest, that I should use EntityId as PK, the reason is, that those are not provided for all data in database. Only data, that are imported, and come from another system, those have EntityId.
In Data Flow drop XML source
Add Derived column, map values from XML (it is necessary to map from string to GUID)
Add Lookup, connect to Addresses table, create JOIN from derived AddressGuid to EntityId. AddressId is output.
Add another Lookup and do the same for Customers table
Add DB destination, map values from Lookup to AddressId and CustomerId
I have a dataset for which the sole means of identifying each record is a customer name and a certificate number. For each purchase, the customer has a different certificate number (IE CustA has certificates 1-4, while CustB has certs 1-650) Neither field is unique, however together they uniquely identify a record. When I try to create a composite key, Access tells me that they would create duplicate values in the primary key. These keys are system generated so there cannot be a mistake with the naming/numbering.
Any way to fix this? My understanding is that this is how the system should work and that my dataset is errant, though I am assured that is not the case.
This is no Access issue, this a data issue. A PK cannot - by definition - have duplicates or Nulls.
Make a query to identify the culprits in your data:
select CustName, CertNum, count(*) as Counter
group by CustName, CertNum
having count(*) > 1
Otherwise your can create a surrogate PK using a Counter.
If it is a matter of Nulls, like cjb110 suggested, I can create a Unique Index with IGNORE NULLS. But until you fix nulls and duplicates, your 2 columns won't fit as a natural PK.
If I have a User table (with a global id as primary key) that is sharded across 10 databases (DB1-DB10) based on the username, and another table tries to refer to the User table using the User table row's global id, there is no way for me to know which DB (1-10) that user is located in.
What is the solution to this problem?
Either:
Change your sharding scheme so that you shard based on the key you use to look up this data. If it's by ID, always do it by ID, and shard it by ID (DB = ID % 10)
Make the primary key of the User table the username. Enforce its uniqueness and use the username as the foreign key in the other tables, not a synthetic identifier.
Create a lookup table you can reference which maps ids to shards.