18, నవంబర్ 2013, సోమవారం

Primary key creation alteration deletion, on single column and multi column



Constraints are the rules enforced on data columns on table. These are used to limit the type of data that can go into a table. This ensures the accuracy and reliability of the data in the database.

Constraints could be column level or table level. Column level constraints are applied only to one column, whereas table level constraints are applied to the whole table.
Following are commonly used constraints available in SQL. These constraints have already been discussed in SQL - RDBMS Concepts chapter but its worth to revise them at this point.
  • NOT NULL Constraint: Ensures that a column cannot have NULL value.
  • DEFAULT Constraint: Provides a default value for a column when none is specified.
  • UNIQUE Constraint: Ensures that all values in a column are different.
  • PRIMARY Key: Uniquely identified each rows/records in a database table.
  • FOREIGN Key: Uniquely identified a rows/records in any another database table.
  • CHECK Constraint: The CHECK constraint ensures that all values in a column satisfy certain conditions.
  • INDEX: Use to create and retrieve data from the database very quickly.
Constraints can be specified when a table is created with the CREATE TABLE statement or you can use ALTER TABLE statement to create constraints even after the table is created.

Dropping Constraints:

Any constraint that you have defined can be dropped using the ALTER TABLE command with the DROP CONSTRAINT option.
For example, to drop the primary key constraint in the EMPLOYEES table, you can use the following command:
ALTER TABLE EMPLOYEES DROP CONSTRAINT EMPLOYEES_PK;
Some implementations may provide shortcuts for dropping certain constraints. For example, to drop the primary key constraint for a table in Oracle, you can use the following command:
ALTER TABLE EMPLOYEES DROP PRIMARY KEY;
Some implementations allow you to disable constraints. Instead of permanently dropping a constraint from the database, you may want to temporarily disable the constraint and then enable it later.

Integrity Constraints:

Integrity constraints are used to ensure accuracy and consistency of data in a relational database. Data integrity is handled in a relational database through the concept of referential integrity.
There are many types of integrity constraints that play a role in referential integrity (RI). These constraints include Primary Key, Foreign Key, Unique Constraints and other constraints mentioned above.
A primary key is a field in a table which uniquely identifies each row/record in a database table. Primary keys must contain unique values. A primary key column cannot have NULL values.
A table can have only one primary key, which may consist of single or multiple fields. When multiple fields are used as a primary key, they are called a composite key.
If a table has a primary key defined on any field(s), then you cannot have two records having the same value of that field(s).
Note: You would use these concepts while creating database tables.
Create Primary Key:
Here is the syntax to define ID attribute as a primary key in a CUSTOMERS table.
CREATE TABLE CUSTOMERS(
       ID   INT              NOT NULL,
       NAME VARCHAR (20)     NOT NULL,
       AGE  INT              NOT NULL,
       ADDRESS  CHAR (25) ,
       SALARY   DECIMAL (18, 2),      
       PRIMARY KEY (ID)
);
To create a PRIMARY KEY constraint on the "ID" column when CUSTOMERS table already exists, use the following SQL syntax:
ALTER TABLE CUSTOMER ADD PRIMARY KEY (ID);
NOTE: If you use the ALTER TABLE statement to add a primary key, the primary key column(s) must already have been declared to not contain NULL values (when the table was first created).
For defining a PRIMARY KEY constraint on multiple columns, use the following SQL syntax:
CREATE TABLE CUSTOMERS(
       ID   INT              NOT NULL,
       NAME VARCHAR (20)     NOT NULL,
       AGE  INT              NOT NULL,
       ADDRESS  CHAR (25) ,
       SALARY   DECIMAL (18, 2),       
       PRIMARY KEY (ID, NAME)
);
To create a PRIMARY KEY constraint on the "ID" and "NAMES" columns when CUSTOMERS table already exists, use the following SQL syntax:
ALTER TABLE CUSTOMERS
   ADD CONSTRAINT PK_CUSTID PRIMARY KEY (ID, NAME);
Delete Primary Key:
You can clear the primary key constraints from the table, Use Syntax:
ALTER TABLE CUSTOMERS DROP PRIMARY KEY ;

SQL> desc cust;
 Name                                      Null?    Type
 ----------------------------------------- -------- ----------------------------

 ID                                        NOT NULL NUMBER(38)
 NAME                                      NOT NULL VARCHAR2(20)
 AGE                                       NOT NULL NUMBER(38)
 ADDRESS                                            CHAR(25)
 SALARY                                             NUMBER(18,2)

SQL> alter table cust add primary key(id);
alter table cust add primary key(id)
*ERROR at line 1:  ORA-02260: table can have only one primary key

SQL> create table cust1(id int not null, name varchar2(20) not null, age int not null, address char(25), salary decimal(18,2));

Table created.

SQL> alter table cust1 add primary key(id,name);
Table altered.

