這是一篇oracle的常用基本操作/語句
1.建立表語句
create table table_name (
column1 datatype1,
column1 datatype1,
......
columnn datatypen,
primary key (one or more columns)
);例如:
create table user_table
(pk_user verchar2(20) not null,
uer_name varchar2(32),
age smallint,
address verchar2(64)
constraint pk_user primary key (pk_user)
);建立表時最好同時加上注釋,例如
comment on table user_table is '使用者表'
comment on column user_table. pk_user is '主鍵';
comment on column user_table. uer_name is '使用者名稱';
comment on column user_table. age is '年齡';
comment on column user_table. address is '家庭住址';
2.刪除表
drop table table_name;
例如:drop table user_talbe;
3.插入資料
insert into table_name
(column1, column2, column3, ..., columnn)
values (value1, value2, value3, ... valuen);
例如:insert into user_table (pk_user, user_name, age, address)
values('pk0001', 'john', 23, '北京市朝陽區');
若是新增按表順序的全部字段可以省略列名,如下:
insert into user_table
values ('pk0002', 'tom', 24, '重慶市渝北區');
這裡提一下,可以使用select語句填充乙個表的資料到另乙個表中的字段
insert into first_table_name [(column1, column2, ... columnn)]
select column1, column2, ... columnn
from second_table_name
[where condition]
4.修改資料
update table_name
set column1= value1, column2= value2, ..., columnn=valuen
where [condition];
例如:update user_table
set address = '北京西城區'
where name = 'john';
5,刪除資料
delete from table_name
where [condition];
例如:delete from user_table
where name = 'john';
6.alter修改表
6.1更新欄位名
alter table table_name
rename column old_column_name to new_column_name;
6.2 新增字段
alter table table_name
add (column_name1 datatype1, column_name2 datatype2, ..., column_namen, datytypen);
6.3刪除字段
alter table table_name
drop column column_name;
刪除單個字段需要加column,刪除多個字段有所不同,不用加column
alter table table_name
drop (column1, column2, ..., columnn);
6.4修改字段資料型別
alter table table_name
modify (column_name1 datatype1, column_name2 datatype2, ..., column_namen, datytypen);
7.查詢資料
select column1, column2, ..., columnn from table_name;
例如:select pk_user, user_name, age, address from user_table;
查詢的是全部資料可以使用*來代替查詢字段,如下:
select * from user_table
7.1如果我們需要在查詢出來的資料集中新增列,並且該列是固定值,可以使用下面方法:
select t.*, constant_data colunm_name from table_name t;
例如:select u.*, '男' gender from user_table;
該語句就會查詢出user_talbe的全部字段,外加gender欄位,並且該字段的內容都是'男'
7.2在查詢的結果中增加乙個序號列,對生成的增加乙個序號列
select rownum, t.* from table_name t;
例如:select rownum, t.* from user_table t;
還有乙個解決方案是使用row_number()函式查詢時增加序列號
select row_number() over(order by t.column_name), t.* from table_name t
例如: select row_number() over(order by t.pk_user) serial_no, t.* from user_table t;
如果查詢出來的資料需要order by排序,則會打亂row_number()排序號,但是只要row_number()對order by的列與查詢出來的列是同一列即可
例如: select row_number() over(order by t.user_name) serial_no, t.* from user_table t order by t.user_name;
springJdbc常用的curd操作
注入jdbctemplate模板類 resource private jdbctemplate jdbctemplate 新增賬戶記錄,返回受影響的行數 param account return override public int addaccount account account int r...
Oracle與Oracle的SQL操作語句
oracle 也是一種資料庫管理系統 儲存結構分類 邏輯儲存結構,物理儲存結構 邏輯儲存結構 資料塊 資料塊是oracle邏輯儲存結構的最小邏輯結構,乙個資料塊對應乙個或多個物理塊,資料塊的結構包括塊頭和儲存區的兩個部分 塊頭包括 資料塊標題,表目錄,行目錄 儲存區 自由空間,行資料 資料區 資料區...
MySQL資料CURD操作
大聖網路 2017 01 28 08 23 我們平時90 以上都是對錶中資料進行增刪改查,而其中的查是使用最多最頻繁的操作。今天,說說幾個入門級的sql語句。新增資料 新增一條資料 方案1 給全表字段插入資料,不需要指定字段列表 要求資料的值出現的順序必須與表中設計的字段出現的順序一致 凡是非數值資...