PostgreSQL 및 MySQL의 모든 테이블에서 특정 데이터 찾기

PHPz
풀어 주다: 2024-07-26 19:29:23
원래의
1049명이 탐색했습니다.

Finding Specific Data Across All Tables in PostgreSQL and MySQL

When working with large databases, you may find yourself needing to locate a specific value across multiple tables and columns. This can be a challenging task, especially if you don't know exactly where to look. Fortunately, there are ways to automate this search in both PostgreSQL and MySQL.

Searching for Data in PostgreSQL

PostgreSQL allows for advanced procedural language capabilities, which can be very helpful in scenarios like these. Below, we'll create a PL/pgSQL block that searches for a specific value across all tables and columns in a PostgreSQL database.

Step-by-Step Guide:

  1. Create a PL/pgSQL Block:

The following PL/pgSQL block will search for the value 'dcea8891-b4e1-45f8-8cb9-c8a164cb98ff' in all columns of type character varying, text, or uuid within the public schema.

   DO $$ 
   DECLARE
       rec RECORD;
       search_text TEXT := 'dcea8891-b4e1-45f8-8cb9-c8a164cb98ff';
       query TEXT;
   BEGIN
       FOR rec IN 
           SELECT table_schema, table_name, column_name 
           FROM information_schema.columns 
           WHERE table_schema = 'public' AND data_type IN ('character varying', 'text', 'uuid') 
       LOOP
           query := 'SELECT ''' || rec.table_schema || '.' || rec.table_name || '.' || rec.column_name || ''' AS location, ' || 
                    rec.column_name || ' 
                    FROM ' || rec.table_schema || '.' || rec.table_name || ' 
                    WHERE ' || rec.column_name || '::text = $1';

           EXECUTE query USING search_text INTO rec;

           IF rec IS NOT NULL THEN
               RAISE NOTICE 'Found in %', rec.location;
           END IF;
       END LOOP;
   END $$;
로그인 후 복사
  1. Explanation:
  • Declare Variables: We declare variables to hold our search text and the dynamic query.
  • Loop Through Columns: We loop through each column in the public schema that is of type character varying, text, or uuid.
  • Construct and Execute Query: For each column, we construct a dynamic SQL query to check if the column contains the search text.
  • Raise Notice: If the search text is found, a notice is raised with the location of the column.
  1. Run the Block:

Execute the above block in your PostgreSQL query tool (e.g., pgAdmin, psql). This will print out the locations where the specified value is found.

Searching for Data in MySQL

MySQL does not support PL/pgSQL-style procedural language blocks. However, you can achieve similar functionality by generating and running the necessary queries manually or using a shell script.

Step-by-Step Guide:

  1. Generate the Search Queries:

The following SQL query will generate a list of queries to search for the value 'dcea8891-b4e1-45f8-8cb9-c8a164cb98ff' in all columns of type varchar, text, or char within your specified database.

   SELECT 
       CONCAT('SELECT ''', table_schema, '.', table_name, '.', column_name, 
              ''' AS location, ', column_name, 
              ' FROM ', table_schema, '.', table_name, 
              ' WHERE ', column_name, ' = ''', 'dcea8891-b4e1-45f8-8cb9-c8a164cb98ff', ''';') AS search_query
   FROM 
       information_schema.columns
   WHERE 
       table_schema = 'your_database_name' 
       AND data_type IN ('varchar', 'text', 'char');
로그인 후 복사

Replace your_database_name with the actual name of your database.

  1. Run the Generated Queries Manually:

Copy the output of the above query, which will look something like this:

   SELECT 'your_database_name.table1.column1' AS location, column1 
   FROM your_database_name.table1 
   WHERE column1 = 'dcea8891-b4e1-45f8-8cb9-c8a164cb98ff';

   SELECT 'your_database_name.table2.column2' AS location, column2 
   FROM your_database_name.table2 
   WHERE column2 = 'dcea8891-b4e1-45f8-8cb9-c8a164cb98ff';
로그인 후 복사

Execute each of these queries manually in your MySQL client.

  1. Automate with a Shell Script:

If you have access to a Unix-like shell, you can automate the process using a shell script:

   #!/bin/bash

   SEARCH_TEXT='dcea8891-b4e1-45f8-8cb9-c8a164cb98ff'
   DATABASE='your_database_name'
   USERNAME='your_username'
   PASSWORD='your_password'

   # Generate the search queries
   QUERIES=$(mysql -u $USERNAME -p$PASSWORD -D $DATABASE -N -e "
     SELECT CONCAT('SELECT ''', table_schema, '.', table_name, '.', column_name, 
                   ''' AS location, ', column_name, 
                   ' FROM ', table_schema, '.', table_name, 
                   ' WHERE ', column_name, ' = ''', '$SEARCH_TEXT', ''';')
     FROM information_schema.columns
     WHERE table_schema = '$DATABASE' AND data_type IN ('varchar', 'text', 'char');")

   # Execute each query
   while read -r QUERY; do
     mysql -u $USERNAME -p$PASSWORD -D $DATABASE -e "$QUERY"
   done <<< "$QUERIES"
로그인 후 복사

Replace your_username, your_password, and your_database_name with your actual MySQL username, password, and database name. This script will generate the necessary search queries and then execute each one, printing the results.

Conclusion

Whether you're using PostgreSQL or MySQL, you can efficiently search for a specific value across all tables and columns in your database by leveraging procedural language blocks in PostgreSQL or by generating and executing dynamic queries in MySQL. These methods can save you significant time and effort when managing large datasets.

For PostgreSQL, the PL/pgSQL block provides a robust solution for automating the search. For MySQL, while procedural language blocks are not available, generating and running dynamic queries manually or through a shell script can achieve the same result.

By utilizing these techniques, you can streamline your database management tasks and quickly locate the data you need.

위 내용은 PostgreSQL 및 MySQL의 모든 테이블에서 특정 데이터 찾기의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

원천:dev.to
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿
회사 소개 부인 성명 Sitemap
PHP 중국어 웹사이트:공공복지 온라인 PHP 교육,PHP 학습자의 빠른 성장을 도와주세요!