数据库中创建表(包括创建主键,外键,非空列,唯一)

创建主键(三种方法)

****************

创建学生表:

第一种:

create table student

(sno char(5) primary key,/*学号*/ /*可以直接指定主键*/

sname char(20) not null,/*姓名*/

ssex char(3) not null,/*性别*/

sage integer not null,/*年龄*/

sdept char(15);/*系别*/

第二种:

create table student

(sno char(5) not null,

constraint pk_student   /*可以指定主键名称*/

primary key (sno),

sname char(20) not null ,/*非空,不可重复*/

ssex char(3) not null,

sage integer ,

sdept char(15));

第三种:

创建课程表:

create table course

(cno char(5),/*课程号*/

cname char(20) not null unique,/*课程名,非空,不可重复*/

cpno char(5),/*前置课程号(学此课之前必修课)*/

ccredit number);/*学分*/

通过修改表,设置主键.

alter table course

add constraint pk_course

primary key (cno);

*************

创建外键(3种方法)

第一种

*************

创建"学生-课程-成绩"表

create table sc

(sno char(5) constraint fk_student   /*第一种,写在属性定义里面的*/  /*可以指定外键名称,*/

references student(sno),

cno char(5),

foreign key(cno) /*可省略外键名称*/

references course(cno),

grade number);

**********

第二种

**********

create table sc

(sno char(5),

constraint fk_student   /*第二种,写在属性定义外面的*/

foreign key (sno)

references student(sno),

cno char(5),

foreign key(cno)

references course(cno),

grade number);

***********

第三种

***********

alter table sc

add constraint fk_student

foreign key(sno)

references student(sno);

****************************

建好表之后,新增或修改,删除约束

****************************

先用此语句查看某个表的所有约束:

select TABLE_NAME,CONSTRAINT_NAME,SEARCH_CONDITION,STATUS from user_constraints WHERE TABLE_name=upper('&TABLE_Name');

0.增加not null约束

alter table student modify sname not null;

1.修改null约束

alter table student modify sname null;/*姓名可以为空*/

2.增加主键约束

alter table student

add constraint pk_student

primary key(id);

3.删除主键约束

先用此语句查看某个表的所有约束:

select TABLE_NAME,CONSTRAINT_NAME,SEARCH_CONDITION,STATUS from user_constraints WHERE TABLE_name=upper('&TABLE_Name');

这里要删除的是约束"fk_student"

alter table drop constraint  fk_student;

4.增加外键约束

alter table sc

add constraint fk_student

foreign key(sno)

references student(sno);

5.删除外键约束

需要知道要删除的外键名称,

先用此语句查看某个表的所有约束:

select TABLE_NAME,CONSTRAINT_NAME,SEARCH_CONDITION,STATUS from user_constraints WHERE TABLE_name=upper('&TABLE_Name');

alter table drop constraint fk_student;

6.添加unique约束

alter table course modify cname unique;

添加unique的同时,会自动创建一个相同名称的索引.

7.取消unique

alter table course

drop constraint sys_c005472;

8.删除索引

查找要删除的索引名称

运行此语句:

select index_name,table_owner,table_name,tablespace_name,status from user_indexes order by table_name;

然后运行

drop index sys_coo5472;

今天在对一个表,对其中一个属性,新建unique约束,

但是提示:ora-02261:such unique or primary key already exists in the table

原因是该属性已经是primary key了。

如果该属性已经有unique约束,则不能赋予该属性primary key约束.

1.一个表只能有一个primary key,可以有多个unique约束

2.当创建primary key的时候,会同时建立一个索引,名字和primary key的名称相同.

3.当某属性为primary key,会默认新增not null约束,当删除primary key后,not null属性取消.

4.表的一个属性可以同时设定not null和primary key约束.


  1. 这里先新建一个表,其中ID就是要设为主键的column。

    创建table的代码如下:

    CREATE TABLE test_tab

    (

    id NUMBER,

    name VARCHAR2(30)

    )

  2. 要设置主键的话这里需要用到alter table这个语法。

    设置主键的代码如下:

    ALTER TABLE test_tab ADD CONSTRAINT pk_test_tab PRIMARY key(id);

  3. 这里解释下:

    alter table table_name-----------------------意思就是要改动某个表

    add constraint constraint_name-------------意思就是给某个表加约束/限制

    primary key(column_name)------------------说明是主键的约束,而且是某个column的。

  4. 执行之后就设置成功了,若不放心可以亲自测试下。

    执行如下代码一次:

    INSERT INTO test_tab(id,name) VALUES (1,'test');

    看到已经insert成功!

  5. 在重复的执行一次:

    INSERT INTO test_tab(id,name) VALUES (1,'test');

    这个时候就有error了:

    ORA-00001: unique constraint (SCOTT.PK_TEST_TAB) violated

 

表的主键就这样设置成功。


create table语句中如何设置主键-CSDN论坛 http://bbs.csdn.net/topics/90208943

create table tt
 (id int, 
  code varchar(9)
  primary key(id,code)
  )

CREATE TABLE tablename (
id int not null PRIMARY KEY ,
name varchar(40)
)
go

or:
CREATE TABLE tablename (
id1 int not null ,
id2 int not null ,
name varchar(40),
CONSTRAINT pk_tb PRIMARY KEY CLUSTERED
(id1,id2)
)
go

CREATE TABLE [dbo].[tt](
a [varchar](30) COLLATE Chinese_PRC_CI_AS NOT NULL,
b [varchar](10) COLLATE Chinese_PRC_CI_AS NOT NULL,
c [varchar](30) COLLATE Chinese_PRC_CI_AS NOT NULL,
d [money] NULL,
e [money] NULL,
f [money] NULL,
 CONSTRAINT [PK_tt] PRIMARY KEY NONCLUSTERED 
(
a ASC,
b ASC,
c ASC
) ON [PRIMARY]
) ON [PRIMARY]
以上是多表的

CREATE TABLE 表名(
id int not null PRIMARY KEY ,
name varchar(40)
)
go

PRIMARY KEY 是设置主键的关键字

【转载自】PROS - 博客园 http://www.cnblogs.com/CodingArt/articles/1621921.html

原文地址:https://www.cnblogs.com/wxl845235800/p/7406851.html