I created a table in mysql. Every column in my table I created with NOT NULL. What does the NULL value in my DEFAULT column mean? I never plan to have a NULL value in those columns.
+------------+--------------+------+-----+-------------------+-------+
| Field | Type | Null | Key | Default | Extra |
+------------+--------------+------+-----+-------------------+-------+
| user_id | binary(16) | NO | PRI | | |
| first_name | varchar(255) | NO | | NULL | |
| last_name | varchar(255) | NO | | NULL | |
| email | varchar(255) | NO | UNI | NULL | |
| digest | binary(40) | NO | | NULL | |
| join_ts | timestamp | NO | | CURRENT_TIMESTAMP | |
+------------+--------------+------+-----+-------------------+-------+
This means you don't have a default value for these columns.
If you have a default value, you can create an insert query without that column.
With your table, you must give a value to all the null default's columns
Related
This is the default of the table:
desc extender_device;
+-------------------------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------------------------+--------------+------+-----+---------+-------+
| sn | varchar(16) | NO | PRI | NULL | |
| syncing_stage | longtext | NO | | NULL | |
| created_at | datetime(6) | NO | | NULL | |
| updated_at | datetime(6) | NO | | NULL | |
+-------------------------+--------------+------+-----+---------+-------+
If I was to insert an entry on that table on sql command where the syncing_stage is of blank text, What is the sql looks like. here is the one I have tried and failed:
insert into extender_device ( sn, syncing_stage) values ( 'AX123', '{}');
as I am using the wrong data for the "syncing_stage" column.
Thanks,
Jack
Initially the Primary Key was
(caseId,osName)
+--------------------+---------------+------+-----+-------------------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------------------+---------------+------+-----+-------------------+-------+
| createdBy | varchar(200) | NO | | NULL | |
| createdOn | timestamp | NO | | CURRENT_TIMESTAMP | |
| osName | varchar(200) | NO | | NULL | |
| caseId | varchar(100) | NO | MUL | NULL | |
| issueType | varchar(100) | NO | | NULL | |
| observationState | varchar(60) | YES | | NULL | |
| problemSolving | int(11) | NO | | NULL | |
| structure | int(11) | NO | | NULL | |
| logicalTransition | int(11) | NO | | NULL | |
| ownership | int(11) | NO | | NULL | |
| expectationSetting | int(11) | NO | | NULL | |
| empathy | int(11) | NO | | NULL | |
| valueAdd | int(11) | NO | | NULL | |
| comments | varchar(2000) | NO | | NULL | |
| version | int(11) | YES | | 0 | |
| showcase_escalate | varchar(30) | YES | | NULL | |
| ringback_state | varchar(200) | YES | | NULL | |
| trt_state | varchar(200) | YES | | NULL | |
| srp_state | varchar(200) | YES | | NULL | |
| id | int(11) | NO | PRI | NULL | |
+--------------------+---------------+------+-----+-------------------+-------+
In the above schema, id was AUTO INCREMENT. I removed that so I could make caseId as PRIMARY KEY. But since there are multiple records with same caseId I am not able to make the change.
At the end I just want caseId as my Primary Key.
Initially the Primary Key was (caseId,osName).
As defined in the MySQL Glossary:
primary key
A set of columns -- and by implication, the index based on this set of columns -- that can uniquely identify every row in a table. As such, it must be a unique index that does not contain any NULL values.
InnoDB requires that every table has such an index (also called the clustered index or cluster index), and organizes the table storage based on the column values of the primary key.
When choosing primary key values, consider using arbitrary values (a synthetic key) rather than relying on values derived from some other source (a natural key).
See Also clustered index, index, natural key, synthetic key.
Since you have multiple records with the same value for caseId, caseId alone cannot "uniquely identify every row" in your table; and therefore caseId alone cannot be a PRIMARY KEY.
select count(distinct (mobile)) from number_data where value = 'A_HNI';
count(distinct (mobile)) = 5046082
select count(mobile) from number_data where value = 'A_HNI';
count(mobile) = 9658150
There are 4612068 duplicates of mobile numbers in value = 'A_HNI'. I want to delete the duplicates and keep the original . The table has no primary key nor indexing . I can't assign a primary key but indexing is possible . The table is as follows :
Field Type Null Key Default Extra
title | varchar(255) | YES | | NULL | |
name | varchar(255) | YES | | NULL | |
age | varchar(255) | YES | | NULL | |
pincode | varchar(255) | YES | | NULL | |
city | varchar(255) | YES | | NULL | |
state | varchar(255) | YES | | NULL | |
mobile | varchar(255) | YES | | NULL | |
source | varchar(255) | YES | | NULL | |
value | varchar(255) | YES | | NULL | |
dnd | varchar(255) | YES | | NULL | |
msc | varchar(255) | YES | | NULL | |
operator | varchar(255) | YES | | NULL | |
Get the Distinct value from your table ,create a new table with it then delete the old table
For reference you can check this page
Remove Duplicate Value
You can also refer this stack answerenter link description here
This is my tables structures
mysql> DESCRIBE sections;
+----------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+----------------+
| sec_id | int(5) | NO | PRI | NULL | auto_increment |
| sec_name | varchar(45) | YES | | NULL | |
| sec_type | tinyint(4) | YES | | NULL | |
+----------+-------------+------+-----+---------+----------------+
mysql> DESCRIBE subjects;
+-----------------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------------+-------------+------+-----+---------+----------------+
| sub_id | int(5) | NO | PRI | NULL | auto_increment |
| sub_name | varchar(45) | YES | | NULL | |
| sections_sec_id | int(5) | NO | MUL | NULL | |
+-----------------+-------------+------+-----+---------+----------------+
mysql> DESCRIBE chapters;
+-----------------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------------+-------------+------+-----+---------+----------------+
| chp_id | int(5) | NO | PRI | NULL | auto_increment |
| chp_name | varchar(45) | YES | | NULL | |
| subjects_sub_id | int(5) | NO | MUL | NULL | |
| sections_sec_id | int(5) | NO | MUL | NULL | |
+-----------------+-------------+------+-----+---------+----------------+
mysql> DESCRIBE questions;
+-----------------+-----------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------------+-----------+------+-----+---------+----------------+
| que_id | int(11) | NO | PRI | NULL | auto_increment |
| que_text | text | YES | | NULL | |
| que_created | timestamp | YES | | NULL | |
| chapters_chp_id | int(5) | NO | MUL | NULL | |
| subjects_sub_id | int(5) | NO | MUL | NULL | |
| sections_sec_id | int(5) | NO | MUL | NULL | |
+-----------------+-----------+------+-----+---------+----------------+
mysql> DESCRIBE answers;
+------------------+-----------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------------+-----------+------+-----+---------+----------------+
| ans_id | int(11) | NO | PRI | NULL | auto_increment |
| ans_text | text | YES | | NULL | |
| ans_created | timestamp | YES | | NULL | |
| questions_que_id | int(11) | NO | MUL | NULL | |
| chapters_chp_id | int(5) | NO | MUL | NULL | |
| subjects_sub_id | int(5) | NO | MUL | NULL | |
| sections_sec_id | int(5) | NO | MUL | NULL | |
+------------------+-----------+------+-----+---------+----------------+
Let's assume 'Answers' table
I created the 'ans_id' as the primary key of 'Answers' table, created a another UNIQUE key using (ans_id,questions_que_id,chapters_chp_id,subjects_sub_id,sections_sec_id)
What i want do.
create primary key for 'Answers' table using (ans_id,questions_que_id,chapters_chp_id,subjects_sub_id,sections_sec_id) columns and create Trigger before INSERT to create 'auto_increment' id.
Finally i want to accomplish this
Definition: Primary key is minimal set of attributes needed to determine every row in a table.
UNIQUE index should be {questions_que_id} + {chapters_chp_id} + {subjects_sub_id} +{sections_sec_id} and make sure it is combined as one index with these 4 columns, if you set it as different 4 indexes than you will probably achieve undesired outcome, as Zohar already pointed. From my experience, it could be unpractical to use composite keys like this. For basic table operations (CRUD) you will have to provide a lot of parameters.
Maybe to try this way: autoincremental numeric id as a primary key, the rest of important attributes as UNIQUE index. Then you could count already inserted answers to make sure right number will be assigned to ans_id. Hint: it's maybe easier to achive this from your application layer, not as a trigger or procedure for MySQL. If your users will provide answers as step-by-step, then just save a timestamp and you will know which answer is older so you don't need ans_id at all.
i have a 'results' table that has a datetime colum 'created_time'.
this table reference another table 'results_values' (results.id reference results_values.results_id). here are tables describe:
mysql> describe webforms_results;
+------------------+------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------------+------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| webform_id | int(11) | NO | | NULL | |
| store_id | int(11) | NO | | NULL | |
| customer_id | int(11) | NO | | NULL | |
| customer_ip | bigint(20) | NO | | NULL | |
| created_time | datetime | NO | | NULL | |
| update_time | datetime | NO | | NULL | |
| approved | tinyint(1) | NO | | NULL | |
| view_on_frontend | tinyint(1) | NO | | 0 | |
+------------------+------------+------+-----+---------+----------------+
mysql> describe webforms_results_values;
+--------------+----------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------------+----------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| result_id | int(11) | NO | MUL | NULL | |
| field_id | int(11) | NO | | NULL | |
| value | text | NO | | NULL | |
| rating | int(11) | YES | | NULL | |
| created_time | datetime | YES | | NULL | |
+--------------+----------+------+-----+---------+----------------+
now i've added a datetime column 'created_time' on 'results_values', and i want to insert datetimes on the second tables copying them from the first table on the corresponding id. is there a quick way to do it?
yes triggers are the solution. And if you want to update already inserted data, you can do :
UPDATE webforms_results_values wrv INNER JOIN webforms_results wr
ON wrv.result_id = wr.id
SET wrv.created_time = wr.created_time
Create "Triggers" to update each and every update of those tables.
Please refer following URL:
Triggers