Generating SQL server script can get Create and alter statements for the same table - sql

HI I am generating schema script for a database, but when i finish creating it and and look at the script it gives create table statement for a table but not including all column in it also it generates alter table add column statement for the same tables but for missing columns which are left in create table statement.
see the attached screenshot.

Assuming the question is "why does it not create all of the columns in the CREATE TABLE statement" ...
You'll notice that between the CREATE TABLE and the ALTER statements, the value for SET ANSI_PADDING is altered. As the documentation notes, the setting's value is taken into account at the point in time when a column is created.
There's no way to override this setting in-line with the declaration of a column.
Since your table apparently contains a mixture of columns, some of which are defined with the setting ON and others with it defined OFF, there's no way to write a single CREATE TABLE statement that creates all of the columns in one go.

Related

Big Query Web UI Alter datatype of Table or empty table

I created a empty table , I m unable to alter the Datatype of the field from web ui, is there any command line or Alter table command for Altering the datatype from INTEGER -> STRING
You need to recreate the schema, you cannot alter types.
If you want to copy the old data. Please make sure you create a temporary table, then once the schema is ready, you can query the old table and write to the new table making the transformation.

DB2: How to add new column between existing columns?

I have an existing DB2 database and a table named
employee with columns
id,e_name,e_mobile_no,e_dob,e_address.
How can I add a new column e_father_name before e_mobile_no?
You should try using the ADMIN_MOVE_TABLE procedure which allows to change the table structure.
The ALTER TABLE only allows adding columns to the end of the table. The reason is that it would change the physical structure of the table, i.e., each row would need to be adapted to the new format. This would be quite expensive.
Using the mentioned procedure ADMIN_MOVE_TABLE you would copy the entire table and during that process change the table structure. It requires a significant amount of space and time.
In DB2 IBM i v7r1 you can do it, try on your DB2 version
alter table yourtable
add column e_father_name varchar(10) before e_mobile_no
I always do the following --
Take a backup/dump of table data and db2look
(If you dump to a CSV file as I do I suggest dumping in the new format so for example put null for the new column in the right place.
Drop table and indexes
Create table with the new colunn
Load data with old values
Recreate all indexes and runstats.
Once you have done it a few times it becomes old hat.

Is it possible to rename a table in Firebird?

Is it possible to rename a table in Firebird or I should create a new table and then move the data using insert?
Apparently not.
You must either create a new table, copying over old values or create a view with the intended name which is identical to the original table.
See http://www.firebirdfaq.org/faq363/ for further details.
It is possible to change the column name by:
ALTER TABLE "tableName" ALTER "columnName" TO "NewColumnName";

Difference between Alter and Update SQL

I am busy studying MySQL and I understand that update is used to update a record or row in a table. So what does alter do that is so different? Seems like they are the same.
Thanks, any help will be appreciated.
ALTER is a DDL (Data Definition Language) statement. Whereas UPDATE is a DML (Data Manipulation Language) statement. ALTER is used to update the structure of the table (add/remove field/index etc). Whereas UPDATE is used to update data.
The ALTER changes the table in the database, you can add or remove columns, etc. But it does not change data (except in the dropped or added columns of course).
While the UPDATE changes the rows in the table, and leaves the table unchanged.
ALTER is used to change things like table structures or stored procs, otherwise known as DDL statements.
ALTER table MyTable
ADD MyNewColumn VARCHAR(100)
OR
ALTER PROC dbo.MyStoredProc
Alter command is a data definition language
Update command is a data manipulation language
Alter example- table structure, table name, sp, functions
Update example-change database in a row or column etc
Alter command make changes in table structure.
Update command make changes with inside the table
Alter command is used to add, delete modify the attributes of the table in the database
Update command is used to update existing record in a database
Let's see in simple words...
Alter command we use for the modify the structure of the database, table(add, drop, modify)and it falls under DDL.
Update command we use for the modify the rows(records) of the table using where condition and its fall under DML.

a special case when modifing the database

sometimes i face the following case in my database design,, i wanna to know what is the best practice to handle this case:::
for example i have a specific table and after a while ,, when the database in operation and some real data are already entered.. i need to add some required fields (that supposed not to accept null)..
what is the best practice in this situation..
make the field accept null as (some data already entered in the table ,, and scarify the important constraint )and try to force the user to enter this field through some validation in the code..
truncate all the entered data and reentered them again (tedious work)..
any other suggestions about this issue...
It depends on requirements. If the data to populate existing rows for the new column isn't available immediately then I would generally prefer to create a new table and just populate new rows when the data exists. If and when you have all the data for every row then put the new column into the original table.
If possible i would set a default value for the new column.
e.g. For Varchar
alter table table_name
add column_name varchar(10) not null
constraint column_name_default default ('Test')
After you have updated you could then drop the default
alter table table_name
drop constraint column_name_default
A lot will come down to your requirements.
It depends on your application, your database scheme, your entities.
The best way to go about it is to truncate the data and re - enter it again, but it need not be too tedious an item. Temporary tables and table variables could assist a great deal with this issue. A simple procedure comes to mind to go about it:
In SQL Server Management Studio, Right - click on the table you wish to modify and select Script Table As > CREATE To > New Query Editor Window.
Add a # in front of the table name in the CREATE statement.
Move all records into the temporary table, using something to the effect of:
INSERT INTO #temp SELECT * FROM original
Then run the script to keep all your records into the temporary table.
Truncate your original table, and make any changes necessary.
Right - click on the table and select Script Table As > INSERT To > Clipboard, paste it into your query editor window and modify it to read records from the temporary table, using INSERT .. SELECT.
That's it. Admittedly not quite straightforward, but a well - kept database is almost always worth a slight hassle.