mysql基本操作

mySQL
=============================================
DML;
SELECT - 从数据库中提取数据
UPDATE - 更新数据库中的数据
DELETE - 从数据库中删除数据
INSERT INTO - 向数据库中插入新数据

DDL;
CREATE DATABASE - 创建新数据库
ALTER DATABASE - 修改数据库
CREATE TABLE - 创建新表
ALTER TABLE - 变更(改变)数据库表
DROP TABLE - 删除表
CREATE INDEX - 创建索引(搜索键)
DROP INDEX - 删除索引


操作符select
----------------------------------
select * from table_name -全表扫描
distinct -去掉重复项 select distinct * from table_name

where子句
where -有条件选择 select * from table_name where column_name='xx'
and -且得关系(交集),连接条件 select * from table_name where column_name1='xx' and column_name2='xx1'
or -或的关系(并集) select * from table_name where column_name1='xx' or column_name2='xx1'
order by asc(可以不写) -对结果集排序。默认顺序排序。select * from table_name order by column_name
order by desc -对结果集排序。逆序排序。select * from table_name order by column_name desc
like -模式匹配(%定义通配符)。select * from table_name where column_name like'aa%';
in -允许where子句中有多个值。select * from table_name where column_name in(value1,value2,...);
between and -,获取闭区间数据(not表示补集),where子句中使用,select * from table_name where column_name between 'value1' and 'value2'

别名
select * from table_name as newName - 使用表别名(oracle不需要as)
select column_name as newName from table_name as -使用列别名

join多表关联:
join(inner join) -内连接,返回两个表都有的,并集 。
select * from table_name1 inner join table_name2 on table_name1.column_name=table_name2.column_name;
left join -左连接,返回两个表都有的,还有左表有右表没有的, 
right join -右连接,返回两个表都有的,还有右表有左表没有的, 
full join -全连接,只要其中一个表有即可。

独立集(只是a独有的部分) SELECT * FROM a left outer JOIN b ON a.id=b.id WHERE (b.id IS null)


操作符insert into和update和delete
----------------------------------
insert into table_name(column_name1) values(column_name1) -插入选择字段
insert into table_name() values(column_name1) -插入全表
update table_name set column_name='新值' where column_name='定位值' -更新数据
delete from table_name where column_name='xx' -删除数据


操作符union
----------------------------------
union 合并两个或多个sql语句的结果集。不允许重复的值出现。
union all ,允许重复的值出现。
SELECT column_name(s) FROM table_name1
UNION
SELECT column_name(s) FROM table_name2


操作符select into 
----------------------------------
select into 创建表的备份附件(oracle不能用)
select * into new_tableName from old_tableName
SELECT *
INTO Persons IN 'Backup.mdb'
FROM Persons
oracle可以用:create table new_tableName as (select * from old_tableName)

操作符create table
----------------------------------
数据类型:
char(10) 定长,查询快,浪费空间 最大2000字符
varchar2(20) 变长,查询慢,节省空间 最大4000
number
number(5,2)五位数,2位有效小数
number(5) 五位整数
date 时间类型(日-月-年)(01-5月-05)
timestamp 时间类型比date精确
blob 二进制类型 图片声音

约束:
not null -非空约束,not null
unique -唯一约束,unique,CONSTRAINT uc_PersonID UNIQUE (Id_P,LastName)
primark key -主键约束 ,primark key CONSTRAINT pk_PersonID PRIMARY KEY (Id_P,LastName)
forein key -外键 ,Id_P int FOREIGN KEY REFERENCES Persons(Id_P)
check -检查约束 ,check (id>0)
default -默认约束 ,default ‘xx’

AUTO_INCREMENT -自动创建主键字段的值


操作符create index
----------------------------------
create index index_name on table_name (column_name)
CREATE INDEX PersonIndex ON Person (LastName DESC) --降序索引
drop index index_name

操作符create view
----------------------------------
create view view_name as SELECT column_name(s) FROM table_name WHERE condition
drop view view_name

操作符create database
----------------------------------
create database db_name

操作符drop
----------------------------------
delete table table_name -删除数据,可以回滚
truncate table table_name -清空数据(不可以回滚),不删除表结构
drop table table_name -删除数据表(删除内容和定义,释放空间。)
drop index index_name -删除索引
drop view view _name -删除视图
drop database db_name -删除数据库

操作符alter
----------------------------------
alter table table_name add column_name datatype -添加字段
alter table table_name drop column column_name -删除字段
alter table MYRESULTTABLE_201411170930 rename to MYRESULTTABLE;--修改表名


操作符case
----------------------------------
select case column_name
when 'x1' then 'y1'
when 'x2' then 'y2'
else 'y3' end

函数
----------------------------------
avg() -平均值 select avg(column_name) from table_name
count(column_name) -返回行数(null排除)
count(distinct column_name) -返回行数(重复项和null排除)
max() -返回列最大值
min() -返回列最小值
sum() - 返回数值列的总和
sum(distinct) - 返回数值列的总和(去重)
group by -按照指定列分组,select id ,avg(column_name) from table_name group by id

你可能感兴趣的:(数据库)