SQL Basics Part3

hana sql neww

HANA SQL DDL.

 

 

DDL statements are used to create, change, rename and delete tables.  

 Various DDL Commands are: 

CREATE COLUMN TABLE  A new (empty) table is created. 
ALTER TABLE The definition of an existing table is changed.
RENAME TABLE An existing table is renamed. 
 DROP TABLE An existing table is deleted.

CREATE COLUMN TABLE – Used to create a new column table.

 Syntax:    

 CREATE COLUMN TABLE Table
                                    (Column Data_Type,
                                     Column Data_Type,
                                     Column Data_Type NOT NULL,
                                     Column Data_Type NOT NULL,
                                     Column Data_Type DEFAULT Default_Value,
                                     Column Data_Type NOT NULL DEFAULT Default_Value,
                                     PRIMARY KEY(Column, Column));

Example1: It is not mandatory to define a primary key.if so then the table can contain duplicate records.

CREATE COLUMN TABLE Official
                                    (PNr VARCHAR(3),
                                     Name VARCHAR(20),
                                     Overtime INTEGER,
                                     Salary VARCHAR(3),
                                     Manager VARCHAR(3));

Example2: primary key can be defined and you can mention primary key as a part of column definition.

CREATE COLUMN TABLE Official
                                     (PNr VARCHAR(3) PRIMARY KEY,
                                       Name VARCHAR(20),
                                       Overtime INTEGER,
                                       Salary VARCHAR(3),
                                       Manager VARCHAR(3));

Example3: primary key can be defined and you can mention primary key as a part of PRIMARY KEY clause.

CREATE COLUMN TABLE Official
                                   (PNr VARCHAR(3),
                                     Name VARCHAR(20),
                                     Overtime INTEGER,
                                     Salary VARCHAR(3),
                                     Manager VARCHAR(3),
                                     PRIMARY KEY(PNr));

Example4. To define multiple column primary key use PRIMARY KEY clause

CREATE COLUMN TABLE Owner
                                    (Country VARCHAR(3),
                                     OwnerID VARCHAR(3),
                                     Name VARCHAR(20),
                                     Birthday DATE,
                                     City VARCHAR(20),
                                     PRIMARY KEY(Country, OwnerID));

Example5. For primary key columns, NULL values are implicitly prohibited.For other columns you can specify that no NULL values are allowed by adding the
words NOT NULL.

CREATE COLUMN TABLE Official
                                    (PNr VARCHAR(3),
                                      Name VARCHAR(20) NOT NULL,
                                     Overtime INTEGER,
                                     Salary VARCHAR(3) NOT NULL,
                                     Manager VARCHAR(3),
                                     PRIMARY KEY(PNr));

Example6. A default value can be assigned to a  column by adding DEFAULT <value>.The default value is used during INSERT, if no value is specified for the corresponding column.The default value will not be used during INSERT, if a NULL value is explicitly specified.

CREATE COLUMN TABLE Official
                                    (PNr VARCHAR(3),
                                      Name VARCHAR(20),
                                      Overtime INTEGER DEFAULT 0,
                                      Salary VARCHAR(3) DEFAULT ‘A06’,
                                      Manager VARCHAR(3),
                                      PRIMARY KEY(PNr));

Example7. For a column, you can specify that this column must not contain double values by adding UNIQUE .In contrast to the PRIMARY KEY, UNIQUE allows NULL values.

CREATE COLUMN TABLE Car
                                   (CarID VARCHAR(3) PRIMARY KEY,
                                     PlateNumber VARCHAR(10) UNIQUE,
                                     Brand VARCHAR(20),
                                     Color VARCHAR(10),
                                     HP INTEGER,
                                     Owner VARCHAR(3));

Example8. For a column, NULL values are not excluded when setting as UNIQUE. To avoid allow null values, NOT NULL can also be useful for columns defined as UNIQUE.

CREATE COLUMN TABLE Car
                                    (CarID VARCHAR(3) PRIMARY KEY,
                                      PlateNumber VARCHAR(10) UNIQUE NOT NULL,
                                      Brand VARCHAR(20),
                                      Color VARCHAR(10),
                                     HP INTEGER,
                                     Owner VARCHAR(3));


ALTER TABLE- This command is used to add columns to the existing table, deleting column(s) from the table or changing the data type of existing field(s).    

 Syntax:

 Adding new column to the existing table.

 ALTER TABLE Table                                                                
                              ADD (Column Data_Type,
                                          Column Data_Type NOT NULL DEFAULT Default_Value);

Deleting columns from existing table.      

ALTER TABLE Table
                              DROP (Column, Column, Column);

Columns that are part of the primary key cannot dropped.Tables without a primary key at least one column must be left.

You can delete the definition of the primary key. It deletes  the primary key property.

ALTER TABLE Table
                             DROP PRIMARY KEY;

A new primary key can be  defined for a table without one. The corresponding columns must not contain NULL values and the key property must not be violated by the existing data.

ALTER TABLE Table
                             ADD PRIMARY KEY(column);

Changing existing field data types                                                                                           

ALTER TABLE Table
                            ALTER (Column Data_Type DEFAULT Default_Value,
                                           Column Data_Type NULL);

You can change the data type of existing columns. Type compatibility must be ensured.For string-based data types the new data type must have at least the same maximum length (in comparison to the old data type).


RENAME TABLE-> An existing table can be renamed with or without data.      

 Syntax:  RENAME TABLE Table1  TO Table2;


DROP TABLE-> It deletes the table content and the table itself.   

Syntax:  DROP TABLE Table;


RENAME COLUMN-> An existing table column is renamed. The table can be empty or can contain data.      

Syntax:  RENAME COLUMN Table.Column TO new_Column_Name;


 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s