How to insert data in a column that was created with alter table?
772
I'm new to sql, the strange thing is that I use the alter table command to create a column called "height" when I insert his height look what happened.
It is a lack of control that alter table only creates columns with null and when inserting data they run.
From what you show, I think you have a confusion regarding instructions.
Insert data refers to inserting new rows to the table. So every time you insert something new rows will be created.
When you create a column, all existing rows will have that column with the "value" NULLrepresenting an empty. This only if there is no default value for the column.
What you want to do is update the already existing row to change the NULL value to the correct value. That is done with the instruction UPDATE.
From what you show, I think you have a confusion regarding instructions.
Insert data refers to inserting new rows to the table. So every time you insert something new rows will be created.
When you create a column, all existing rows will have that column with the "value"
NULL
representing an empty. This only if there is no default value for the column.What you want to do is update the already existing row to change the NULL value to the correct value. That is done with the instruction
UPDATE
.If you want to create a table, you don't need to use ALTER TABLE unless you want to modify a table that already exists.
CREATE TABLE is used to create a new table, where the information is actually stored. ALTER TABLE is used to modify an existing table.
Example of how to use ALTER TABLE:
So with this you can see that you normally modify columns, and add one new field per column to the table, another real world example:
And if what you want is that it does not appear: NULL, you must say NOT NULL at the end, to add a function to the column definition, like this:
UPDATE
In your case, it should be something like this:
And to create a table, it's like this:
I hope I have helped you, for more information you can go to: https://www.w3schools.com/sql/sql_create_table.asp