I am a beginner in SQL, I created a table called Product and in one of the columns I placed SURNAME instead of DESCRIPTION, I am trying to follow the code below but this syntax error is appearing:
alter table palmsdatabase.product
CHANGE COLUMN SURNAME DESCRIPTION NVARCHAR(500) NULL
Just try this:
ALTER TABLE Product CHANGE `DESCRIPTION` `SURNAME` NVARCHAR(500) NULL;
and check for more information: Alter Table
Related
I want to create a column with a space in its name. In MySQL I just created this using back ticks, but in SQL Server that causes an error. Are there any possibilities to create a column name with a space?
I've tried the following but it doesn't work.
create table space_check
(
`roll num` int,
name varchar(50)
)
Can anybody tell me how to create this?
Use brackets in SQL-Server
create table space_check
(
[roll num] int,
name varchar(50)
)
Give the column name within Square brackets.
create table space_check
(
[roll num] int,
name varchar(50)
)
It was not a good practice. Try to create using underscore(roll_num) or caps(rollNum).
use bracket [ to create columns with spaces.
create table space_check
(
[roll num] int,
name varchar(50)
)
However it is not a good practice. Try using RollNum or rollNum or roll_num.
I am trying to add multiple columns to an existing table in phpMyAdmin, but I keep getting the same error:
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax ...
I am writing:
ALTER TABLE `WeatherCenter`
ADD COLUMN
BarometricPressure SMALLINT NOT NULL,
CloudType VARCHAR(70) NOT NULL,
WhenLikelyToRain VARCHAR(30) NOT NULL;
I have referred to past posts on StackOverflow, and I am following the experts' recommendation, so why am I getting an error?
ALTER TABLE table_name
ADD COLUMN column_name datatype
correct syntax
ALTER TABLE `WeatherCenter`
ADD COLUMN BarometricPressure SMALLINT NOT NULL,
ADD COLUMN CloudType VARCHAR(70) NOT NULL,
ADD COLUMN WhenLikelyToRain VARCHAR(30) NOT NULL;
check syntax
You need to specify multiple ADD COLUMN
ALTER TABLE `WeatherCenter`
ADD COLUMN BarometricPressure SMALLINT NOT NULL,
ADD COLUMN CloudType VARCHAR(70) NOT NULL,
ADD COLUMN WhenLikelyToRain VARCHAR(30) NOT NULL;
You can alter a table and add multiple columns in one statement by doing it like this.
alter table WeatherCenter add column (BarometricPressure SMALLINT NOT NULL, CloudType VARCHAR(70) NOT NULL, WhenLikelyToRain VARCHAR(30) NOT NULL);
This will help you:
alter table A add first_name varchar(10),last_name varchar(10);
As you're adding columns to an existing table I don't think you're meant to declare NOT NULL in the statement. Also, you don't need to use ADD COLUMN, you can just use ADD.
ALTER TABLE WeatherCentre
ADD BarometricPressure SMALLINT,
ADD CloudType VARCHAR(70),
ADD WhenLikelyToRain VARCHAR(30);
This is from Official MySQL Documentation
ALTER TABLE tbl_name
[alter_specification [, alter_specification] ...]
[partition_options]
alter_specification:
table_options
| ADD [COLUMN] col_name column_definition
[FIRST | AFTER col_name]
| ADD [COLUMN] (col_name column_definition,...)
Possible duplicate of alter table add MULTIPLE columns AFTER column1
alter table table_name add (product varchar(20) not null, price int(10))
this is also working fine
I have created a table users
create table users (
id int,
name varchar(40)
);
Now i want a default value for name
This query works in MYSQL but not in Oracle database 11g XE
alter table users alter name set default 'user';
Can anyone explain why ?
The syntax for adding a default to an existing column is different in Oracle, viz:
alter table users
modify (name default 'user');
SqlFiddle here
I think the correct query will be like this:
ALTER TABLE users MODIFY name VARCHAR(40) NOT NULL DEFAULT 'user';
I am very new to SQL and having problems with creating a table structure.
I want to create a table with four columns - id,text,date and replace. Problem is this is giving me an error in MySQL, I think because the words TEXT and DATE are also names for data types and REPLACE is another term used in SQL so MySQL is getting confused about what my column names should be or doesn't realise the names I've given are actual names. Is there any way I can get around this to get the column names I want, without having to call the columns something else and then change them back manually once created?
Here's my SQL:
CREATE TABLE message (
id INT UNIQUE AUTO_INCREMENT NOT NULL,
text TEXT,
date INT,
replace INT DEFAULT 0
);
And the error I'm getting:
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'replace INT DEFAULT 0 )' at line 5
CREATE TABLE message(
id INT UNIQUE AUTO_INCREMENT NOT NULL ,
TEXT TEXT,
DATE INT,
REPLACE INT DEFAULT 0
);
CREATE TABLE message (
`id` INT UNIQUE AUTO_INCREMENT NOT NULL,
`text` TEXT,
`date` INT,
`replace` INT DEFAULT 0
);
General rule is don't use these keywords, come up with something different than 'text' for your field name. If you must, use ` (back-tick) around the column name to tell SQL it's a column name and not what the keyword means. I recommend against calling your columns 'from' 'select' and 'where' as well ;)
I know, we cannot rename a column using MODIFY COLUMN syntax, but we can using CHANGE COLUMN syntax.
My question is: what is the main usage of modify syntax?
For example:
ALATER TABLE tablename CHANGE col1 col1 INT(10) NOT NULL;
instead of
ALATER TABLE tablename MODIFY col1 INT(10) NOT NULL;
Edited (question replaced)
What is the main usage of MODIFY syntax?
Why we have to use CHANGE COLUMN instead of MODIFYCOLUMN?
CHANGE COLUMN
If you have already created your MySQL database, and decide after the fact that one of your columns is named incorrectly, you don't need to remove it and make a replacement, you can simply rename it using change column.
ALTER TABLE MyTable CHANGE COLUMN foo bar VARCHAR(32) NOT NULL FIRST;
MODIFY COLUMN
This command does everything CHANGE COLUMN can, but without renaming the column. You can use the MODIFY SQL command if you need to resize a column in MySQL. By doing this you can allow more or less characters than before. You can't rename a column using MODIFY and other.
ALTER TABLE MyTable MODIFY COLUMN foo VARCHAR(32) NOT NULL AFTER baz;
Note
ALTER TABLE is used for altering a table in order to change column name, size, drop column etc. CHANGE COLUMN and MODIFY COLUMN commands cannot be used without help of ALTER TABLE command.
The difference is whether you want to change the column name, column definition or both.
CHANGE
Can change a column name or definition, or both
ALTER TABLE t1 CHANGE a b BIGINT NOT NULL
MODIFY
Can change a column definition but not its name
ALTER TABLE t1 MODIFY b INT NOT NULL
RENAME COLUMN (from MySQL 8.0)
Can change a column name but not its definition
ALTER TABLE t1 RENAME COLUMN b TO a
Also, CHANGE and MODIFY can be followed by an optional COLUMN keyword.
For complete explanation:
MySQL 5.7 Docs- Renaming, Redefining, and Reordering Columns
MySQL 8.0 Docs- Renaming, Redefining, and Reordering Columns
I found one difference after more than an hour of effort in trying to make a non auto_increment column into auto_increment
statement:
alter table `doctor_experience` modify column `id` int(11) unsigned auto_increment
works, but statment:
alter table `doctor_experience` change column `id` `id` int(11) unsigned auto_increment
will report an error.
That is the same. It was done to support another syntax (Oracle ALTER TABLE as I know). You can use both of them.
Note: ALTER TABLE CHANGE old_col_name new_col_name syntax allows renaming column using one command.
Change Column : Used when we want to change the column name with its definition.
eg - alter table student CHANGE name full_name VARCHAR(32) NOT NULL;
Modify column : Used when column name is to be same but change in its definition.
eg - alter table student MODIFY full_name VARCHAR(64) NOT NULL;
Rename column : Used when we only need to change the column name (its definition will be same)
alter table student RENAME COLUMN full_name TO name;