SQL> desc cust1;
 Name                                      Null?    Type
 ----------------------------------------- -------- ----------------------------

 ID                                        NOT NULL NUMBER(38)
 NAME                                      NOT NULL VARCHAR2(20)
 AGE                                       NOT NULL NUMBER(38)
 ADDRESS                                            CHAR(25)
 SALARY                                             NUMBER(18,2)

SQL> alter table cust1 add constraint pk_custid primary key(id,name);
alter table cust1 add constraint pk_custid primary key(id,name)
                                       *
ERROR at line 1: ORA-02260: table can have only one primary key

SQL> alter table cust1 drop primary key;
Table altered.

SQL> alter table cust drop primary key;
Table altered.

SQL> alter table cust add constraint pk primary key(id , name);
Table altered.

SQL> desc cust;
 Name                                      Null?    Type
 ----------------------------------------- -------- ----------------------------

 ID                                        NOT NULL NUMBER(38)
 NAME                                      NOT NULL VARCHAR2(20)
 AGE                                       NOT NULL NUMBER(38)
 ADDRESS                                            CHAR(25)
 SALARY                                             NUMBER(18,2)

SQL>

13, అక్టోబర్ 2013, ఆదివారం

Celkon CT910+ HD tablet with voice calling launched for Rs. 7,999


Celkon-tablet-HD.jpg

Celkon Mobiles has launched yet another tablet in the Indian market - CT910+ HD. Celkon CT910+ comes with a 7-inch five point touch display.The screen resolution of this tablet is 960x540 pixels. Celkon CT910+ packs in a 1GHz dual-core processor with 512MB of RAM. The tablet has an internal storage of 4GB, of which 1.7GB is user accessible.
For camera, there is a 2-megapixel rear camera and a VGA front camera. Celkon CT910+ comes with 3500mAh battery and runs on Android 4.1 (Jelly Bean). It is a 3G tablet that supports voice calling. Other connectivity options include Wi-Fi, Bluetooth and GPS. The tablet comes with a price tag of Rs. 7,999.
This tablet will be competing with the likes of Micromax Funbook Talk P362 Tablet, Lava E-Tab XTRON+ and Zync Dual 7.0. All these are 7-inch offerings. Micromax Funbook Talk P362 comes with a screen resolution of 480X800 pixels. It is powered by 1.2GHz dual-core Cortex A9 processor with 1GB of RAM. The tablet has a 4GB internal storage that can be expanded by up to 32GB via microSD card. This tablet runs on Android 4.1 (Jelly Bean) and is available at a best buy price of Rs. 6,999
Priced at Rs. 6,990, Lava E-Tab XTRON+ comes with 7-inch capacitive multi-touch IPS display and has a resolution of 1024X600 pixels. It is powered by a 1.5GHz dual-core processor along with 1GB of RAM and 8GB of internal storage, which can be further expanded by up to 32GB via a microSD card. This tablet runs on Android 4.2.2 (Jelly Bean), and has a 3,700mAh battery.
Zync Dual 7.0 is priced at Rs. 5,990 and has a screen resolution of 800X480 pixels. It comes with 1.6 GHz dual-core tablet with 1GB of RAM, 8GB of internal storage. There is a 0.3-megapixel front camera and a 2-megapixel rear camera. The tablet runs on Android 4.1 (Jelly Bean) and is powered by 3,000mAh battery.
Celkon CT910+ HD key specifications
  • 7-inch display
  • 1GHz dual-core processor
  • 512MB RAM
  • 4GB Internal storage
  • 2-megapixel rear camera
  • VGA front camera
  • 3G with Voice Calling, Wi-Fi, Bluetooth and GPS
  • 3500mAh battery
  • Android 4.1.2 Jelly bean
For the latest technology news and reviews, like us on Facebook or follow us on Twitter and get the NDTV Gadgets app for Android or iOS.

ABOUT THE INTEGERITY CONSTARAINTS IN ORACLE



SQL> select seq1.nextval from dual;

   NEXTVAL
----------
        25

SQL> select seq1.currval from dual;

   CURRVAL
----------
        25

SQL> create sequence seq2 minvalue 1 maxvalue 1000 start with 1 increment by -2
cache 5;

Sequence created.

SQL> select seq1.currval from dual;

   CURRVAL
----------
        25

SQL> select seq2.currval from dual;
select seq2.currval from dual
       *
ERROR at line 1:
ORA-08002: sequence SEQ2.CURRVAL is not yet defined in this session


SQL> select seq2.nextval from dual;

   NEXTVAL
----------
         1

SQL> select seq2.nextval from dual;
select seq2.nextval from dual
                         *
ERROR at line 1:
ORA-08004: sequence SEQ2.NEXTVAL goes below MINVALUE and cannot be instantiated


SQL> select seq2.currval from dual;

   CURRVAL
----------
         1

SQL> select * from dept;

    DEPTNO DNAME                     NOEMP
---------- -------------------- ----------
         1 production                   22
         2 marketing                    30
         3 finance                      15
         4 auditing                     20
         5 hr                           10
         6 management                   25

