> For the complete documentation index, see [llms.txt](https://docs.uxwizz.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.uxwizz.com/import-sql-data-on-an-existing-server.md).

# Import SQL data on an existing server

Use a normal [database restore](/guides/migrating-to-a-new-server.md) when the destination is empty. Combining two databases that both contain tracking data requires a schema-specific migration.

{% hint style="warning" %}
Do not import an old full SQL dump directly over an active UXWizz database. IDs can collide, and relationships between sessions, pages, events, users, and saved configuration can break. Back up both databases before any changes.
{% endhint %}

1. Keep the source database unchanged and take verified backups of both servers.
2. Record the exact UXWizz version and schema on each server, including all databases in an Agency setup.
3. Restore copies into an isolated test environment.
4. Have a database administrator map all affected primary keys, foreign keys, and application references. Decide separately how to handle accounts, access rules, saved dashboards, and credentials.
5. Test the merge on those copies. Check row counts, relationships, domain access, reports, and session playback.
6. Plan a maintenance window, final backups, and rollback before applying the verified migration to the live destination.

### SQL script to increase all IDs (in order to make room for old data)

The original ID-offset procedure is retained below for **UXWizz 8.5.0 only**. Its ID changes and foreign-key relationships were rechecked on that version's schema in MariaDB 11.8. It is not a merge script for UXWizz 10: newer tables also refer to users, sessions, pageviews, and domains.

#### Prepare the two 8.5.0 databases

1. Back up **both** databases before cleanup or ID changes, and test a restore.
2. Restore working copies with the same 8.5.0 schema. Stop tracking and scheduled tasks on the copies while testing.
3. Export the old database as **data only**, without table creation, indexes, or `DROP TABLE` statements.
4. Reconcile duplicate values in unique columns before import. Both installations commonly have an `admin` username; adding an ID offset does not resolve that name conflict. Check session tokens and domain limits too. Keep the destination's `ust_meta` configuration instead of blindly importing duplicate settings.
5. Choose an offset that avoids collisions in **every** table listed below. Compare the minimum and maximum IDs in both copies: the shifted destination range must start above both original maximums, and its maximum must stay within the unsigned integer limit, 4,294,967,295. For example, inspect a table with `SELECT MIN(id), MAX(id) FROM ust_clients;`. Repeat for all affected tables, including `ust_partials`, whose IDs can already be large.
6. Run the SQL below on the **destination test copy**. Replace `10000000` with the checked offset.
7. Import the old data into that shifted copy. Stop on any import error. Do not use `INSERT IGNORE` to hide duplicate rows.
8. Check row counts, all foreign-key relationships, account/domain access, reports, and sample recordings. Foreign-key checks being re-enabled does **not** validate rows that were written while checks were off.
9. Only after the complete test succeeds, repeat the agreed procedure during a maintenance window with fresh backups. Keep a restore plan.

{% hint style="warning" %}
The foreign-key checks are disabled only in the connection running this legacy migration, because linked IDs are changed in separate statements. The `ALTER TABLE` statements implicitly commit: the surrounding transaction is **not** a rollback plan for the whole procedure. Restore the test copy or backup after a partial failure instead of rerunning the offset on already-shifted rows.
{% endhint %}

```sql
-- Use the offset verified for every affected table in both database copies.

-- Disable FK checks
SET FOREIGN_KEY_CHECKS=0;
SET @offset = 10000000;

START TRANSACTION;

-- Primary keys
UPDATE ust_users SET id = id + @offset;
UPDATE ust_clients SET id = id + @offset;
UPDATE ust_clientpage SET id = id + @offset;
UPDATE ust_client_tag SET id = id + @offset;
UPDATE ust_client_event SET id = id + @offset;
UPDATE ust_clicks SET id = id + @offset;
UPDATE ust_movements SET id = id + @offset;
UPDATE ust_records SET id = id + @offset;
UPDATE ust_partials SET id = id + @offset;
UPDATE ust_access SET id = id + @offset;
UPDATE ust_user_client_ip_label SET id = id + @offset;
UPDATE ust_limits SET id = id + @offset;
UPDATE ust_ab SET id = id + @offset;

-- Foreign keys
UPDATE ust_access SET userid = userid + @offset;
UPDATE ust_user_client_ip_label SET userid = userid + @offset;
UPDATE ust_user_client_watched
    SET userid = userid + @offset,
        clientid = clientid + @offset;
UPDATE ust_clientpage SET clientid = clientid + @offset;
UPDATE ust_client_tag SET clientid = clientid + @offset;
UPDATE ust_client_event
    SET clientid = clientid + @offset,
        clientpageid = clientpageid + @offset;
UPDATE ust_clicks SET client = client + @offset;
UPDATE ust_movements SET client = client + @offset;
UPDATE ust_records SET client = client + @offset;
UPDATE ust_partials SET client = client + @offset;

-- === Reset AUTO_INCREMENT for each table ===

-- ust_users
SELECT GREATEST(IFNULL(MAX(id), 0) + 1, @offset) INTO @next_id FROM ust_users;
SET @sql = CONCAT('ALTER TABLE ust_users AUTO_INCREMENT = ', @next_id);
PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;

-- ust_clients
SELECT GREATEST(IFNULL(MAX(id), 0) + 1, @offset) INTO @next_id FROM ust_clients;
SET @sql = CONCAT('ALTER TABLE ust_clients AUTO_INCREMENT = ', @next_id);
PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;

-- ust_clientpage
SELECT GREATEST(IFNULL(MAX(id), 0) + 1, @offset) INTO @next_id FROM ust_clientpage;
SET @sql = CONCAT('ALTER TABLE ust_clientpage AUTO_INCREMENT = ', @next_id);
PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;

-- ust_client_tag
SELECT GREATEST(IFNULL(MAX(id), 0) + 1, @offset) INTO @next_id FROM ust_client_tag;
SET @sql = CONCAT('ALTER TABLE ust_client_tag AUTO_INCREMENT = ', @next_id);
PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;

-- ust_client_event
SELECT GREATEST(IFNULL(MAX(id), 0) + 1, @offset) INTO @next_id FROM ust_client_event;
SET @sql = CONCAT('ALTER TABLE ust_client_event AUTO_INCREMENT = ', @next_id);
PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;

-- ust_clicks
SELECT GREATEST(IFNULL(MAX(id), 0) + 1, @offset) INTO @next_id FROM ust_clicks;
SET @sql = CONCAT('ALTER TABLE ust_clicks AUTO_INCREMENT = ', @next_id);
PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;

-- ust_movements
SELECT GREATEST(IFNULL(MAX(id), 0) + 1, @offset) INTO @next_id FROM ust_movements;
SET @sql = CONCAT('ALTER TABLE ust_movements AUTO_INCREMENT = ', @next_id);
PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;

-- ust_records
SELECT GREATEST(IFNULL(MAX(id), 0) + 1, @offset) INTO @next_id FROM ust_records;
SET @sql = CONCAT('ALTER TABLE ust_records AUTO_INCREMENT = ', @next_id);
PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;

-- ust_partials
SELECT GREATEST(IFNULL(MAX(id), 0) + 1, @offset) INTO @next_id FROM ust_partials;
SET @sql = CONCAT('ALTER TABLE ust_partials AUTO_INCREMENT = ', @next_id);
PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;

-- ust_access
SELECT GREATEST(IFNULL(MAX(id), 0) + 1, @offset) INTO @next_id FROM ust_access;
SET @sql = CONCAT('ALTER TABLE ust_access AUTO_INCREMENT = ', @next_id);
PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;

-- ust_user_client_ip_label
SELECT GREATEST(IFNULL(MAX(id), 0) + 1, @offset) INTO @next_id FROM ust_user_client_ip_label;
SET @sql = CONCAT('ALTER TABLE ust_user_client_ip_label AUTO_INCREMENT = ', @next_id);
PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;

-- ust_limits
SELECT GREATEST(IFNULL(MAX(id), 0) + 1, @offset) INTO @next_id FROM ust_limits;
SET @sql = CONCAT('ALTER TABLE ust_limits AUTO_INCREMENT = ', @next_id);
PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;

-- ust_ab
SELECT GREATEST(IFNULL(MAX(id), 0) + 1, @offset) INTO @next_id FROM ust_ab;
SET @sql = CONCAT('ALTER TABLE ust_ab AUTO_INCREMENT = ', @next_id);
PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;

COMMIT;

-- Re-enable FK checks
SET FOREIGN_KEY_CHECKS=1;
```

For current versions or customized schemas, [contact support](/guides/support.md) with the versions and database layout so the migration can include the additional references. Do not send SQL dumps or credentials in an ordinary support message.
