How do I get the hash generated after updates?
thufir#dur:~$
thufir#dur:~$ mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 138
Server version: 5.1.58-1ubuntu1 (Ubuntu)
Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.
This software comes with ABSOLUTELY NO WARRANTY. This is free software,
and you are welcome to modify and redistribute it under the GPL v2 license
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> use nntp;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> show tables;
+---------------------+
| Tables_in_nntp |
+---------------------+
| articles |
| newsgroups |
| newsgroups_articles |
+---------------------+
3 rows in set (0.00 sec)
mysql> describe newsgroups;
+-----------+----------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------+----------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| newsgroup | longtext | NO | | NULL | |
| hash | char(32) | NO | | NULL | |
+-----------+----------+------+-----+---------+----------------+
3 rows in set (0.00 sec)
mysql> show triggers;
+-----------+--------+------------+-----------------------------------------------------+--------+---------+----------+----------------+----------------------+----------------------+--------------------+
| Trigger | Event | Table | Statement | Timing | Created | sql_mode | Definer | character_set_client | collation_connection | Database Collation |
+-----------+--------+------------+-----------------------------------------------------+--------+---------+----------+----------------+----------------------+----------------------+--------------------+
| make_hash | INSERT | newsgroups | BEGIN
set new.hash = md5(new.newsgroup);
END | BEFORE | NULL | | root#localhost | utf8 | utf8_general_ci | latin1_swedish_ci |
+-----------+--------+------------+-----------------------------------------------------+--------+---------+----------+----------------+----------------------+----------------------+--------------------+
1 row in set (0.00 sec)
mysql>
mysql>
mysql> DELIMITER $$
mysql>
mysql> USE `nntp`$$
Database changed
mysql> CREATE
-> TRIGGER `nntp`.`make_hash_update`
-> AFTER UPDATE ON `nntp`.`newsgroups`
-> FOR EACH ROW
-> BEGIN
-> set old.hash = md5(new.newsgroup);
-> END$$
ERROR 1362 (HY000): Updating of OLD row is not allowed in trigger
mysql>
mysql> quit;
-> exit
-> ^CCtrl-C -- exit!
Aborted
thufir#dur:~$
Also, from the workbench, I see the old trigger:
but don't see how to add an additional trigger.
It's not after update, it's before update, and new. Don't quite understand the syntax, but this at least doesn't generate syntax errors. Would've like to have use the workbench:
thufir#dur:~$
thufir#dur:~$ mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 144
Server version: 5.1.58-1ubuntu1 (Ubuntu)
Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.
This software comes with ABSOLUTELY NO WARRANTY. This is free software,
and you are welcome to modify and redistribute it under the GPL v2 license
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> use nntp;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> show triggers;
+-----------+--------+------------+-----------------------------------------------------+--------+---------+----------+----------------+----------------------+----------------------+--------------------+
| Trigger | Event | Table | Statement | Timing | Created | sql_mode | Definer | character_set_client | collation_connection | Database Collation |
+-----------+--------+------------+-----------------------------------------------------+--------+---------+----------+----------------+----------------------+----------------------+--------------------+
| make_hash | INSERT | newsgroups | BEGIN
set new.hash = md5(new.newsgroup);
END | BEFORE | NULL | | root#localhost | utf8 | utf8_general_ci | latin1_swedish_ci |
+-----------+--------+------------+-----------------------------------------------------+--------+---------+----------+----------------+----------------------+----------------------+--------------------+
1 row in set (0.00 sec)
mysql> show create trigger make_hash;
+-----------+----------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------+----------------------+----------------------+--------------------+
| Trigger | sql_mode | SQL Original Statement | character_set_client | collation_connection | Database Collation |
+-----------+----------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------+----------------------+----------------------+--------------------+
| make_hash | | CREATE DEFINER=`root`#`localhost` TRIGGER `nntp`.`make_hash`
BEFORE INSERT ON `nntp`.`newsgroups`
FOR EACH ROW
BEGIN
set new.hash = md5(new.newsgroup);
END | utf8 | utf8_general_ci | latin1_swedish_ci |
+-----------+----------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------+----------------------+----------------------+--------------------+
1 row in set (0.00 sec)
mysql>
mysql> DELIMITER $$
mysql>
mysql> USE `nntp`$$
Database changed
mysql> CREATE
-> TRIGGER `nntp`.`make_hash_update`
-> BEFORE UPDATE ON `nntp`.`newsgroups`
-> FOR EACH ROW
-> BEGIN
-> set new.hash = md5(new.newsgroup);
-> END$$
Query OK, 0 rows affected (0.19 sec)
mysql>
mysql> show triggers;
-> ^CCtrl-C -- exit!
Aborted
thufir#dur:~$
Related
I have the following two Triggers
cascadeUserInsertionToRecommendations_tbl
and
afterinsertiontousers
Now below, just for completeness, is the result of "show triggers;"
+-------------------------------------------+--------+-----------+--------------------------------------------------------------------------------------------------------------------------------------------------------+--------+------------------------+-----------------------------------------------------+---------+----------------------+----------------------+--------------------+
| Trigger | Event | Table | Statement | Timing | Created | sql_mode | Definer | character_set_client | collation_connection | Database Collation |
+-------------------------------------------+--------+-----------+--------------------------------------------------------------------------------------------------------------------------------------------------------+--------+------------------------+-----------------------------------------------------+---------+----------------------+----------------------+--------------------+
| cascadeUserInsertionToRecommendations_tbl | INSERT | users_tbl | INSERT INTO recommendations_tbl(recommendation_userid, recommendation_category, recommendation_manufacturer) VALUES(NEW.user_id, 'diverse', 'diverse') | AFTER | 2019-09-02 12:32:46.19 | NO_ZERO_IN_DATE,NO_ZERO_DATE,NO_ENGINE_SUBSTITUTION | # | latin1 | latin1_swedish_ci | latin1_swedish_ci |
| afterinsertiontousers | INSERT | users_tbl | INSERT INTO recommendations_tbl(recommendation_userid, recommendation_category, recommendation_manufacturer) VALUES(NEW.user_id, DEFAULT, DEFAULT) | AFTER | 2019-09-02 15:48:39.52 | NO_ZERO_IN_DATE,NO_ZERO_DATE,NO_ENGINE_SUBSTITUTION | # | latin1 | latin1_swedish_ci | latin1_swedish_ci |
+-------------------------------------------+--------+-----------+--------------------------------------------------------------------------------------------------------------------------------------------------------+--------+------------------------+-----------------------------------------------------+---------+----------------------+----------------------+--------------------+
Now the problem is that I just cant drop them :/
When I'm on foundationtests, the database where I created the triggers like so:
CREATE TRIGGER afterinsertiontousers
-> AFTER INSERT ON users_tbl
-> FOR EACH ROW
-> INSERT INTO recommendations_tbl(recommendation_userid, recommendation_category, recommendation_manufacturer) VALUES(NEW.user_id, DEFAULT, DEFAULT);
Query OK, 0 rows affected (0.016 sec)
this is what happens:
MariaDB [foundationtests]> DROP TRIGGER users_tbl.cascadeUserInsertionToRecommendations_tbl;
ERROR 1360 (HY000): Trigger does not exist
I already swapped to information_schema DB and deleted the above Trigger there. However, it still shows up here AND it still does its job, which it shouldnt...
I read about having to use "db5" on multiple other threads on SO, but I can't access this DB neither does it show up in my list of DBs when using "show databases";
So I'm somehow clueless on what I am missing Oo
When attempting to insert 💩 (for example, which is a 4-byte unicode char), both MySQL (5.7) and MariaDB (10.2/10.3/10.4) give the same error:
Incorrect string value: '\xF0\x9F\x92\xA9'
The statement:
mysql> insert into bob (test) values ('💩');
Here's my database's charset/collation:
mysql> select ##collation_database; +----------------------+
| ##collation_database |
+----------------------+
| utf8mb4_unicode_ci |
+----------------------+
1 row in set (0.00 sec)
mysql> SELECT ##character_set_database; +--------------------------+
| ##character_set_database |
+--------------------------+
| utf8mb4 |
+--------------------------+
1 row in set (0.00 sec)
The server's character set:
mysql> show global variables like '%character_set_server%'\G; *************************** 1. row ***************************
Variable_name: character_set_server
Value: utf8mb4
The table:
create table bob ( `test` TEXT NOT NULL );
mysql> SHOW FULL COLUMNS FROM bob;
+-------+------+--------------------+------+-----+---------+-------+---------------------------------+---------+
| Field | Type | Collation | Null | Key | Default | Extra | Privileges | Comment |
+-------+------+--------------------+------+-----+---------+-------+---------------------------------+---------+
| test | text | utf8mb4_unicode_ci | NO | | NULL | | select,insert,update,references | |
+-------+------+--------------------+------+-----+---------+-------+---------------------------------+---------+
1 row in set (0.00 sec)
Can anyone point me in the right direction?
Yes, as you commented, you need to use SET NAMES utf8mb4.
Your 4-byte character must pass from your client through the database connection and into a table. All of those must support utf8mb4. If any one of them does not support utf8mb4, then 4-byte characters will not be able to get through.
SET NAMES utf8mb4 makes the database session expect clients to send string using that encoding. The default for character_set_client on MySQL 5.7 is utf8, so you need to set it to utf8mb4.
In MySQL 8.0.1 and later, the default character_set_client is utf8mb4 already, so you won't need to change it.
I'm running MySQL Ver 15.1 Distrib 10.3.9-MariaDB for OSX10.13 and have a bunch of users (around 14) where the username is apparently too long, see below.
MariaDB [(none)]> SELECT Host, User FROM mysql.user;
+-----------+-------------------------+
| Host | User |
+-----------+-------------------------+
| 127.0.0.1 | tenant_2SxSBywyXh3QW5L4 |
| 127.0.0.1 | tenant_4j3CWVXjgtDD2OrI |
| 127.0.0.1 | tenant_5OOBxRgjSvFehwI2 |
| 127.0.0.1 | tenant_6C3vAqBTVns8rMTT |
| 127.0.0.1 | tenant_9PVCY1Msa61u43Oh |
| 127.0.0.1 | tenant_RyFspluIGEi3Fkby |
| 127.0.0.1 | tenant_SQtfEQZmWRFfKmHy |
| 127.0.0.1 | tenant_VC8pYaJ3it1LdYK3 |
| 127.0.0.1 | tenant_Zr84h3vkLdwkqR33 |
| 127.0.0.1 | tenant_cvifD1I2Rjghjnpu |
| 127.0.0.1 | tenant_jO1tUJJokremTW5P |
| 127.0.0.1 | tenant_p6Bhzhi0OqMH4gc9 |
| 127.0.0.1 | tenant_rjyOX1NqEN6k6mk4 |
| 127.0.0.1 | tenant_xfcHjsn0mHBfURAw |
| 127.0.0.1 | test_testing_local |
| localhost | mysql.session |
| localhost | mysql.sys |
| localhost | root |
+-----------+-------------------------+
18 rows in set (0.000 sec)
When I try to drop these users using the following command I'm in encountering this error:
MariaDB [(none)]> DROP USER 'tenant_5OOBxRgjSvFehwI2#127.0.0.1';
ERROR 1470 (HY000): String 'tenant_5OOBxRgjSvFehwI2#127.0.0.1' is too long for user name (should be no longer than 32)
MariaDB [(none)]> DROP USER 'tenant_5OOBxRgjSvFehwI2#localhost';
ERROR 1470 (HY000): String 'tenant_5OOBxRgjSvFehwI2#localhost' is too long for user name (should be no longer than 32)
Any idea's on how I can drop these users?
Most annoying side effect of this is when opening up SequalPro, and I get 14 errors (one after the other) notifying me of this user name being too long issue.
Also, another odd question that I'd be interested to hear about is why when these user name are only 22-24 characters long are they causing a ... too long for user name (should be no longer than 32) error, as they don't breach the 32 character limit?
Appreciate any help :)
You can try several options. Can try the latest test build of SequelPro. The test build (3477d22) seems to solve the long names issue for me.
Test Builds are here: https://sequelpro.com/test-builds
Or one of the latest development builds. http://nightly.sequelpro.com/
For me, with Mariadb 10.x server and nightly build, it also worked for me.
You have to comment the 'username' # 'host' ...
Use:
DROP USER 'tenant_5OOBxRgjSvFehwI2'#'127.0.0.1';
I can't reproduce the problem:
MariaDB [(none)]> SELECT VERSION();
+-----------------+
| VERSION() |
+-----------------+
| 10.3.11-MariaDB |
+-----------------+
1 row in set (0.000 sec)
MariaDB [(none)]> DESC mysql.user; -- User char(80)
+------------------------+-----------------------------------+------+-----+----------+-------+
| Field | Type | Null | Key | Default | Extra |
+------------------------+-----------------------------------+------+-----+----------+-------+
| Host | char(60) | NO | PRI | | |
| User | char(80) | NO | PRI | | |
.
.
.
+------------------------+-----------------------------------+------+-----+----------+-------+
47 rows in set (0.001 sec)
MariaDB [(none)]> CREATE USER 'tenant_5OOBxRgjSvFehwI2'#'127.0.0.1';
Query OK, 0 rows affected (0.001 sec)
MariaDB [(none)]> DROP USER 'tenant_5OOBxRgjSvFehwI2#127.0.0.1';
ERROR 1396 (HY000): Operation DROP USER failed for 'tenant_5OOBxRgjSvFehwI2#127.0.0.1'#'%'
MariaDB [(none)]> DROP USER 'tenant_5OOBxRgjSvFehwI2I'#'127.0.0.1'; -- Wrong user
ERROR 1396 (HY000): Operation DROP USER failed for 'tenant_5OOBxRgjSvFehwI2I'#'127.0.0.1'
MariaDB [(none)]> DROP USER 'tenant_5OOBxRgjSvFehwI2'#'127.0.0.1';
Query OK, 0 rows affected (0.001 sec)
In postgresql all you needed to do was type in postgres <database_name>
to access the database. In MySql, you need to do this:
mysql -u root
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 52
Server version: 5.7.20 Homebrew
Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> use arthouse_development
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> select * from catalog_items;
Empty set (0.00 sec)
mysql> select * from catalog_items
-> ;
Empty set (0.00 sec)
mysql> select * from users;
Empty set (0.01 sec)
mysql> select * from users
-> ;
Empty set (0.00 sec)
Is there a way to not have to specify the user name and specify the database all in one line?
How do you see all the tables in mysql once inside the database?
Use the -D option of the program mysql the default login with a database.
The -p option let's you enter a password into the mysql program
C:\Program Files\MySQL\MySQL Server 5.7\bin>mysql -u root -p -D stackoverflow
Enter password: **
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 10
Server version: 5.7.20-log MySQL Community Server (GPL)
Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
Confirm we are using the database stackoverflow.
mysql> select database();
+---------------+
| database() |
+---------------+
| stackoverflow |
+---------------+
1 row in set (0.00 sec)
How do you see all the tables in mysql once inside the database?
mysql> show tables;
+-------------------------+
| Tables_in_stackoverflow |
+-------------------------+
| baskets |
| baskets_fruits |
| conditions |
| employee |
| employees |
| hugetable |
| kanji |
| newtable |
| result |
| t1 |
| t2 |
| table1 |
| table_test |
| temp |
| test |
| testing |
| word |
+-------------------------+
17 rows in set (0.00 sec)
Or by using information_schema.TABLES table
mysql> SELECT TABLE_NAME FROM information_schema.TABLES WHERE TABLE_SCHEMA = 'stackoverflow';
+----------------+
| TABLE_NAME |
+----------------+
| baskets |
| baskets_fruits |
| conditions |
| employee |
| employees |
| hugetable |
| kanji |
| newtable |
| result |
| t1 |
| t2 |
| table1 |
| table_test |
| temp |
| test |
| testing |
| word |
+----------------+
17 rows in set (0.00 sec)
Other answers mention the --database / -D option, but you don't even need that.
https://dev.mysql.com/doc/refman/5.7/en/mysql.html shows the simpler example:
Using mysql is very easy. Invoke it from the prompt of your command
interpreter as follows:
shell> mysql db_name
In other words, exactly like the usage you showed for postgres <database_name>.
You may also need to supply username and password to authenticate for the MySQL instance, but I prefer to put those into an option file so I don't have to type them repeatedly.
$ cat ~/.my.cnf
[client]
username = scott
password = tiger
See https://dev.mysql.com/doc/refman/5.7/en/option-files.html for more on these files.
There's also an optional way to store the user credentials encrypted. For this https://dev.mysql.com/doc/refman/5.7/en/mysql-config-editor.html
Any software is easier to use if you try reading a little bit of the documentation! :-)
Using man mysql to view the mysql manpage you'll find the -D flag:
· --database=db_name, -D db_name
The database to use. This is useful primarily in an option file.
GOAL: to output all SQL queries and their outputs into a text file
SQL CODE:
\W /*enable warnings*/
USE bookdb; /*doesn't exist because I WILL DROP DATABASE booksdb BEFORE RUNNING THIS SCRIPT (to avoid duplicate entry errors from testing out the queries during the assignment)*/
/* Query 0 */
SELECT user(), current_date(), version(), ##sql_mode\G
/*Query 1*/
CREATE DATABASE IF NOT EXISTS bookdb;
Use bookdb;
/*QUERY 2*/
CREATE TABLE books (
isbn CHAR(10),
author VARCHAR(100) NOT NULL,
title VARCHAR(128) NOT NULL,
price DECIMAL(7 , 2 ) NOT NULL,
subject VARCHAR(30) NOT NULL,
PRIMARY KEY (isbn)
)ENGINE = INNODB;
/*QUERY 3*/
INSERT INTO books
VALUES ('0345377648', 'Anne Rice', 'Lasher', 14.00, 'FICTION');
INSERT INTO books
VALUES ('1557044287','Ridley Scott','Gladiator',26.36,'FICTION');
INSERT INTO books
VALUES ('0684856093', 'Sean Covey', 'The 7 Habits', 12, 'CHILDREN');
/*QUERY 4*/
SHOW TABLES;
/*QUERY 5*/
DESC books;
/*QUERY 6*/
SELECT * FROM books;
/*QUERY 7*/
SELECT ISBN, title, price FROM books;
COMMANDS FROM SQL PROMPT:
mysql> tee /my_scripts/yourname_assignment1.txt
mysql> source /my_scripts/yourname_assignment1.sql
mysql> notee
RESULTING assignment1.txt FILE:
user(): root#localhost
current_date(): 2016-09-25
version(): 5.7.15-0ubuntu0.16.04.1
##sql_mode: ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_ FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
1 row in set (0.00 sec)
Query OK, 1 row affected (0.00 sec)
Database changed
Query OK, 0 rows affected (0.32 sec)
Query OK, 1 row affected (0.06 sec)
Query OK, 1 row affected (0.04 sec)
Query OK, 1 row affected (0.08 sec)
+------------------+
| Tables_in_bookdb |
+------------------+
| books |
+------------------+
1 row in set (0.00 sec)
+---------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+--------------+------+-----+---------+-------+
| isbn | char(10) | NO | PRI | NULL | |
| author | varchar(100) | NO | | NULL | |
| title | varchar(128) | NO | | NULL | |
| price | decimal(7,2) | NO | | NULL | |
| subject | varchar(30) | NO | | NULL | |
+---------+--------------+------+-----+---------+-------+
5 rows in set (0.00 sec)
+------------+--------------+--------------+-------+----------+
| isbn | author | title | price | subject |
+------------+--------------+--------------+-------+----------+
| 0345377648 | Anne Rice | Lasher | 14.00 | FICTION |
| 0684856093 | Sean Covey | The 7 Habits | 12.00 | CHILDREN |
| 1557044287 | Ridley Scott | Gladiator | 26.36 | FICTION |
+------------+--------------+--------------+-------+----------+
3 rows in set (0.00 sec)
+------------+--------------+-------+
| ISBN | title | price |
+------------+--------------+-------+
| 0345377648 | Lasher | 14.00 |
| 0684856093 | The 7 Habits | 12.00 |
| 1557044287 | Gladiator | 26.36 |
+------------+--------------+-------+
3 rows in set (0.00 sec)
mysql> notee
As you can see, my queries are executed without errors (ie. they are coming out correctly), but the output is not listing the queries themselves.
My professor's "example.txt" file includes the queries that are listed in the assignment1.sql file. In other words, his output apparently includes his queries in the SQL file where my output does not. How do you change the "tee" command to include the queries? Did my professor simply manually edit a copy of the file or am I missing something?
I realize that if I manually enter the commands, my output will look more like his (ie. with the queries), but that's not the way he explained this assignment.
I'm running the latest x64 Ubuntu OS if that might have an effect on the SQL. I'm new to this. I've thoroughly searched online for this specific issue, but no one has this one listed.
Thank you.
ASSIGNMENT DIRECTIONS IF THIS CLARIFIES ANYTHING:
Run the following command:
mysql -u root –p --force --comments –vvv
Use the tee command to put your output in a text file to submit.
mysql> tee c:/my_scripts/yourname_assignment1.txt
Run the SQL script hibrahim_assignment1.sql
mysql> source c:\my_scripts\yourname_assignment1.sql
Type in notee to stop the tee command.
mysql> notee
I fixed it. It was because my Linux machine was not giving permissions correctly. The command
mysql -u root -p --force --comments -vvv
required elevation (and didn't require root). I guess I was missing the --force --comments -vvv hence the queries and comments not being included. So for future reference, you'll need elevation. Furthermore, I didn't need to specify root. So the resulting command is
sudo mysql --force --comments -vvv