How to insert row with next id in mysql - mysql

I'm using mysql 8 and I'm a big noob so be polite please, I have a table with only 2 columns, ID (Primary key, Not Null, Auto Increment, Unique) and Password (Not Null, Unique)
I'd like to have something like this:
+----+------------+
| id | Password |
+----+------------+
| 1 | dsakjhsajs |
+----+------------+
| 2 | xczkcjhczx |
+----+------------+
| 3 | treiuytreu |
+----+------------+
Is it possible to INSERT a new password without specifying the id and let the db calculate the id?
Because if I use
INSERT INTO myTable VALUES ('anotherpassword')
I obviously get an error

Name the columns for which you will be inserting values:
INSERT into mytable (passwordcol) VALUES ('password1');
The word "password" is a keyword, so avoid that as a column name.

insert has a syntax to specify columns:
INSERT INTO myTable(`Password`) VALUES ('anotherpassword')
ps. Don't store clear text passwords.

Related

MySql enter duplicate rows into table

I would like to enter two of the same emails as two different rows into MySQL table Person. However, it keeps only one row. How do I modify my code? Thanks.
create table if not exists Person (
Id int auto_increment primary key,
Email varchar(20)
);
insert into Person(Email)
values ('abc#efg.com'),
('abc#efg.com')
;
Your query looks ok
SQL fiddle demo
OUTPUT
| Id | Email |
|----|-------------|
| 1 | abc#efg.com |
| 2 | abc#efg.com |

How to force mySQL NOT to convert varchar to numeric in WHERE comparison

When a table containing a column of type varchar contains data which could be converted to a numeric value, such as '100', mySQL (helpfully??) converts the WHERE string to a numeric value and returns a match for
WHERE column = '100'
WHERE column = '100.00'
WHERE column = '100abc'
WHERE column = '100areyoukiddingme'
I want to return only exact matches, but don't want to use the
WHERE BINARY column = as that would force a case-sensitive search when the value is not a numeric, which I don't want. I also want to avoid using REGEX or LIKE as that would involve a table scan.
The search string is an ad-hoc value entered by the end-user, which may or may not be numeric, and the values in the table likewise.
How do I test for an exact match other than doing a double check on the column in PHP to check that the column does indeed match. Something like
WHERE column === '100abc'
would be nice.
EDIT
This is not how mySQL behaves, it was my error in not checking the table I was testing with - the column was not defined as varchar as expected, but INT(11) therefore mySQL was casting all strings to integer correctly.
create table t7
( id int auto_increment primary key,
thing varchar(100) not null
);
insert t7(thing) values ('100'),('100kidding');
select * from t7 where thing='100';
+----+-------+
| id | thing |
+----+-------+
| 1 | 100 |
+----+-------+
select * from t7 where thing='100kidding';
+----+------------+
| id | thing |
+----+------------+
| 2 | 100kidding |
+----+------------+
select * from t7 where thing=100;
+----+------------+
| id | thing |
+----+------------+
| 1 | 100 |
| 2 | 100kidding |
+----+------------+
You know it is a string (meaning you know your schema has a varchar). So, pass a string regardless.

Insert random string into field with MySQL UNI key

The task is to fill the table with N rows of random unique data.
I have the next MySQL table structure:
+----------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| username | varchar(20) | NO | UNI | NULL | |
+----------+--------------+------+-----+---------+----------------+
Username field has string type but if the script will insert numbers its OK.
Theres is dirty solution with INSERT IGNORE, that can make 1000 random rows with endless cycle.
INSERT IGNORE INTO table (id,username) VALUES ('', 1 + ceil(rand() * 1000));
Also, I can use ON DUPLICATE KEY structure, but this 2 solutions are not OK.
I want to make the query, that generate unique username which will be unique and will be inserted from the first time.
So, I tell the script to add 1m of rows and it will insert 1m of unique data without any infelicities.
Any ideas? Thanks.
You can use UUID() which will give you a random string which would fit in your field, guaranteed to be unique with slim chance of collisions.

Get the auto increment-primary key column programatically

witch is the auto increment field in a mysql table?
I have a table structure, for example:
table name is my_table and the fields are my_id, my_name, my_blah.. The one of fields is an auto incremented primary key. Witch is it?
How can I get the name of auto increment field on this table with a php code and/or a mysql query?
You can use the MySQL SHOW COLUMNS query to retrieve information about the columns in a table:
mysql> SHOW COLUMNS FROM `test`;
+----------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| col1 | varchar(100) | YES | | NULL | |
| col2 | int(11) | YES | | NULL | |
+----------+--------------+------+-----+---------+----------------+
The extra column will contain auto_increment for the primary key field.
You could try with a
SHOW COLUMNS FROM TableName
See here: http://dev.mysql.com/doc/refman/5.0/en/show-columns.html
Use a MySQL client and issue the query SHOW CREATE TABLE my_table.
It shows you the code one needs to run to create that table. You can see the column names, types (and lengths), other attributes each column may have (they depend on the type). AUTO_INCREMENT is such an attribute.
It also shows you the PK and the indexes of the table.

Referencing code value tables in SQL

I have just started working with MySQL and have a quick question. I would like to create a table "My_Table" that has a field "SEX". I would also like to create a table "SEX_Values" which has fields "CODE" and "VALUE" with 0 = male, 1 = female. so that it looks like this.
My_Table SEX_Values
+--------+ +--------+--------+
| SEX | | CODE | VALUE |
+--------+ +--------+--------+
| | | 0 | male |
+--------+ +--------+--------+
| 1 | female |
+--------+--------+
I would like to somehow put a constraint on the SEX field in My_Table so that the data that is inserted/imported into it must match one of the codes in the SEX_Values table and if it doesn't I would like to throw a warning, something like this.
My_Table SEX_Values
+--------+ +--------+--------+
| SEX |<reference>| CODE | VALUE |
+--------+ +--------+--------+
| 1 | >OK | 0 | male |
+--------+ +--------+--------+
| 0 | >OK | 1 | female |
+--------+ +--------+--------+
| 0 | >OK
+--------+
| 3 | >Throws Warning
+--------+
Any help would be greatly appreciated as I have not used SQL much before.
What you describe is known as a FOREIGN KEY.
Essentially, it ensures referential integrity - in other words, you can't insert anything that doesn't exist in the referenced table, nor can you delete anything from the referenced table that still exists in the main one.
So, in your case you couldn't insert Hermaphrodite into My_Table without it being present in SEX_Values, nor could you remove male from SEX_Values, if there were still a male in My_Table.
CREATE TABLE SEX_Values
(
`CODE` INT NOT NULL,
`VALUES` VARCHAR(10),
PRIMARY KEY (`CODE`) -- !!
);
CREATE TABLE My_Table
(
SEX INT,
FOREIGN KEY (SEX) REFERENCES SEX_VALUES(`CODE`) -- !!
);
Why not use an enum type?
You can create the table with
CREATE TABLE my_table(
sex enum("male", "female") NOT NULL
);
That way you can insert like so:
INSERT INTO my_table(`sex`) VALUES("male")
You have the syntactical advantage of using strings (I think this is a lot more clear than using numeric codes), MySQL will optimize the database as if you were using the codes, and it will enfor.
A very simple solution is to set the datatype of the sex column to bit, that accepts only 0, 1 or null values (MSDN ref.).
Set also the NOT NULL constraint in the filed definition, in order to exclude NULL values as well.