how to select all columns without PRIMARY key in mysql? - mysql

I going to list all columns of a table except PRIMARY key in mysql.
Note : operation is automatically so I don't know the names of fields.
Can you help me for making this query?

This is something worth doing some research on, if you are going to be working with databases in any length.
All DBMS I have worked with thus far have a means of looking at the constraints, columns, and table information. The ones for MySQL that will help you do what you want are likely in the INFORMATION_SCHEMA:
TABLE_CONSTRAINTS The MySQL Reference for this is here.
SELECT table_name, constraint_name, constraint_type FROM INFORMATION_SCHEMA.table_constraints;
COLUMNS The MySQL reference for this is here.
SELECT column_name FROM INFORMATION_SCHEMA.columns;
KEY_COLUMN_USAGE
You should be able to do something like this to get what you want:
SELECT INFORMATION_SCHEMA.key_column_usage.column_name
FROM INFORMATION_SCHEMA.key_column_usage
JOIN INFORMATION_SCHEMA.table_constraints
ON INFORMATION_SCHEMA.key_column_usage.column_name = INFORMATION_SCHEMA.table_constraints.column_name
WHERE INFORMATION_SCHEMA.table_constraints.constraint_type <> 'PRIMARY KEY'
The should be essentially what you need. Views/tables like these can be your best friend when needing to get information about your schema.
I hope that this information helps.

Related

drop primary key if exists

I need a query that could drop primary key only if it exists.
ALTER TABLE tablename DROP PRIMARY KEY;
This will return error if it does not exists, but my requirement is to run a query in different databases.
In MariaDB 10.2.16 i was able to solve this problem with:
ALTER TABLE tablename DROP INDEX IF EXISTS `PRIMARY`;
This should work with any table since the primary keys in MySQL are always called PRIMARY as stated in MySQL doc:
The name of a PRIMARY KEY is always PRIMARY, which thus cannot be
used as the name for any other kind of index.
I would recommend using this:
SELECT CONCAT('ALTER TABLE ', TABLE_SCHEMA, '.',TABLE_NAME,
' DROP PRIMARY KEY; ANALYZE TABLE ', TABLE_SCHEMA, '.',TABLE_NAME, ';')
FROM information_schema.COLUMNS
WHERE CONCAT(TABLE_SCHEMA,TABLE_NAME,COLUMN_NAME) IN
(SELECT CONCAT(TABLE_SCHEMA,TABLE_NAME,COLUMN_NAME)
FROM INFORMATION_SCHEMA.STATISTICS
WHERE INDEX_NAME = 'PRIMARY' -- *Required* to get only the primary keys from the statistics table.
-- *Optional*
AND TABLE_SCHEMA = 'clients_database');
Run this to generate your required SQL.
Copy your results, then run them as working queries.
ANALYZE TABLE is optional as well as the WHERE clause.
You can remove ANALYZE TABLE ', TABLE_SCHEMA, '.',TABLE_NAME, ';' if desired from the query below.
I exploit the information_schema when researching and utilizing standardization techniques.
Just about everything you would ever need or want to know about your tables and columns lives in some System table in either (if applicable)
Database / Table_schema:
information_schema
performance_schema
mysql
Note: From doc's
Internal schemas, such as "performance_schema", "information"schema", "sys", and "mysql", are hidden by default. Toggle the Show Metadata and Internal Schemas preference to list them in the object browser. Schemas beginning with a "." are also controlled by this setting.
NOTE: Here's something similar that has been created.
Hope this helps!
Cheers,
Jay
I think the easy option might be this:
first go to :
'YourDatabase'>tables>your table name>keys>copy the constraints like 'PK__TableName__0001'
then run this:
Query:alter Table 'TableName' drop constraint PK__TableName__0001

Receiving Duplicate Results When Querying information_schema

I'm running a quick query to make sure that all the foreign keys in my table are referencing a field that is indexed.
My code is as follows:
select table_name, column_name, index_name from statistics where table_name in (select referenced_table_name from key_column_usage where table_name='table' and table_schema='schema') and column_name in (select referenced_column_name from key_column_usage where table_name='table' and table_schema='schema') order by table_name;
I'm running this on multiple environments, but for some reason I'm receiving duplicate results on one particular environment. The tables are all set up the same, however, so I'm not sure what is causing this behavior.
Any suggestions?
In that mysql server you probably have 2 databases (schemas) with the same structure or you have multi-column foreign keys. Include the table_schema field in the select list of the outer query to confirm.
Use a multi-column in operator or inner join instead of multiple single column in operators in the where clause of your query.

