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';
Related
I have the following column
`Equipamento_Solucao_id` VARCHAR(32) NOT NULL,
I would like it to be
`Equipamento_Solucao_id` VARCHAR(32) DEFAULT NULL,
How can I do this without changing my database model, that is, with a sql query?
You would use an alter table statement. A typical method would be:
alter table t alter column Equipamento_Solucao_id VARCHAR(32) DEFAULT NULL;
You could also look through the system tables on your database, find the not-null constraint, and then drop it specifically.
how do you modify a table to allow nulls using percona.
pt-online-schema-change --modfiy mycolumn default null d=database, t=table
I see the --alter but nothing to modify an existing column.
First of all, you wouldn't HAVE to use pt-online-schema-change to achieve this as you can do it in native SQL (though you may have a reason for asking how to do it with pt-online-schema-change)
For this table:
DROP DATABASE IF EXISTS test;
CREATE DATABASE test;
USE test;
CREATE TABLE t1 (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(40)
) ENGINE=InnoDB;
You can use this SQL:
ALTER TABLE `test`.`t1` CHANGE COLUMN name name VARCHAR(40) NULL;
Next, though, if you have a good reason to use pt-online-schema-change, for example if the table is very big, then this would be the syntax:
pt-online-schema-change h=127.0.0.1,P=3306,u=user,p=password,D=test,t=t1 --alter "CHANGE COLUMN name name VARCHAR(40) NULL" --execute
Here's the link to the tool's documentation https://www.percona.com/doc/percona-toolkit/LATEST/pt-online-schema-change.html
Hello stackoverflows friends.
My problem in sql is this sintaxis:
ALTER TABLE user MODIFY COLUMN (role INT(10)) DEFAULT 2 NOT NULL;
I want to modify a column in this case role but my default should be 2.
Thanks.
This syntax should work:
alter table user modify column role int default 2;
Here is a SQL Fiddle.
Try this - For SQL
ALTER TABLE USER ALTER COLUMN ROLE SET DEFAULT(2);
For MySql :
ALTER TABLE USER ALTER ROLE SET DEFAULT(2);
I am trying to make tables from a lua script (on a fiveM server) however I get the 1064 error when it tries to run the lines below. I don't know where the problem lies. I will post some of the lines as they are all similar. I am running the latest version of mysql database.
ALTER TABLE vrp_user_vehicles ADD IF NOT EXISTS veh_type varchar(255) NOT NULL DEFAULT 'default' ;
ALTER TABLE vrp_user_vehicles ADD IF NOT EXISTS vehicle_plate varchar(255) NOT NULL;
ALTER TABLE vrp_user_vehicles ADD IF NOT EXISTS vehicle_colorprimary varchar(255) DEFAULT NULL;
ALTER TABLE vrp_user_vehicles ADD IF NOT EXISTS vehicle_turbo varchar(255) NOT NULL DEFAULT 'off';
If you check out the mysql manual on alter table statement, you can see that it does not support IF NOT EXISTS clause. You need to remove them from your statements.
You must use the columns view in the information_schema if you want to check if a field exists.
select count(*) from information_schema.columns
where table_schema='your db name' and table_name='vrp_user_vehicles' and column_name='veh_type'
i'm sure there is such question at stackoverflow, but i just c't find it :(
I have 2 databases with same data ("developer" database and "production" database).
"production" database is "Live" database - sitve visitors see this data
"developer" database is database where i create new functions at my local server.
I have situation when i add to "developer" database some new tables and some new fields in old tables.
And now i have to copy this new created fields and tables to "production" database (but only structure, data should not be copied and no data at "production" database must be changed).
UPD: Maybe there is solution where i can make database structure dump from developing database and when i import it to production database, it automatically add all new fields from all tables
What functions should i use?
Thanks.
You want alter table: http://dev.mysql.com/doc/refman/5.1/en/alter-table.html
To alter an existing table:
alter table tablename add column newcolumn tinyint(1) default 1 AFTER othercolumn
To create a new table:
CREATE TABLE `newtablename` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`newcolumn` int(10) unsigned NOT NULL,
PRIMARY KEY (`id`),
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
to copy ONLY the structure you can use like:
jcho360> create table t3 like t2;
Query OK, 0 rows affected (0.05 sec).
If you want to copy the content after create the structure you can use :
INSERT INTO t3 SELECT * FROM t2;
if you want to copy with data you can use (without structure, I mean, PK, FK, etc)
mysql> create table t4 as select * from t1;
of course you are able to use backup to restore tables too
if you want to add new column you can use
alter table tablename add column newcolumn vartype;
if you want to use a table from another database you can use queries calling first the database and later the name, like
select * from database.tablename;
remember the user need permission to the other db too.