Database study notes day01 + day02

- a system time
SELECT SYSDATE from Dual
 
- shows the basic structure of a relational database
- a two-dimensional table, the columns and rows
- row is called a record, called fields column
- a first table
Create Table HW (
name VARCHAR2 (20 is), int data type is used here --Java varchar2, number (6,2) 6 bits decimal point of 2
Age Number (2),
address VARCHAR2 (50)
);
- Table View created
desc hw;
- delete a table (actually delete the table structure)
drop the Table hw;
--修改表结构alter table name modify();
alter table hw modify(name varchar2(15));
alter table hw drop(name);
alter table hw add(Xingming varchar2(20));
- Modify the table name
rename hw to hw1;
- an insert data
insert into hw (name, age, address) values ( ' Li', 22, 'a long clear zone Court Myrtle')
INSERT INTO HW (name) values ( 'old')
- View data table
select * from hw;
- delete data table delete [from] hw where xxx = xxx; delete to delete the data in the table, drop delete header
delete hw where name = 'Li';
--修改数据update name set col='' where col=''
update hw set age=21 where age is null;
update hw set name='再见' where age is not null;
update hw set name='' where age=null;
char (1) - represents a character, often expressed gender
--DDL: a data definition language, the operation header. Table name Create (); drop Table name; ALTER Table motify name ();
--DML: Data Manipulation Language, operation data insert into name () values () ; delete [from] hw where xxx = xxx; update name set xxx = where yyy = null;
 
--drop table emp: Delete table emp;
--char & VARCHAR2: char: fixed-length character, declare how much occupied, not enough to fill the space. varchar2 variable-length character, how much actual occupancy with.
--char maximum 2000 bytes, varchar2 maximum 4000 bytes, long the largest 2G, clob largest 4G, a table can have only one clob.
--concat: The two fields into one field
- Connector ||
 
Example:
- Create a table emp
create table emp(
empno number(4,0),
ename varchar2(10),
job varchar2(9),
rngr number(4,0),
hiredate date,
sal number(7,2),
comm number(7,2),
deptno number(2,0)
);
- inserting data into the table
insert into emp values(7369,'smith','clerk',7902,to_date('1980/12/17','yyyy-mm-dd'),800.00,null,20);
insert into emp values(7499,'allen','salesman',7698,to_date('1981/12/20','yyyy-mm-dd'),1600.00,300.00,30);
insert into emp values(7521,'ward','salesman',7698,to_date('1982/2/22','yyyy-mm-dd'),1250.00,500.00,30);
insert into emp values(7566,'jones','manager',7839,to_date('1981/4/2','yyyy-mm-dd'),2975.00,null,20);
insert into emp values(7654,'martin','manager',7698,to_date('1981/9/28','yyyy-mm-dd'),1250.00,1400.00,30);
insert into emp values(7698,'blake','manager',7839,to_date('1981/5/1','yyyy-mm-dd'),2850.00,null,30);
insert into emp values(7782,'clark','manager',7839,to_date('1981/6/9','yyyy-mm-dd'),2450.00,null,10);
insert into emp values(7788,'scott','analyst',7566,to_date('1987/4/19','yyyy-mm-dd'),3000.00,null,20);
insert into emp values(7839,'king','president',null,to_date('1981/11/17','yyyy-mm-dd'),5000.00,null,10);
insert into emp values(7844,'turner','salesman',7698,to_date('1981/9/8','yyyy-mm-dd'),1500.00,0.00,30);
insert into emp values(7876,'adamas','clerk',7788,to_date('1987/5/23','yyyy-mm-dd'),1100.00,null,20);
insert into emp values(7900,'james','clerk',7698,to_date('1981/12/3','yyyy-mm-dd'),950.00,null,30);
insert into emp values(7902,'ford','analyst',7566,to_date('1981/12/3','yyyy-mm-dd'),1300.00,null,20);
insert into emp values(7934,'miller','clerk',7782,to_date('1982/1/23','yyyy-mm-dd'),1300.00,null,10);
commit              --提交
select * from emp; - view the contents of the table
- || connector
SELECT the concat (the concat (ename, ':'), SAL) from EMP;
SELECT ename || ':' || SAL from EMP; - See Table enama and sal, and connected
- length returns the length of the string
select ename, length (ename) from emp; - See table header length in ename
--lower, upper, initcap: change case or initials.
select lower (ename) from emp; - converting in lowercase and enanme attribute lists
select upper (ename) from emp; - converted to upper case
select initcap (ename) from emp; - conversion hyphens uppercase
- truncated substring / truncated left / right truncation: TRIM / LTRIM / RTRIM
SELECT TRIM (from EMPNO. 7) from EMP; - truncated 7
select ltrim ( 'qwer', ' q') from dual; - from the left amputated Q
SELECT RTRIM ( 'qwer', 'r') from Dual; - r truncated from the right
- complement bit function lpad / rpad
SAL from EMP SELECT;
SELECT LPAD (SAL,. 5, '+') from EMP; --sal up to five, sal + complement itself is not
- taken character substr ( '', m, n ) from the m-th start, intercepting n characters
select substr ( 'Following the track of the gale, I am chasing the sun.', 33,25) from dual; - Chasing The Sun AM to obtain the I.
--instr (char1, char2) trans char2 char1 back in the position (of several)
SELECT InStr ( 'qwer', 'W') from Dual;
 