6 rows selected.

SQL> create synonym ramusyn for dept;

Synonym created.

SQL> select * from ramusyn;

    DEPTNO DNAME                     NOEMP
---------- -------------------- ----------
         1 production                   22
         2 marketing                    30
         3 finance                      15
         4 auditing                     20
         5 hr                           10
         6 management                   25

6 rows selected.

SQL> select * from ramusyn;

    DEPTNO DNAME                     NOEMP
---------- -------------------- ----------
         1 production                   22
         2 marketing                    30
         3 finance                      15
         4 auditing                     20
         5 hr                           10
         6 management                   25

6 rows selected.

SQL> create table un(accno number(3) unique, not null);
create table un(accno number(3) unique, not null)
                                        *
ERROR at line 1:
ORA-00904: : invalid identifier


SQL> create table un(accno number(3) unique);

Table created.

SQL> create table un1(accno number(3) not null);

Table created.

SQL> insert into un1 values(&accno);
Enter value for accno: 100
old   1: insert into un1 values(&accno)
new   1: insert into un1 values(100)

1 row created.

SQL> /
Enter value for accno: 100
old   1: insert into un1 values(&accno)
new   1: insert into un1 values(100)

1 row created.

SQL> select * from un1;

     ACCNO
----------
       100
       100

SQL> /

     ACCNO
----------
       100
       100

SQL> insert into un1 values(&accno);
Enter value for accno:
old   1: insert into un1 values(&accno)
new   1: insert into un1 values()
insert into un1 values()
                       *
ERROR at line 1:
ORA-00936: missing expression


SQL> insert into un values(&accno);
Enter value for accno: 100
old   1: insert into un values(&accno)
new   1: insert into un values(100)

1 row created.

SQL> /
Enter value for accno: 100
old   1: insert into un values(&accno)
new   1: insert into un values(100)
insert into un values(100)
*
ERROR at line 1:
ORA-00001: unique constraint (SYSTEM.SYS_C004030) violated


SQL> /
Enter value for accno:
old   1: insert into un values(&accno)
new   1: insert into un values()
insert into un values()
                      *
ERROR at line 1:
ORA-00936: missing expression


SQL> /
Enter value for accno: null
old   1: insert into un values(&accno)
new   1: insert into un values(null)

1 row created.

SQL> select *from un;

     ACCNO
----------
       100


SQL>                 *
ERROR at line 1:
ORA-00907: missing right parenthesis


SQL> create table tra(accno number , trans varchar2(1), amount number(3), constr
aint noway forigen key(accno) references master(accno));
create table tra(accno number , trans varchar2(1), amount number(3), constraint
noway forigen key(accno) references master(accno))

                 *
ERROR at line 1:
ORA-00907: missing right parenthesis



























A live demonstration for the primary key, foreign keys and not null keys
SQL> CREATE TABLE CUSTOMERS(
  2         ID   INT              NOT NULL,
  3         NAME VARCHAR (20)     NOT NULL,
  4         AGE  INT              NOT NULL,
  5         ADDRESS  CHAR (25) ,
  6         SALARY   DECIMAL (18, 2),
  7         PRIMARY KEY (ID)
  8  );

Table created.
SQL> CREATE TABLE ORDERS (
  2         ID          INT        NOT NULL,
  3         DATE        DATETIME,  //here the problem is  coming
  4         CUSTOMER_ID INT references CUSTOMERS(ID),
  5         AMOUNT     double,
  6         PRIMARY KEY (ID)
  7  );
       DATE        DATETIME,  ERROR at line 3:  ORA-00904: : invalid identifier

SQL> create table orders(id int not null, c_id int references customers(id), pri
mary key(id));
Table created.
SQL> desc customers;
 Name                                      Null?    Type
 ----------------------------------------- -------- ----------------------------

 ID                                        NOT NULL NUMBER(38)
 NAME                                      NOT NULL VARCHAR2(20)
 AGE                                       NOT NULL NUMBER(38)
 ADDRESS                                            CHAR(25)
 SALARY                                             NUMBER(18,2)

SQL> desc orders;
 Name                                      Null?    Type
 ----------------------------------------- -------- ----------------------------

 ID                                        NOT NULL NUMBER(38)
 C_ID                                               NUMBER(38)

SQL> insert into customers values(&id , '&name' , &age , '&address' , &salary);
Enter value for id: 10
Enter value for name: ramu
Enter value for age: 36
Enter value for address: jimma
Enter value for salary: 1000
old   1: insert into customers values(&id , '&name' , &age , '&address' , &salary)
new   1: insert into customers values(10 , 'ramu' , 36 , 'jimma' , 1000)
1 row created.

SQL> insert into orders values(12345, 11);
insert into orders values(12345, 11)
*ERROR at line 1: ORA-02291: integrity constraint (SYSTEM.SYS_C004040) violated - parent key not found

SQL> insert into orders values(12345, 10);
1 row created.