SQL之创建表
阅读原文时间:2023年07月08日阅读:1

1.创建表------

(方法一)-------create table Persons(id NUMBER,
                                       age NUMBER,
                                       name char)

但是没有设置主键,如果你想设置主键的话则可以使用:ALTER TABLE Persons ADD PRIMARY KEY (id)

去掉主键:ALTER TABLE Persons DROP CONSTRAINT pk_PersonID或者alter table Persons drop id   但是我在oracle上没操作成功!其实去掉主键可以使用

Navicate Premium去操作,非常的方便。

(方法二)------create table Students(stu_id NUMBER not null  primary key,
                      name VARCHAR(10) not null,
                      age NUMBER default 9,
                      grade NUMBER)

增加表的某一列:alter table students add birthday DATE

删除表的某一列:alter table students add bigname varchar(10) default 'shali'
                           alter table students drop column bigname

更新字段名:alter table students rename column bigname to oldname

更改某个字段的数据类型:alter table students modify column bigname varchar(9)  (验证没有成功!)

2.删除表-------

drop table Persons.

如果我们仅仅需要除去表内的数据,但并不删除表本身,那么我们该如何做呢?

truncate table students

3.check 约束

CHECK 约束用于限制列中的值的范围。

如果对单个列定义 CHECK 约束,那么该列只允许特定的值。

如果对一个表定义 CHECK 约束,那么此约束会在特定的列中对值进行限制。

create table Students(stu_id NUMBER not null  primary key,
                      name VARCHAR(10) not null,
                      age NUMBER default 9,
                      grade NUMBER,
                      check(age<100))

或者:create table Students(stu_id NUMBER not null  primary key,
                      name VARCHAR(10) not null  check(age<100),
                      age NUMBER default 9,
                      grade NUMBER,
                     )