MySQL: get all references to another scheme

I have: Two schemes scheme1 and scheme2.
I want To delete something from scheme1.
Problem: scheme2 have dependencies on scheme1 so I can't delete what I want.
Question: Is there a way to print all dependencies in scheme2 to scheme1? How to do that?
Question2: It would be perfect if you know how to get ALL dependencies on some_table from all schemes. Do you know?
Answer for second question, if it's tables that are dependent on "some_table":
select TABLE_NAME
from information_schema.REFERENTIAL_CONSTRAINTS
where REFERENCED_TABLE_NAME = <some_table>
of the other way around:
select REFERENCED_TABLE_NAME
from information_schema.REFERENTIAL_CONSTRAINTS
where TABLE_NAME = <some_table>
If you by dependency mean foreign key you can check that in:
select * from information_schema.REFERENTIAL_CONSTRAINTS;
Your first question already had a good answer. If you are in MySQL 5.0 you can use information_schema.table_constraints else if you are in 5.1 or higher you can use information_schema.referential_constraints.
See this thread here from dbo.stackexchange.com
https://dba.stackexchange.com/questions/5441/how-to-find-dependencies-on-a-table-in-mysql-5-0
By your second query if you mean find dependencies of a table, I think you can just use
SHOW CREATE TABLE
which will list out all foreign key constraint defined for a particular table.

What is the MySQL command to check if a field of table A is already referenced to a field of table B as the foreign key?

As the question says, what is the MySQL command to check if a field of table A is already referenced to a field of table B as the foreign key? Are there any commands to let the user know it?
Are you looking for something like this:-
USE information_schema;
SELECT *
FROM
KEY_COLUMN_USAGE
WHERE
REFERENCED_TABLE_NAME = 'A'
AND REFERENCED_COLUMN_NAME = 'A_id';
You can use the systems table for this purpose, however from your previous question I think this might be the wrong way to achieve whatever you are trying to achieve.
http://dev.mysql.com/doc/refman/5.6/en/innodb-sys-foreign-table.html

Constraint detail from information_schema (on update cascade, on delete restrict)

Almost all the information I had needed about a database, I could find in information_schema
This time I needed to read details of all foreign keys in a database through single query I found every thing in information_schema.key_Column_usage but could not find the constraints like on delete, on update
I could do show create table for all individual tables. But is there any way to get these details through some select query like this?
SELECT CONSTRAINT_NAME, TABLE_NAME,COLUMN_NAME, REFERENCED_TABLE_NAME,
REFERENCED_COLUMN_NAME FROM information_schema.`KEY_COLUMN_USAGE` WHERE
table_schema = 'mydbname' AND referenced_column_name IS NOT NULL
It is doing the job well but just missing constraints like on delete, on update How can I get those values as well so that I can get all info about foreign keys in a single query?
UPDATE_RULE and DELETE_RULE is the thing you asked for
it's a little bit too late but it could help someone else, here the solution :
SELECT tb1.CONSTRAINT_NAME, tb1.TABLE_NAME, tb1.COLUMN_NAME,
tb1.REFERENCED_TABLE_NAME, tb1.REFERENCED_COLUMN_NAME, tb2.MATCH_OPTION,
tb2.UPDATE_RULE, tb2.DELETE_RULE
FROM information_schema.`KEY_COLUMN_USAGE` AS tb1
INNER JOIN information_schema.REFERENTIAL_CONSTRAINTS AS tb2 ON
tb1.CONSTRAINT_NAME = tb2.CONSTRAINT_NAME
WHERE table_schema = 'sfa' AND referenced_column_name IS NOT NULL
Update From information_schema.REFERENTIAL_CONSTRAINTS table add in mysql 5.1 mysql-5.1 we can get information about all constraints**. Accepted answer gives the solution as query.
Earlier
Before mysql 5.1 like mysql-5.0, we could not get this information, we could only use show create table for individual table.
If you are looking for (primary|foreign|unique) keys :
http://dev.mysql.com/doc/refman/5.5/en/table-constraints-table.html
Now, you can find foreign key constraint details in table INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS
http://dev.mysql.com/doc/refman/5.5/en/referential-constraints-table.html