altering a table column - sql-server-2005

create table Names (
FirstName varchar(40),
LastName varchar(40),
FullName AS FirstName+LastName
)
but now, in the full name i want that a space is inserted between first and last name, so i am altering the table but it gives syntax error. how to alter it.
alter table Names
alter column fullname as FirstName+' '+LastName

You can do this instead:
alter table Names drop column fullname
alter table Names add fullname as FirstName+' '+LastName

You can not ALTER computed columns. You can drop and recreate

Related

SQL DROP TABLE FUNCTION

Hello i've created a table if in a VM provided by my university and inserted the following:
create table first_table (record_id int primary key,
first_name varchar(20) not null,
last_name varchar(20) not null);
However, instead of the first_name and last_name I actually inserted my name and am looking to drop the table to recreate it.
" I typed in DROP TABLE [IF EXISTS] first_table "
and then it simply doesn't do anything. Any idea why.
In the Postgresql documentation, when you see something inside square brackets such as [IF EXISTS] it means that "IF EXISTS" is optional. You shouldn't type the square brackets if you put that in.
In this case, though, you know that the table exists so just leave the "IF EXISTS" part off:
drop table first_table;
Given that the problem is that you typed your first and last names instead of the desired column name (first_name and last_name) you can simply rename the columns using
alter table first_table
rename leo as first_name
alter table first_table
rename whatever_your_last_name_is as last_name
Try this:
DROP TABLE first_table;
In PSQL you should be able to do so using the command
DROP TABLE first_table;

Add a new column in table with a sequence - Oracle

I have a table that has 60 million rows of data. I would like to introduce a new column say "id" for the table that is an auto incremented sequence.
For example:
CREATE TABLE Persons (
LastName varchar(255),
FirstName varchar(255)
);
INSERT INTO Persons VALUES ('abc', 'def');
INSERT INTO Persons VALUES ('abcd', 'ghi');
CREATE SEQUENCE "PERSON_SEQUENCE" START WITH 1 INCREMENT BY 1;
ALTER TABLE PERSONS ADD (PERSONID NUMBER);
UPDATE persons SET personid = PERSON_SEQUENCE.NEXTVAL;
In the above sql statements, I am able to create a sequence then alter the table and update it.
Since the amount of data I need to update is large.. I would like to perform this with as much low cost as possible.
I am trying to do so something like this:
ALTER TABLE PERSONS ADD (PERSONID NUMBER DEFAULT(PERSON_SEQUENCE.NEXTVAL));
but the above does not work. Oracle throws me the below error:
Error starting at line :
1 in command - ALTER TABLE PERSONS ADD (PERSONID NUMBER
DEFAULT(PERSON_SEQUENCE.NEXTVAL)) Error report -
ORA-00984: column not allowed here
00984. 00000 - "column not allowed here"
*Cause:
*Action:
However this works:
ALTER TABLE PERSONS ADD (PERSONID NUMBER DEFAULT(0));
Could some one help me with how I can achieve to alter a table (create a new column) and populate the column with a seq id both in a single sql. Thank you!
For a table with 60 million rows, I would not do an add column + insert, but create the table new:
RENAME persons TO persons_old;
CREATE TABLE Persons (
personid number,
LastName varchar(255),
FirstName varchar(255)
);
INSERT INTO persons (personid, lastname, firstname)
SELECT person_sequence.nextval, lastname, firstname
FROM persons_old;
DROP TABLE persons_old;
If this is still taking too long, speak to your DBA about ALTER TABLE NOLOGGING and INSERT /*+ APPEND */ and PARALLEL DML.
EDIT: Ah, yes, for 60 million you could even increase the cache size of the sequence for the initial assignment:
ALTER SEQUENCE PERSON_SEQUENCE CACHE 1000;
This worked for me:
alter table PERSONS add (PERSON_ID number default PERSON_SEQ.nextval);

Have to create a Column with Unique constraint in the already made table

