Содержание
I’m trying to create a table with primary and foreign keys using following syntax for sql developer but it is not working.
Can anyone see where I am going wrong?
3 Answers 3
You forgot the TABLE keyword.
this should work
just create table first and then try to alter the table
ALTER TABLE Athlete ADD CONSTRAINT ATHLETE_PK PRIMARY KEY (ATHLETEID);
for foreign key try this
ALTER TABLE Athlete ADD CONSTRAINT ATHLETE_FK FOREIGN KEY (COACHID) REFERENCES COACH(COACHID);
Первичный ключ PRIMARY KEY
PRIMARY KEY — первичный ключ, ограничение, позволяющее однозначно идентифицировать каждую запись в таблице SQL.
PRIMARY KEY Oracle
Первичный Ключ (PRIMARY KEY) может ограничивать таблицы или их столбцы. Это ограничение работает так же как и ограничение UNIQUE. Но следует учитывать различие между первичными ключами и уникальностью столбцов в способе их использования с внешними ключами. Первичные ключи не могут позволять значений NULL. Это означает что, подобно полям в ограничении UNIQUE, любое поле, используемое в ограничении PRIMARY KEY, должно уже быть обьявлено NOT NULL.
PRIMARY KEY Oracle. Пример №1.
Пример создания таблицы SQL с ограничением PRIMARY KEY:
CREATE TABLE Student
( Kod_stud integer NOT NULL PRIMARY KEY,
Fam char(30) NOT NULL UNIQUE,
Adres char(50),
Ball decimal);
Лучше всего помещать ограничение PRIMARY KEY в поле (или в поля), которое будет образовывать уникальный идентификатор строки, и сохранить ограничение UNIQUE для полей которые должны быть уникальными логически (такие как номера телефона или поле sname), а не для идентификации строк. Ограничение PRIMARY KEY может также быть применено для многочисленных полей, составляющих уникальную комбинацию значений:
PRIMARY KEY Oracle. Пример №2.
CREATE TABLE Student
( Fam char (30) NOT NULL,
Im char (30) NOT NULL
Adres char (50),
PRIMARY KEY (Fam, Im));
PRIMARY KEY MySQL
PRIMARY KEY SQL / MySQL. Пример №3.
CREATE TABLE Persons (
P_Id int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Address varchar(255),
City varchar(255),
PRIMARY KEY (P_Id));
PRIMARY KEY SQL / MySQL. Пример №4.
CREATE TABLE `ad_packages` (
`id` int(111) NOT NULL auto_increment,
`title` varchar(132) NOT NULL default »,
`price` float NOT NULL default ‘0’,
`type` varchar(22) NOT NULL default »,
`c_type` enum(‘cash’,’points’,’rur’) NOT NULL default ‘cash’,
PRIMARY KEY (`id`)
);
PRIMARY KEY SQL / MySQL. Пример №5.
CREATE TABLE `gamestat` (
`id` int(11) NOT NULL auto_increment,
`game` varchar(10) NOT NULL default ‘tuz’,
`stavok` int(11) NOT NULL default ‘0’,
`usd` float NOT NULL default ‘0’,
`rur` float NOT NULL default ‘0’,
`point` float NOT NULL default ‘0’,
`bank_usd` decimal(12,2) NOT NULL default ‘0.00’,
`bank_rur` decimal(12,2) NOT NULL default ‘0.00’,
`bank_point` decimal(12,2) NOT NULL default ‘0.00’,
PRIMARY KEY (`id`)
);
Вы должны войти, чтобы оставить комментарий.
This Oracle tutorial explains how to create, drop, disable, and enable a primary key in Oracle with syntax and examples.
What is a primary key in Oracle?
In Oracle, a primary key is a single field or combination of fields that uniquely defines a record. None of the fields that are part of the primary key can contain a null value. A table can have only one primary key.
- In Oracle, a primary key can not contain more than 32 columns.
- A primary key can be defined in either a CREATE TABLE statement or an ALTER TABLE statement.
Create Primary Key — Using CREATE TABLE statement
You can create a primary key in Oracle with the CREATE TABLE statement.
Syntax
The syntax to create a primary key using the CREATE TABLE statement in Oracle/PLSQL is:
Example
Let’s look at an example of how to create a primary key using the CREATE TABLE statement in Oracle:
In this example, we’ve created a primary key on the supplier table called supplier_pk. It consists of only one field — the supplier_id field.
We could also create a primary key with more than one field as in the example below:
Create Primary Key — Using ALTER TABLE statement
You can create a primary key in Oracle with the ALTER TABLE statement.
Syntax
The syntax to create a primary key using the ALTER TABLE statement in Oracle/PLSQL is:
Example
Let’s look at an example of how to create a primary key using the ALTER TABLE statement in Oracle.
In this example, we’ve created a primary key on the existing supplier table called supplier_pk. It consists of the field called supplier_id.
We could also create a primary key with more than one field as in the example below:
Drop Primary Key
You can drop a primary key in Oracle using the ALTER TABLE statement.
Syntax
The syntax to drop a primary key using the ALTER TABLE statement in Oracle/PLSQL is:
Example
Let’s look at an example of how to drop a primary key using the ALTER TABLE statement in Oracle.
In this example, we’re dropping a primary key on the supplier table called supplier_pk.
Disable Primary Key
You can disable a primary key in Oracle using the ALTER TABLE statement.
Syntax
The syntax to disable a primary key using the ALTER TABLE statement in Oracle/PLSQL is:
Example
Let’s look at an example of how to disable a primary using the ALTER TABLE statement in Oracle.
In this example, we’re disabling a primary key on the supplier table called supplier_pk.
Enable Primary Key
You can enable a primary key in Oracle using the ALTER TABLE statement.
Syntax
The syntax to enable a primary key using the ALTER TABLE statement in Oracle/PLSQL is:
Example
Let’s look at an example of how to enable a primary key using the ALTER TABLE statement in Oracle.
In this example, we’re enabling a primary key on the supplier table called supplier_pk.