How to add a column of type array using phpMyAdmin or sql
P粉0128759272024-03-27 00:18:52
0
1
511
How to use phpMyAdmin to add a column of type array
Know
Change the table name and add column listid integer array;
Change table name and add column listid integer [];
Not working
As mentioned in the comments, arrays are not types. You can choose to use a separate table to hold the elements in the array and have them have a foreign key that references the original table, or parse the array into a string each time and store it as text, depending on your needs.
CREATE TABLE orders (
id INT NOT NULL PRIMARY KEY,
description TEXT,
reference TEXT
-- This is where you'd want to add your list of order lines
);
-- Instead, we'll create an orderline table referring back to the orders
CREATE TABLE orderlines (
id INT NOT NULL PRIMARY KEY,
description TEXT,
order_id INT REFERENCES orders(id)
);
Now you can put the array values (I'm assuming for now the order lines) into their own separate table. To query them you can do this
SELECT * FROM orders
LEFT JOIN orderlines ON orderlines.order_id = orders.id;
You can use subqueries to be smart enough to return an array, especially in your application.
As mentioned in the comments, arrays are not types. You can choose to use a separate table to hold the elements in the array and have them have a foreign key that references the original table, or parse the array into a string each time and store it as text, depending on your needs.
Now you can put the array values (I'm assuming for now the order lines) into their own separate table. To query them you can do this
You can use subqueries to be smart enough to return an array, especially in your application.