I am just wondering to know can I have a string of TIMESTAMPs in a MySQL table schema like following:
CREATE TABLE IF NOT EXISTS auth (
id BINARY(16) PRIMARY KEY,
email VARCHAR(64) UNIQUE NOT NULL,
password VARCHAR(64) NOT NULL,
admin INT NOT NULL DEFAULT 0,
created_at [TIMESTAMP]
);
Unfortunately the above code gives me a syntax error!
My idea is to have a list of TIMESTAMs to store every future updates inside of it, something like ['2023-01-01' , '2023-02-02' , ....]
If this is not possible in my suggested way, how can I store any changes of one column of a table like created_at column? Should I do it in the web-server?
Related
I have an SQL table:
CREATE TABLE pu_events(
int eid AUTO_INCREMENT PRIMARY KEY,
varchar(20) title,
varchar(255) description,
int(11) start_date UNSIGNED,
int(11) end_date UNSIGNED,
timestamp created DEFAULT CURRENT_TIMESTAMP,
json members
)
I plan on populating the members field with a members json object which will be an array of objects containing the user id (uid) and status of attending members, such as:
{members: [{uid:1, status:0}, {uid:2, status:1}]}
But I'm having trouble finding any resources which describe how to correctly reference this object structure to manipulate it, for example if i wish to 'register' a user to the event, to append their object to the array of members like: (members.push({uid:3, status:0}), or to update the status of a given user once they are confirmed or resign from the event, like: (update members set status = 2 where uid = 1;).
I understand that the pseudo-c I've used is a combination of js & mysql, and i also understand that MySQL now has JSON functions for manipulating this datatype, but I'm confused with the best way to approach this particular use case.
Many thanks for any advice in advance!
The proper solution is to create another table:
CREATE TABLE pu_event_members (
event_id INT NOT NULL,
user_id INT NOT NULL,
status TINYINT NOT NULL,
PRIMARY KEY (event_id, user_id)
);
Then it's easy to register a new member of the event:
INSERT INTO pu_event_members SET event_id=?, user_id=?, status=?
Or update their status:
UPDATE pu_event_members SET status=? WHERE event_id=? AND user_id=?
Working example but only select:
select pu_events.*
from pu_events, JSON_TABLE(members, "$.members[*].uid" COLUMNS(f INT PATH '$')) as list
where list.f = 1
Mysql 8 has support JSON_TABLE
https://dev.mysql.com/doc/refman/8.0/en/json-search-functions.html
https://dev.mysql.com/doc/refman/8.0/en/json-table-functions.html
I am new to MySQL, so I'll explain by example.
I have 2 tables:
Admins(
id int auto_increment not null,
primary key(id)
);
Users(
admin_id int not null,
id varchar(255) not null,
password varchar(255) not null
);
I basically create an entry for a admin, then I want the admin to be able to add users that are tied to his ID, so then I can also read all users tied to that certain admin (the id and password parameters, to be exact)
I cannot figure out the syntax for how to do this, could anyone provide some help? Maybe there is a way to just write all that data straight in the admin table somehow so I don't have to use 2 tables?
I use PHP to do everything, by the way
I made a table like this:
CREATE TABLE options(
id MEDIUM INT NOT NULL AUTO_INCREMENT,
email VARCHAR(254) NOT NULL,
created_at DATETIME DEFAULT NULL,
PRIMARY KEY(id)
);
however.. after inserting a value like:
INSERT INTO options VALUES(0,'test#test.com',CURRENT_TIMESTAMP)
I got the created_at column with this value : 10:29 but right now the time is 16:29.
I believe that's because the server/db is located in the US and I'm in Brazil.
Is there a way to get my current time when inserting values into the table?
Sorry if this is an easy question, I am coming to MySQL from SQL Server.
When I execute my create statement it contains nvarchar but commits to the database as varchar. Even in my alter statement afterwards the column does not change at all. Does the collation or DB engine make a difference?
During execution I am not encountering any issues in results, other than the fact the column changes datatype. I attached a screencast of my activity http://screencast.com/t/wc94oei2
I have not been able to find anyone with similar issues through my Google searches
Did you mean, this..
CREATE TABLE stars (
idstars int(11) NOT NULL AUTO_INCREMENT,
Name nvarchar(200) DEFAULT NULL,
PRIMARY KEY (idstars),
UNIQUE KEY Name_UNIQUE (Name)
)
----turns to---
CREATE TABLE stars (
idstars int(11) NOT NULL AUTO_INCREMENT,
Name varchar(200) DEFAULT NULL,
PRIMARY KEY (idstars),
UNIQUE KEY Name_UNIQUE (Name)
)
I don't seem to be able to get my SELECT statement to work.
This is the table:
CREATE TABLE clients(
user_id MEDIUMINT UNSIGNED NOT NULL AUTO_INCREMENT,
email VARCHAR (70) NOT NULL,
mobile_number int(11) NOT NULL,
UNIQUE KEY (email)
);
The Select Query
SELECT user_id FROM clients WHERE email='info#candy.co.uk';
Whenever I try using this SELECT statement from mysqlADMIN it returns null; this happens even when I enter an email address that I know is in the database.
I would really appreciate some advice on where I am going wrong.
Try the statement without the "WHERE" clause. If it returns the entire table you have narrowed it down to an error in your "email" string.
If it returns nothing and you know there is data in this table then check your connection string and make sure you are using the correct DB.
I think there is some error in your SQL create statement. You should make the unique key to which you have applied auto increment. In this case the database will give an error.
Please try the following creation statement
CREATE TABLE clients(
user_id MEDIUMINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
email VARCHAR (70) NOT NULL,
mobile_number int(11) NOT NULL
);