数据库的基础 | 青训营笔记

84 阅读1分钟

1、创建数据库

CREATE DATABASE database-name

2.删除数据库

drop database dbname

3、创建新表

create table tabname (col1 type1 [not nulll] [primary key]col2 type2 [not null],..)

根据已有的表创建新表: A: create table tab newlike tab old(使用旧表创建新表) B: create table tab new as select col1col2… from tab_old definition only

4、删除新表

drop table tabname

5、增加一个列

Alter table tabname add column col type

注:列增加后将不能删除。DB2中列加上后数据类型也不能改变,唯一能改变的是增加varchar类型的长度。

6、添加主键

Altertabletabnameadd primary key(col)

7、删除主键

Altertable tabname drop primary key(col)

8、创建索引

create[unique]index idxname on tabname(col….)

9、删除索引

drop indexidxname

注:索引是不可更改的,想更改必须删除重新建。

10、创建视图

create viewviewnameas select statement

11、删除视图

drop view viewname