DELETE complete column from table - sql

[Name] [major_version] [minor_version] [revision] [install_failures]
s 23 1 NULL 0
This is my table. How can I delete the revision column (not the value, the complete revision column) from table?

ALTER TABLE `table_name` DROP COLUMN `revision`;

You can do this with the following sql
ALTER TABLE table_name
DROP COLUMN revision
Here can you read simple documentation on that.

Use ALTER statement
ALTER TABLE table_name DROP COLUMN revision;
The ALTER TABLE statement is used to
add, delete, or modify columns in an
existing table.
Syntax :
ALTER TABLE table_name
DROP COLUMN column_name

You have to use the alter table statement to delete the revision column from your table.
The ALTER TABLE statement is used to
add, delete, or modify columns in an
existing table.
your delete column syntax will look like this
ALTER TABLE table_name
DROP COLUMN revision

This is very simple...use this
ALTER TABLE table_name DROP COLUMN column_name
Here are some links that will help you.
techonthenet, w3schools, php.about, sqlzoo.

Related

Is it possible to insert column in the first postion with sas/sql

When I add a column to my my table with alter table, it is inserted as the last record. Is there a way to insert the column into the position I choose?
Thank you.
Some dbms let you control the column position with an alter table statement, but most do not. MySQL is one that does. Assume you start with
create table table_name (b char(1), d char(1));
You can alter that table to put column a first, and alter it again to put c after b.
alter table table_name add column a char(1) first;
alter table table_name add column c char(1) after b;
All dbms support "reordering" columns using create view... and select... though.
In the relational model of data, the order of columns is irrelevant. But I know what it's like to work in companies where that irrelevance is irrelevant.

How can I DROP COLUMN in Microsoft SQL Server with a value?

Code:
ALTER TABLE tblUser
DROP COLUMN Mobile
Error:
ALTER TABLE DROP COLUMN Mobile failed because one or more objects access this column.
This column had values in Table. How can I delete all objects that access this column?
How can I DROP COLUMN with values?
how can do it with code? How can I delete all constraints in column automatically?
ALTER TABLE DROP COLUMN Mobile failed because one or more objects access this column.
Your column won't be deleted. Because one column or multiple columns are getting reference from this column that you want to delete.
So first, you will have to find in which table your column is being referenced by below query.
SELECT * FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS WHERE TABLE_NAME = 'TABLENAME'
It will show you all constraints of all tables of your current database. You need to find it and remove the constraint. After that your column will be deleted successfully because there is no reference of your column in any table.
To remove constraint from column - use below query
alter table tablename
drop constraint constraintid
SQL Search is a great tool. I will search for your all the objects which are using the targeted object.
You can easily find where your column is being used, then simply you can modify or drop that objects too.
Use below query to find the constraints name for particular tablename
SELECT * FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS WHERE TABLE_NAME = 'TABLENAME'
Noe you can see the constraints name under constraint_name column, drop all constraint using below syntax
ALTER TABLE TABLENAME DROP CONSTRAINT CONSTRATINTSNAME
After that you can use below statement to drop the column
ALTER TABLE TABLENAME DROP COLUMN COLUMNNAME
You need to know what those constraints are and what their names are in order to drop them; there's nothing in SQL Server to say DROP ALL CONSTRAINTS and just do it. – marc_s yesterday

PostgreSQL query to rename and change column type with single query

In PostgreSQL if I need to rename and change a column data type, I run two separate queries to do so.
To rename:
ALTER TABLE tblName RENAME <oldColumn> TO <newColumn>
and to change column type:
ALTER TABLE tblName ALTER COLUMN <newColumn> <columnType>.
But is there any way to do both of these works with a single query like the following MySQL query:
ALTER TABLE tblName CHANGE COLUMN <oldColumn> <newColumn> <columnType>
In PostgreSQL, ALTER TABLE can take a series of operations. So:
ALTER TABLE <tablename> RENAME <oldcolumn> TO <newcolumn>;
ALTER TABLE <tablename> ALTER COLUMN <columnname> TYPE <newtype>;
is the same as
ALTER TABLE <tablename>
ALTER COLUMN <columnname> TYPE <newtype>
RENAME <oldcolumn> TO <newcolumn>;
However... why? IIRC the rename won't cause a full-table scan, so there's no benefit over just doing the two statements separately, within one transaction. What problem are you actually trying to solve with this?
PostgreSQL: Alter table column name and data-type:
ALTER TABLE <TableName>
ALTER [ COLUMN ] column [ SET DATA ] TYPE data_type [ COLLATE collation ] [ USING expression ]
RENAME [ COLUMN ] column TO new_column;
See ALTER TABLE.

Update foreign key column from not null to null

How I can update a foreign key column in a table from not null to null without having to recreate a table?
I tried to use this:
update tblProduct
set ConsumerID not null
Please advise, thank you
did you try something like this?
ALTER TABLE [Table] ALTER COLUMN [Column] INTEGER NULL
ALTER TABLE tblProduct MODIFY ConsumerID BIGINT NOT NULL;
For more details about altering a MySQL table please visit ALTER TABLE SYNTAX
This is altering data about the table and not data in the table, thus you need to alter the data schema.
So look at create and alter table statements
e.g. for Sybase (and MS Sql server)
alter table tblProduct modify ConsumerID integer NULL

How to use ALTER TABLE to add a new column and make it unique?

How do I use ALTER TABLE to add a new column and make it unique?
Depends on the DBMS, but I think the following is quite portable:
ALTER TABLE table_name ADD column_name datatype
ALTER TABLE table_name ADD UNIQUE (column_name)
If you want to give a name to the UNIQUE constraint, you could replace the last command with this:
ALTER TABLE table_name ADD CONSTRAINT constraint_name UNIQUE (column_name)
if table is empty
ALTER TABLE ADD (FieldName Type)
ALTER TABLE ADD CONSTRAINT UNIQUE(FieldName)
If you have data in table you need to this in three steps:
Add column
Fill values
Add unique constraint
You can do it with a single SQL statement (at least with MySQL):
ALTER TABLE `people` ADD COLUMN `personal_code` VARCHAR(50) UNIQUE AFTER `surname`;
It is a two step process: add the new coloumn, then add the constraint. Because UNIQUE constraints permit nulls it doesn't matter whether the table is populated:
SQL> select count(*) from t23
2 /
COUNT(*)
----------
2
SQL> alter table t23
2 add new_col number
3 /
Table altered.
SQL> alter table t23
2 add constraint t23_uk unique (new_col)
3 /
Table altered.
SQL>