alter-table

Modify column Vs change column

眉间皱痕 提交于 2019-11-26 12:41:47
问题 I know, we can not rename a column using modify column syntax ,but can change column syntax . My question is: what is the main usage of modify syntax ? For example, alter table tablename change col1 col1 int(10) not null instead of alter table tablename modify col1 int(10) not null Edited Question replaced What is the main usage of modify syntax ? Above question was replaced by below Why we have to use change column instead of modify column? 回答1: CHANGE COLUMN If you have already created your

SQL Server add auto increment primary key to existing table

 ̄綄美尐妖づ 提交于 2019-11-26 12:39:43
As the title, I have an existing table which is already populated with 150000 records. I have added an Id column (which is currently null). I'm assuming I can run a query to fill this column with incremental numbers, and then set as primary key and turn on auto increment. Is this the correct way to proceed? And if so, how do I fill the initial numbers? No - you have to do it the other way around: add it right from the get go as INT IDENTITY - it will be filled with identity values when you do this: ALTER TABLE dbo.YourTable ADD ID INT IDENTITY and then you can make it the primary key: ALTER

Modify a Column's Type in sqlite3

僤鯓⒐⒋嵵緔 提交于 2019-11-26 12:11:18
问题 I\'m pretty new to SQLite 3 and just now I had to add a column to an existing table I had. I went about doing that by doing: ALTER TABLE thetable ADD COLUMN category; . Of course, I forgot to specify that column\'s type. The first thing I was thinking about doing was dropping that column and then re-adding it. However, it seems that SQLite does not have a simple way of doing this, and I would have had to backup the table and re-create it without the column. This seems messy, and I was

Check if a temporary table exists and delete if it exists before creating a temporary table

我们两清 提交于 2019-11-26 10:04:40
问题 I am using the following code to check if the temporary table exists and drop the table if it exists before creating again. It works fine as long as I don\'t change the columns. If I add a column later, it will give an error saying \"invalid column\". Please let me know what I am doing wrong. IF OBJECT_ID(\'tempdb..#Results\') IS NOT NULL DROP TABLE #Results CREATE TABLE #Results ( Company CHAR(3), StepId TINYINT, FieldId TINYINT, ) select company, stepid, fieldid from #Results --Works fine

ALTER TABLE ADD COLUMN IF NOT EXISTS in SQLite

时光毁灭记忆、已成空白 提交于 2019-11-26 08:19:45
问题 We\'ve recently had the need to add columns to a few of our existing SQLite database tables. This can be done with ALTER TABLE ADD COLUMN. Of course, if the table has already been altered, we want to leave it alone. Unfortunately, SQLite doesn\'t support an IF NOT EXISTS clause on ALTER TABLE . Our current workaround is to execute the ALTER TABLE statement and ignore any \"duplicate column name\" errors, just like this Python example (but in C++). However, our usual approach to setting up

MySQL: ALTER TABLE if column not exists

孤者浪人 提交于 2019-11-26 08:05:48
问题 I have this code: ALTER TABLE `settings` ADD COLUMN `multi_user` TINYINT(1) NOT NULL DEFAULT 1 And I want to alter this table only if this column doesn\'t exist. I\'m trying a lot of different ways, but nothing works: ALTER TABLE `settings` ADD COLUMN IF NOT EXISTS `multi_user` TINYINT(1) NOT NULL DEFAULT 1 With procedure: DELIMITER $$ CREATE PROCEDURE Alter_Table() BEGIN DECLARE _count INT; SET _count = ( SELECT COUNT(*) FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = \'settings\' AND

ALTER TABLE to add a composite primary key

断了今生、忘了曾经 提交于 2019-11-26 07:54:45
问题 I have a table called provider . I have three columns called person , place , thing . There can be duplicate persons, duplicate places, and duplicate things, but there can never be a dupicate person-place-thing combination. How would I ALTER TABLE to add a composite primary key for this table in MySQL with the these three columns? 回答1: ALTER TABLE provider ADD PRIMARY KEY(person,place,thing); If a primary key already exists then you want to do this ALTER TABLE provider DROP PRIMARY KEY, ADD

Rename column SQL Server 2008

半腔热情 提交于 2019-11-26 06:52:23
问题 I am using SQL Server 2008 and Navicat. I need to rename a column in a table using SQL. ALTER TABLE table_name RENAME COLUMN old_name to new_name; This statement doesn\'t work. 回答1: Use sp_rename EXEC sp_RENAME 'TableName.OldColumnName' , 'NewColumnName', 'COLUMN' See: SQL SERVER – How to Rename a Column Name or Table Name Documentation: sp_rename (Transact-SQL) For your case it would be: EXEC sp_RENAME 'table_name.old_name', 'new_name', 'COLUMN' Remember to use single quotes to enclose your

Error renaming a column in MySQL

时光怂恿深爱的人放手 提交于 2019-11-26 06:12:08
问题 How do I rename a column in table xyz ? The columns are: Manufacurerid, name, status, AI, PK, int I want to rename to manufacturerid I tried using PHPMyAdmin panel, but I get this error: MySQL said: Documentation #1025 - Error on rename of \'.\\shopping\\#sql-c98_26\' to \'.\\shopping\\tblmanufacturer\' (errno: 150) 回答1: Lone Ranger is very close... in fact, you also need to specify the datatype of the renamed column. For example: ALTER TABLE `xyz` CHANGE `manufacurerid` `manufacturerid` INT;

How can I modify the size of column in a mysql table?

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-26 05:16:31
问题 I have created a table and accidentally put varchar length as 300 instead of 65353 . How can I fix that? An example would be appreciated. 回答1: Have you tried this? ALTER TABLE <table_name> MODIFY <col_name> VARCHAR(65353); This will change the col_name 's type to VARCHAR(65353) 回答2: ALTER TABLE <tablename> CHANGE COLUMN <colname> <colname> VARCHAR(65536); You have to list the column name twice, even if you aren't changing its name. Note that after you make this change, the data type of the