# UUID in Datenbank erstellen

**URL:** <https://forum.shopware.com/t/uuid-in-datenbank-erstellen/103715>\
**Category:** Shopware 6 (German)\
**Created:** [16. April 2024 um 06:23 UTC](https://forum.shopware.com/t/uuid-in-datenbank-erstellen/103715 "2024-04-16T06:23:26Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![brettvormkopp](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.shopware.com/brettvormkopp/32/7788_2.png) [@brettvormkopp](https://forum.shopware.com/u/brettvormkopp)\
**Post date:** [16. April 2024 um 06:23 UTC](https://forum.shopware.com/t/uuid-in-datenbank-erstellen/103715/1 "2024-04-16T06:23:26Z")

</div>

Wenn ich innerhalb der Datenbank eine UUID() erstelle, dann erhalte ich:

```auto
SELECT UUID() > 9405853d-fbb8-11ee-a525-74563c917154
#und um die kompakter zu bekommen alle Bindestriche entfernen
SELECT REPLACE(UUID(),'-','') > 9405853dfbb811eea52574563c917154

```

Wenn ich aber das gleiche mit INSERT in die Tabelle schreiben möchte, dann kürzt er mir das Ergebnis auf 16 Zeichen und man kann es nur als String inserten?

```auto
INSERT INTO property_group(id) VALUES (REPLACE(UUID(),'-','')) 
#respektive
INSERT INTO property_group(id) VALUES ('9405853dfbb811eea52574563c917154')

```

Erst wenn ich UNHEX() nutze fügt er es korrekt ein.

```auto
INSERT INTO property_group(id) VALUES (UNHEX(REPLACE(UUID(),'-','')))

```

Ist das der korrekte Weg?

---

<div class="post-metadata">

**Author:** ![R4M](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.shopware.com/r4m/32/9983_2.png) [@R4M](https://forum.shopware.com/u/R4M)\
**Post date:** [16. April 2024 um 06:39 UTC](https://forum.shopware.com/t/uuid-in-datenbank-erstellen/103715/2 "2024-04-16T06:39:52Z")

</div>

Vielleicht so:

> SELECT UUID\_TO\_BIN(UUID())

oder

> SET @NewUUID = ( SELECT UUID\_TO\_BIN(UUID()) );  
> INSERT INTRO … @NewUUID …

---

<div class="post-metadata">

**Author:** ![area-net-gmbh](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.shopware.com/area-net-gmbh/32/21995_2.png) [@area-net-gmbh](https://forum.shopware.com/u/area-net-gmbh)\
**Post date:** [16. April 2024 um 06:58 UTC](https://forum.shopware.com/t/uuid-in-datenbank-erstellen/103715/3 "2024-04-16T06:58:35Z")

</div>

`UUID()` erstellt einen String. Die ID-Spalten in Shopware sind vom Typ Binary.

Daher musst du den String (wie von dir beschrieben) mit `UNHEX()` in einen Binary-Typ umwandeln. Oder alternativ wie bei @r4m über `UUID_TO_BIN()`.

---

<div class="post-metadata">

**Author:** ![brettvormkopp](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.shopware.com/brettvormkopp/32/7788_2.png) [@brettvormkopp](https://forum.shopware.com/u/brettvormkopp)\
**Post date:** [16. April 2024 um 07:47 UTC](https://forum.shopware.com/t/uuid-in-datenbank-erstellen/103715/4 "2024-04-16T07:47:55Z")

</div>

OK, UUID\_TO\_BIN() kennt meine MariaDB 10.6 nicht :-S . dann muss ich wohl auf 10.7 warten 😃