--round (num, int): rounding, decimal Reserved int, int + 1 bit rounded
SELECT round (3.1415926,2) from Dual;
SELECT round (46.33, -1) from Dual;
 
--trunc (num, int): unconditionally discarded digits after the decimal int bit
select trunc (123456, -2) from dual; - bits is -1, -2 ten.
 
--mod (m, n) Returns the n I m
select mod(5,3)from dual;
select sal,job from emp;
 
--ceil / floor: up / down rounding
SELECT ceil (3.18) from Dual;
SELECT Floor (3.18) from Dual;
 
--to_date (): Convert string format into the system format
- 81 years later recruits who have
select *from emp where hiredate>to_date('1987-01-01','yyyy-mm-dd');
 
--to_char: into a string format
SELECT SAL, ename from EMP;
SELECT ename, Job, TO_CHAR (HireDate, 'YYYY "of" mm "month" dd "Day"') from emp;
 
--last_day (time): Returns the time where the last day of the month
select last_day (sysdate) from dual;
- View hires each month of the last day of the
select last_day (hiredate) from emp;
 
--add_months (d, i) through the return date d i is the day months
select add_months (sysdate, 10) from dual;
 
- View hires twentieth anniversary of the
select add_months (hiredate, 12 * 20 ) from emp;
--months_between (time1, time2): calculated time1 and time2 separated for many months

- how long handover
select round (months_between (sysdate, to_date ( '1997-07-1', 'yyyy-mm-dd'))) from dual;
 
--计算我的一百周年诞辰
select round(months_between(to_date('2097-11-6','yyyy-mm-dd'),sysdate))from dual;
select round(months_between(add_months(to_date('1997-11-6','yyyy-mm-dd'),12*100),sysdate))from dual;
 
--next_day (): Returns the next few weeks, depending on the week of the week have not had, over the next week refers, not too refers to this week,
the SELECT NEXT_DAY (sysdate, 1) from Dual;
 
--nvl (arg1, arg2): If the first argument is null, then converted to the second argument.
- took the view employee each month how much money

select ename,sal,comm,sal+nvl(comm,0) from emp;
--nvl2 (arg1, arg2, arg3) : determining whether the first parameter is null, if the first argument is null is returned arg3, otherwise arg2
SELECT ename, SAL, COMM, NVL2 (COMM, SAL + COMM, SAL ) from emp;
 
- Alias
select ename e, job j from emp ;
 
General conditions for adding --where

- View size ten sectors person
* from emp the SELECT;
the SELECT * from emp the WHERE deptno = 10;
- who is the manager

select *from emp where job='manager';

- Who salary greater than two thousand
select * from emp where sal> 2000 ;
 
--and & or used in the condition, and represents the mean or &
select * from emp where sal> 2000 and job = 'manager';

- View entry time is 81 years later, and 20 people in the department of
select * from emp where deptno = 20 and hiredate> to_date ( '1981-01-01', 'yyyy-mm-dd'); --- * *
 
--like 模糊查询
select *from emp where ename like '_a%';
select *from emp where job like '%na%';
 
--in&not in 在&不在什么内
select * from emp where job not in ('manager','clerk');
select * from emp where job !='manager' and job !='clerk';
 
--between a and b:查看在a和b范围内有撒
select *from emp where hiredate between to_date('1981-01-01','yyyy-mm-dd')and to_date('1982-12-31','yyyy-mm-dd');
 
-> any more than the minimum <any smaller than the maximum> all greater than the maximum <all less than the minimum
select * from emp where sal> any (1500,3000) and sal <any (1500,3000);
 
--distinct (): eliminating duplicate
select distinct (ename) from emp;

Guess you like

Origin www.cnblogs.com/cgwjava/p/11420421.html