I was quite confused on why in the preperation for the microsoft 70-461 exam it says this:
Create tables without using the built in tools; ALTER; DROP; ALTER COLUMN; CREATE
What exactly does ALTER COLUMN mean? I do not recall that being a statement or command could anybody please explain this for me. Thanks :)
You use it as part of an alter table statement to change a column definition:
alter table t1
alter column c1 varchar(50) not null
It will give an error without the alter table piece.
Related
I don't understand why the table is dropped before creating it in the is this query I am reviewing. Can anyone help to explain this better? Thank you!
DROP TABLE #Table1
CREATE TABLE #Table1(
ID INT NOT NULL,
PRIMARY KEY (ID)
)
If table exists already you will get an error. That is why you clean env first:
DROP TABLE #Table1;
CREATE TABLE #Table1( ID INT NOT NULL, PRIMARY KEY (ID) );
I would use DROP TABLE IF EXISTS to be safe.
db<>fiddle demo
DROP before CREATE is common in SQL script. It is for making sure to delete the old table before making a new one.
This is because SQL script will not halt if the table exists. Not just on tables but it is common on creating the database.
For example, if we running a restoring backup script. In the create table process if the table already exists it will raise an error but the script will not stop. It will be a problem if the data is not restored properly.
I have a previously created table and I am trying to make the Primary Key, which is BookingID, auto increment. I have 4 records in the database currently. The SQL function keeps telling me there is a syntax error near ' ALTER' . I am using SQLite if that matters.
ALTER TABLE Booking AUTO_INCREMENT=5
Because "alter" in SQLite only lets you rename tables or add columns; it doesn't support what you're trying to do.
For more information, check out the documentation of ALTER TABLE in SQLite:
SQLite supports a limited subset of ALTER TABLE. The ALTER TABLE command in SQLite allows the user to rename a table or to add a new column to an existing table.
I am trying to rename a column name in w3schools website
ALTER TABLE customers
RENAME COLUMN contactname to new_name;
However, the above code throws syntax error. What am I doing wrong?
You can try this to rename the column in SQL Server:-
sp_RENAME 'TableName.[OldColumnName]' , '[NewColumnName]', 'COLUMN'
sp_rename automatically renames the associated index whenever a
PRIMARY KEY or UNIQUE constraint is renamed. If a renamed index is
tied to a PRIMARY KEY constraint, the PRIMARY KEY constraint is also
automatically renamed by sp_rename. sp_rename can be used to rename
primary and secondary XML indexes.
For MYSQL try this:-
ALTER TABLE table_name CHANGE [COLUMN] old_col_name new_col_name
From "Learning PHP, MySQL & JavaScript" by Robin Nixon pg 185. I tried it and it worked.
ALTER TABLE tableName CHANGE oldColumnName newColumnName TYPE(#);
note that TYPE(#) is, for example, VARCHAR(20) or some other data type and must be included even if the data type is not being changed.
#Hannah's answer definitely helped me there but, as it took me some time to translate the syntax into real-life, I thought it might help someone to get an actual real-life example on how to use this syntax on MySQL.
1.So here again is the syntax:
ALTER TABLE tableName CHANGE oldColumnName newColumnName TYPE(#);
NB: TYPE(#) is, for example, VARCHAR(255) or some other data type and must be included even if the data type is not being changed.
2.Here is my real-life example, where "dashboard_dummy" is my table name, "product_code" my old column name and "order_id" my new column name:
ALTER TABLE dashboard_dummy CHANGE COLUMN product_code order_id int;
Hope it helps!
I created a table called test with column called code:
create table test(
code char(3) not null);
I then populated the table with the following data:
insert into test values ('A12');
insert into test values ('B23');
insert into test values ('C45');
I then altered the column to make it char(4):
alter table test
alter column code char(4) not null;
I then added a 'X' to all existing data so that it becomes 4 characters long:
update test
set code='X'+code
where LEN(code)=3;
So far so good but then when I tried to add a check constraint:
alter table test
add constraint codeCheck check (code like 'A-Z''A-Z''0-9''0-9');
I got this error:
The ALTER TABLE statement conflicted with the CHECK constraint "codeCheck".
I understand that the error implies that the existing data violates the check constraint that I am trying to add into the table, but why?
and how do I do it such that the existing data and check constraint do not violate each other?
Your pattern syntax is wrong. It should be
alter table test
add constraint codeCheck check (code like '[A-Z][A-Z][0-9][0-9]');
Because your data doesn't match the like constraint.
Try
alter table test
add constraint codeCheck check (code like '[A-Z][A-Z][0-9][0-9]' );
I donĀ“t know how it works with SQL Server but your like clause looks odd.
Try using
'[A-Z]{2}\d{2}'
I have a table and I want to drop or set the default value of one of columns. I use below scripts :
ALTER TABLE AccAccountGroup ALTER COLUMN Name DROP DEFAULT
ALTER TABLE AccAccountGroup ALTER COLUMN Name SET DEFAULT 'default value'
when I run the scripts, below errors appears :
Incorrect syntax near the keyword 'DEFAULT'. => for drop script
Incorrect syntax near the keyword 'SET'. => for add script
these scripts are from msdn.
What is the problem? I use SQL Server 2008 R2.
I believe that your reference is for SQL Server Compact Edition. Use this one instead.
You need to use the CONSTRAINT syntax and you need to use the default's name. Even though you didn't assign a name (and I suggest that you do in the future as it's a good practice), SQL Server will assign one, which you can find using EXEC sp_help AccAccountGroup.
Try these syntaxes:
ALTER TABLE AccAccountGroup
DROP CONSTRAINT <default name>
ALTER TABLE AccAccountGroup
ADD CONSTRAINT DF_AccAccountGroup_name DEFAULT 'default value' FOR name
The link you provided refers only to SQL Server Compact Edition, not the 'full' SQL Server. The above statements are only valid in SQL Server Compact Edition.
In the 'full' SQL Server, default values for columns are implemented using constraints. You can add a default constraint to a table using something like
ALTER TABLE AccAccountGroup ADD CONSTRAINT AccAcctGrp_Name_Def DEFAULT 'default value' FOR name;
and drop it using
ALTER TABLE AccAccountGroup DROP CONSTRAINT AccAcctGrp_Name_Def;
Note that you need to provide the name of the constraint when you drop it. If you don't know the name of the constraint, the following query will look it up. Change the schema name dbo, and also the table and column names, if/as necessary:
SELECT object_name(default_object_id)
FROM sys.columns
WHERE object_id = object_id('dbo.AccAccountGroup')
AND name = 'Name';
Once you've got the name of the constraint, you can drop it. (Acknowledgement: this query was taken from a comment posted here.)