Let's assume I have the below table
mysql> desc countrylist;
+---------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+-------+
| country | varchar(32) | YES | | NULL | |
+---------+-------------+------+-----+---------+-------+
1 row in set (0.02 sec)
While querying the table, I always want 'USA' in the result whether or not the value is there in the table, in addition to other countries in the table. How do I handle that?
TIA.
James.
You can use UNION:
SELECT country FROM countrylist
UNION
SELECT 'USA'
Related
I have the following table in MySQL database:
mysql> describe student;
+------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+------------+-------------+------+-----+---------+-------+
| name | varchar(50) | YES | | NULL | |
| dob | date | YES | | NULL | |
| reg_no | varchar(20) | NO | PRI | NULL | |
| department | varchar(50) | YES | | NULL | |
| branch | varchar(50) | YES | | NULL | |
| semester | int(11) | YES | | 1 | |
+------------+-------------+------+-----+---------+-------+
6 rows in set (0.01 sec)
When I try to insert into this database without specifying the last value (i.e.semester), it results in an error
mysql> insert into student values('John Smith','1990-01-01','123ABC','Chemistry','Organic Chemistry');
ERROR 1136 (21S01): Column count doesn't match value count at row 1
Why is this not working? Isn't that the whole point of having a default constraint? That the value specified as default gets assigned if it isn't present in the insert query.
If I write the query like this, it works:
mysql> insert into student values('John Smith','1990-01-01','123ABC','Chemistry','Organic Chemistry',DEFAULT);
Query OK, 1 row affected (0.01 sec)
What is happening? Is my syntax wrong? I would like to be able to insert the record without having to specify 'DEFAULT'
If you are not adding values to all the table columns you should specify which columns you add data.
Example:
INSERT INTO table_name (column1, column2, column3, ...)
VALUES (value1, value2, value3, ...);
So in your case,
INSERT INTO student(name, dob, regno, department, branch)
VALUES ('John Smith','1990-01-01','123ABC','Chemistry','Organic Chemistry');
your syntax is wrong while inserting default values,so instead of that query use insert into student(name,dob,reg_no,department,branch)values('John Smith','1990-01-01','123ABCF','Chemistry','Organic Chemistry');
Consider 2 queries:
SELECT * FROM T1 WHERE NAME LIKE '%\%%';
SELECT * FROM T1 WHERE NAME LIKE '%\%\%%';
Assume that T1 has records where NAME is %, %%, or %%%.
I would expect the second query to return fewer results but it is including the record where T1.NAME = '%'! Is there a way to filter out that record using like query? Something like SELECT * FROM T1 WHERE NAME LIKE '%\%\%%' AND NAME <> '%'; is not what I am looking for.
mysql> explain table1;
+-------+------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+------------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| text | varchar(4) | YES | | NULL | |
+-------+------------+------+-----+---------+-------+
2 rows in set (0.00 sec)
mysql> explain select * from table1
-> where text like '%\%%';
+----+-------------+--------+------+---------------+------+---------+------+------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+--------+------+---------------+------+---------+------+------+-------------+
| 1 | SIMPLE | table1 | ALL | NULL | NULL | NULL | NULL | 4 | Using where |
+----+-------------+--------+------+---------------+------+---------+------+------+-------------+
1 row in set (0.00 sec)
mysql> explain select * from table1
-> where text like '%\%\%%';
+----+-------------+--------+------+---------------+------+---------+------+------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+--------+------+---------------+------+---------+------+------+-------------+
| 1 | SIMPLE | table1 | ALL | NULL | NULL | NULL | NULL | 4 | Using where |
+----+-------------+--------+------+---------------+------+---------+------+------+-------------+
1 row in set (0.00 sec)
Here is my config if it helps:
[mysqld]
innodb_buffer_pool_size=402653184
innodb_log_file_size=262144000
innodb_log_buffer_size=8388608
max_allowed_packet=5241856
innodb_additional_mem_pool_size=20971520
I need to investigate further but it appears that the problem has something to do with the way that the database is being created:
create database testdb character set utf8 collate utf8_unicode_ci;
I was able to get the correct results when I create the database in a less specific way:
create database testdb;
Any idea why?
As far as I can tell, this appears to be a bug in mysql... it is sometimes impossible to search for consecutive percent signs using LIKE. (I was experiencing the same issue you described)
Still not sure what conditions are required to trigger the bug.
One workaround, however, would be to use REGEXP:
SELECT * FROM T1 WHERE NAME REGEXP "%%";
I have a table contains user infos.
mysql> desc accounts;
+-------------+------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------+------------+------+-----+---------+----------------+
| cid | int(11) | NO | PRI | NULL | auto_increment |
| username | text | YES | | NULL | |
| password | text | YES | | NULL | |
| mysignature | text | YES | | NULL | |
| is_admin | varchar(5) | YES | | NULL | |
+-------------+------------+------+-----+---------+----------------+
5 rows in set (0.01 sec)
Normal group by statement always follows by one or more column names like below:
select * from accounts group by cid;
Just an example, in general group by works with Aggregate functions like sum().
I have found some follows by an expression without column names which I cant understand:
mysql> select username from accounts group by now();
+----------+
| username |
+----------+
| admin |
+----------+
1 row in set (0.00 sec)
I am new in MySQL. How does this query work ?
Thanks.
In fact it's expression. It compares expression results instead of column values for the grouping.
now() function with the group by is not so effective.
It will always return the first row from the selected data.
You always need to specify the column name in the group by clause in query. Here are link of tutorial of group by.
It specify that which aggregate function you can use with group by.
now() with group by return that will select the data from the query and match the first and it will be first row.
Ok let me rephrase. This is what my table is described as.
mysql> describe department;
+---------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+-------+
| dnumber | int(1) | NO | PRI | NULL | |
| dname | varchar(15) | YES | | NULL | |
| mgrssn | varchar(9) | YES | | NULL | |
+---------+-------------+------+-----+---------+-------+
3 rows in set (0.00 sec)
This is what I need my table to be described as:
+--------------+-------------+------+-----+---------+
| Field | Type | Null | Key | Default |
+--------------+-------------+------+-----+---------+
| dnumber | int(1) | NO | PRI | NOT NULL|
| dname | varchar(15) | YES | | NULL |
| mgrssn | varchar(9) | YES | | NULL |
+--------------+-------------+------+-----+---------+
How do I change the default of dnumber to make it not null from null?
Since dnumber is the primary key of the department table, it cannot have a null value.
So, what will happen if you actually try to insert the following?
INSERT INTO department(dname, mgrssn) VALUES ("test", "123456789");
You'd expect the insert to fail with an error. But no, it succeeds, (albeit, with a warning):
mysql> INSERT INTO department(dname, mgrssn) VALUES ("test", "123456789");
Query OK, 1 row affected, 1 warning (0.04 sec)
What just happened? Well, turns out to be one of MySQL's many gotchas, where "NULL is NOT NULL". Today's MySQL manual says pretty much the same thing:
If a column definition includes no explicit DEFAULT value, MySQL determines the default value as described in Section 11.5, “Data Type Default Values”.
So, what actually got inserted? Let's take a look:
mysql> select * from department;
+---------+-------+-----------+
| dnumber | dname | mgrssn |
+---------+-------+-----------+
| 0 | test | 123456789 |
+---------+-------+-----------+
1 row in set (0.00 sec)
What happened here was that MySQL took that insert statement that was implicitly trying to insert NULL into dnumber and quietly converted it to the default value for INT, or 0.
There's also a really good video that describes this and other strange things MySQL does (as compared to PostgreSQL) in more detail.
Finally, let me add one more piece of advice - since it is the primary key, it's a really good practice to make dnumber an auto-incrementing column, like this:
mysql> ALTER TABLE department MODIFY dnumber int(1) AUTO_INCREMENT;
Query OK, 1 row affected (0.27 sec)
Records: 1 Duplicates: 0 Warnings: 0
mysql> desc department;
+---------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+----------------+
| dnumber | int(1) | NO | PRI | NULL | auto_increment |
| dname | varchar(15) | YES | | NULL | |
| mgrssn | varchar(9) | YES | | NULL | |
+---------+-------------+------+-----+---------+----------------+
3 rows in set (0.00 sec)
That way, future inserts into department won't always try to insert another 0 for dnumber causing a duplicate key error, but will instead use the next lowest available integer value, e.g. 1, then 2, then 3, etc.
Hope this helps!
as picture(phpmyadmin),I have tables in the database week1 the tables are now empty.
I have another databases with exactly the same sql schema w1moninside and w1monoutside
but they have values.
I want to union these two and insert them in week1
what should I do?
I inserted the picture just for clearance.unoin for one of the tables for example the first one, data
is enough for me
Thanks.
mysql> describe w1moninside.data;
+--------------+------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------------+------------------+------+-----+---------+-------+
| sid | int(10) unsigned | NO | PRI | NULL | |
| cid | int(10) unsigned | NO | PRI | NULL | |
| data_payload | text | YES | | NULL | |
+--------------+------------------+------+-----+---------+-------+
3 rows in set (0.01 sec)
Insert into week1.TableName (column list)
select column list from w1moninside.TableName where...
union
select column list from w1monoutside.TableName where
Insert into week1 (column list)
select column list from dbname.w1moninside where...
union
select column list from dbname.w1monoutside where..