Home > Database > Mysql Tutorial > How to Create a Persistent Temporary Table in MySQL without Explicit CREATE TABLE?

How to Create a Persistent Temporary Table in MySQL without Explicit CREATE TABLE?

DDD
Release: 2024-12-20 19:10:11
Original
833 people have browsed it

How to Create a Persistent Temporary Table in MySQL without Explicit CREATE TABLE?

How to Create a Persistent Temporary Table without an Explicit CREATE TABLE Statement

In MySQL, you can effortlessly create a temporary table from a SELECT statement without resorting to a separate CREATE TABLE command that specifies every column type. This approach offers a time-saving solution, eliminating the need to manually craft the CREATE TABLE statement and align the column name and type lists.

Solution:

CREATE TEMPORARY TABLE IF NOT EXISTS table2 AS (SELECT * FROM table1)
Copy after login

This command creates a temporary table named table2 that mirrors the structure and data of table1. The TEMPORARY keyword ensures that the table is visible only within the current session and is automatically dropped once the session ends.

Explanation:

As mentioned in the MySQL documentation, using the TEMPORARY keyword allows you to create a table that remains accessible only for the active session. This eliminates any potential conflicts or overlaps with tables of the same name in other sessions or with non-temporary tables. Additionally, users must possess the CREATE TEMPORARY TABLES privilege to create temporary tables.

The above is the detailed content of How to Create a Persistent Temporary Table in MySQL without Explicit CREATE TABLE?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template