I have to add a column with Unique constraint in the already made table. The Column should have default value as combination of two other columns such as Posted_By and Posted_DateTime.
How to implement it through sql query ?
Easiest solution might be to create the column, populate it, and then alter it to be unique. Make sense?
Here is some untested code:
ALTER TABLE TABLENAME ADD COLUMNNAME VARCHAR(100) NULL;
UPDATE TABLENAME
SET COLUMNNAME = Posted_By + CAST(Posted_DateTime as Varchar)
ALTER TABLE TABLENAME ADD CONSTRAINT
CONSTRAINTNAME UNIQUE NONCLUSTERED ( COLUMNNAME )
Good luck.

how to remove default constraint from column in Db2

I had a table STUDENT_TB, which had column STUDENT_ID, NAME, AGE. I added a column with a following command :-
alter table STUDENT_TB add DOB TIMESTAMP NOT NULL DEFAULT CURRENT TIMESTAMP
as for the DOB column i didn't wanted it to be null. Now i need to remove that default constraint.
I tried searching but not got any success.
Regards.
I tried with this and it worked properly
alter table STUDENT_TB alter DOB drop DEFAULT
ALTER TABLE STUDENT_TB ALTER COLUMN DOB DROP NOT NULL

SQL Server: Extracting a Column Into a Table

I have a table with a column that I want to extract out and put into a separate table.
For example, lets say I have a table named Contacts. Contacts has a column named Name which stores a string. Now I want to pull out the names into another table named Name and link the Contact.Name column to the Id of the Name table.
I can only use SQL to do this. Any ideas on the best way to go about this?
Let me know if I can clarify anything, thanks!
[edit]
One problem is that different contacts can be tied to the same name. So when different contacts have the same name and it gets exported the Name table would only have one unique row for that name and all the contacts would point to that row. I guess this wouldn't make sense if I were actually working on a contact book, but I'm just using it to illustrate my problem.
CREATE TABLE Name (NameID int IDENTITY(1, 1), [Name] varchar(50))
INSERT INTO Name ([Name])
SELECT DISTINCT [Name]
FROM Contact
ALTER TABLE Contact
ADD COLUMN NameID int
UPDATE Contact
SET NameID = [Name].NameID
FROM Contact
INNER JOIN [Name]
ON Contact.[Name] = [Name].[Name]
ALTER TABLE Contact
DROP COLUMN [Name]
Then add foreign key constraint, etc.
Create the new table with a Foreign key that points back to the contact table. Then insert the names and contactids from the contact table into this new table. After that you can drop the "name" column from the contact table.
CREATE TABLE Name
(
ContactId int,
Name nvarchar(100)
);
INSERT Name(Name)
SELECT ContactId, Name From Contact;
ALTER TABLE Contact
DROP Column name;
EDIT: Since you have edited the question to mention that one name can be associated with multiple contacts, this changes things in the opposite way.
CREATE TABLE Name
(
NameId int IDENTITY,
Name nvarchar(100)
);
INSERT Name(Name)
SELECT DISTINCT Name From Contact;
ALTER TABLE Contact
ADD NameId int;
UPDATE c
SET c.NameId = n.NameId
FROM Contact c
JOIN Name n on n.Name = c.Name;
ALTER Table Contact
Drop Column Name;
NOTE: Make sure that you create the appropiate foreign key between the Contact and Name tables using the NameId on the Contact table and also create a UNIQUE constraint on the "name" column in the Name table.
insert into another_table( contact_id, name )
select id, name
from contacts;
insert into new_table (contact_id, name)
select min(id), name
from contacts
group by name;
This is one way of ensuring only one row per name - you can substitute other functions for min (like, for eg max).
I'm not too sure why you would want to do this, though. No matter what, you will end up with some contacts that don't have a name linked to them...
ALTER TABLE `Contacts` ADD `name_id` INT( 12 ) NOT NULL
ALTER TABLE `Name` ADD `Name` VARCHAR( 200 ) NOT NULL
INSERT INTO Name (id, name) SELECT id, Name FROM Contacts
ALTER TABLE `Contacts` DROP `Name`
The problem is the name_id field, which is filles with "0" and should be have the same value as the id in the Contacts-Table. Here you can use the LOOP or ITERATE statement (if you using MySQL).