HI I added a column to the table with just this, do i need to add anything else? I have seen our previous consultant add more with same data type
but regrettably I did not note that.
ALTER TABLE ZRS.PMAPS
ADD COUNTY nvarchar(MAX)
while adding a new column in to a table with alter.
the column must be null. else it will raise an error.
nothing required along with u'r syntax if u wanna add constraint.
Related
I'm trying to change a column from DATE to DATETIME in mySQL using sqlfiddle. But I'm just getting syntax errors, when looking at other stack overflow answers it seems like this method should work? What am I doing wrong? Is the only way (is it safer?) to add a new column, update new column with old and drop the old one?
My current method (assume there is data already in the table after creation)
CREATE TABLE re ( file_id INT NOT NULL AUTO INCREMENT PRIMARY KEY, mydate DATE NOT NULL);
ALTER TABLE re alter column "mydate" DATETIME NOT NULL
The above doesn't work on sqlfiddle but it's the most common answer, other than the previous way. Am I doing something wrong?
There is, at least, one errors in you question:
AUTO INCREMENT should have an underscore between AUTO and INCREMENT.
The ALTER TABLE statement should be written with MODIFY COLUMN, and not with ALTER COLUMN. (This is a possible bug in MySQL?)
see: DBFIDDLE
I created a bug-report for this: https://bugs.mysql.com/bug.php?id=109461
P.S.: the time part of the DATETIME field will be set to '00:00:00', see: DBFIDDLE
EDIT: Response from [22 Dec 13:36] MySQL Verification Team
Thank you for your bug report.
However, it is not a bug.
Our Reference Manual clearly states that ........ ALTER COLUMN
....... and ....... MODIFY COLUMN ..... have two very different
scopes, with the first one being reduced to the changes for only
couple of keywords.
Re-reading the docs, I read: ALTER: "Used only to change a column default value."
and MODIFY COLUMN
Can change a column definition but not its name.
More convenient than CHANGE to change a column definition without renaming it.
With FIRST or AFTER, can reorder columns.
i knowCREATE TABLE new LIKE old;,but it can't copy fields to a existing table
i want to add the field,type and collation to an existing table
is there any simple way to do it?
You would have to SHOW CREATE TABLE old then copy & paste the columns you want to add to your existing table, and use them in an ALTER TABLE statement . You can use ADD COLUMN to add multiple columns in one statement.
There's no shortcut syntax for this. It's unlikely that you would want to copy all the columns. For example, you can't copy columns that conflict with column names in the existing table.
There are edge cases, like what if a column has NOT NULL but no DEFAULT? You can't add that to an existing table unless the table is empty.
You just have to build the ALTER TABLE statement to suit what you want to do. This is similar to most other custom programming scenarios.
I am working on a SQL table and would like to create and insert a new column into the same table. The thing is, I would like my new column to be at a specific place in the middle of the table, rather than at the end, and there does not seem to be a method of doing this exact insertion in the documentation.
So, the name of my table is "actor" and its columns are "actor_id", "first_name", "last_name", and "last_update". I would like to create a column called "middle_name" between the columns "first_name" and "last_name". My code that I tried out was
ALTER TABLE actor
ADD middle_name VARCHAR(25);
However this just adds the column to the end of the table and not the middle as I want. Are there any suggestions for how to correct this?
It goes simple as:
ALTER TABLE actor
ADD COLUMN middle_name VARCHAR(25) AFTER first_name;
In MySQL Workbench you can open the table editor (via the context menu in the schema tree -> Alter table...). On the columns tab you see all currently defined columns. You can add new ones, remove unneeded ones and drag them into the order you want. Then apply the changes. The upcoming wizard with allow you to review of the code which will be sent to the server. Existing data should not be touched (unless you drop a column with data), but it's certainly safer to keep a backup around in case something goes wrong.
Is it possible to add new columns to an existing table without using alter statement?
Other people are answering unequivocally "no, it is not possible." This is the answer to your literal question. But I'm wondering why you ask the question.
One of the biggest pain points of MySQL is that using ALTER TABLE locks the table while you're making a change like adding a column, and the more data in your table, the longer this lasts while it restructures the table. I'm guessing this is the issue you have, and you're trying to get an alternative that doesn't block access to the table while you're adding a new column.
(In the future, it would help folks give you the best answers if you explain more about what you're trying to do.)
The answer to this question is yes, there is a solution: pt-online-schema-change is a free tool that accomplishes this.
You use it just like you would use ALTER TABLE, but you use it at the command-line instead of in an SQL query.
pt-online-schema-change --alter "ADD COLUMN c1 INT" D=sakila,t=actor
In this example, the database name is sakila and the table name is actor. The script does a lot of work behind the scenes:
Create a table like the original table, but empty of rows
ALTER TABLE to add the column or whatever other alteration you told it. You can do anything you would normally do with ALTER TABLE. In fact, it's doing ALTER TABLE for you, against the empty copy table.
Copy rows from the original table to the new table in the background.
Create triggers to capture any changes made to the original table while it's gradually copying the bulk of the data.
Swap the names of the new table (with the extra column) and the original table, once all data has been copied.
Drop the original table.
This has a few caveats, like the original table must have a primary key, and must not have existing triggers.
It tends to take longer than doing a traditional ALTER TABLE, but since it's not blocking access to the original table, it's still more convenient.
Does this help?
Is it possible to add new columns to an existing table without using the alter statement?
No.
Is it possible to add new columns to an existing table without using alter statement?
I don't think it's impossible.
However I'm not sure what you want to do.
lets say you have a table
select * from Store
and you want just export the data or perhaps you want to do something with that data like a selection. but you don't want to STORE the data in your Database
you can just fill a value and give it a name
select
'Test' as name,
*
from Store
this will populate your column with the value your entered.
data results
I have a database table that saved users profile information.
Sometimes when users register, they get duplicated with an extra column with same records, sometimes not.
So, I wonder if I put Unique on the column Email to make sure the user don't dup when register.
I think it should be something like this:
ALTER TABLE users ADD UNIQUE idx_row_unique(email);
But in case the Unique give error, how do I undo it?
Just scare that after I change it, I don't know how to undo it.
If there are duplicate emails, the alter table should fail. So you're safe with that!
I'd export the table structure and data first. That way if you need to put it back, you have the SQL right there.