Chinaunix首页 | 论坛 | 博客
  • 博客访问: 564613
  • 博文数量: 201
  • 博客积分: 7734
  • 博客等级: 少将
  • 技术积分: 1994
  • 用 户 组: 普通用户
  • 注册时间: 2010-04-09 19:18
文章分类

全部博文(201)

文章存档

2011年(28)

2010年(173)

分类: Mysql/postgreSQL

2010-06-12 18:15:18

更改管理员口令:
1、MySQL安装后,管理员(root)的口令默认为空,用mysqladmin进行更改:
mysqladmin -u root password redhat
 
2、登录MySQL数据库用:
mysql -u root -p
 
3、如果已经设置了口令,修改口令用:
mysqladmin -u root -p password oracle
Enter password:redhat
##把原来的redhat口令改为oracle。
 
MySQL服务器管理程序mysqladmin的使用:
1、查看MySQL服务器正在运行的线程列表:
mysqladmin -u root -p processlist
 
2、检查MySQL服务是否正在运行:
mysqladmin -u root -p ping
Enter password:oracle
 
DDL(Data Definition Languages) 数据定义语言
DML(Data Manipulation Language) 数据操纵语句
DCL(Data Control Lanaguage)   数据控制语句
 
DDL(Data Definition Languages)   >>>
1、创建数据库
CREATE DATABASE dbname;
2、选择要操作的数据库
USE dbname;
3、查看数据库中创建的所有数据表
SHOW tables;
4、删除数据库
drop database dbname;
5、创建表
CREATE TABLE tablename (column_name_1 column_type_1 constraints, column_name2 column_type2 constraints, ......column_name_n column_type_n constraints)
例:
create table emp (ename varchar(10),hiredate date,sal decimal(10,2),deptno int(2));
6、查看表的结构
desc emp;
7、全面的查看表的结构
show create table emp \G;
8、删除表
drop table tablename;
9、修改表
修改字段的定义:
ALTER TABLE tablename MODIFY [COLUMN] column_definition [FIRST | AFTER col_name]
例:
alter table emp modify ename varchar(20);
增加表字段
ALTER TABLE tablename ADD [COLUMN] column_definition [FIRST |AFTER col_name]
例如:
alter table emp add column age int(3);
删除表字段:
ALTER TABLE tablename DROP [COLUMN] col_name;
例:
alter table emp drop column age;
字段改名:
ALTER TABLE tablename CHANGE [COLUMN] old_col_name column_definition [FIRST |AFTER col_name]
例:
alter table emp change age age1 int(4);
change和modify都可以修改表的定义,不同的是change后面需要写两次列名,不方便。但是change的优点是可以修改列名称,modify则不能。
修改字段排列顺序:
alter table emp add birth date after ename;   把birth字段放到ename后面。
alter table emp add birth date first ;  把birth字段放在表的最前面。
注意:CHANGE/FIRST/AFTER COLUMN这些关键字都属于MYSQL在标准SQL上的扩展,在其它数据库上不一定适用。
=============================================================================================================
=============================================================================================================
DML(Data Manipulation Language)>>>
1、插入记录
INSERT INTO tablename (filed1,filed2,.....fieldn) VALUES (value1,value2,.....valuesn);
例:
INSET INTO emp (ename,hiredate,sa1,deptno) values ('zengqinghua','2010-05-10','2000',1);
INSET INTO emp  values ('ducong','2010-05-12','2000',1')
INSET INTO emp (ename,deptno) values ('zengqinghua',1)

一次插入多条记录:
INSERT INTO tablename (field1,field2,......fieldn)
values
(record1_value1,record1_value2,......record1_valuesn),
(record2_value1,record2_value2,......record2_valuesn),
......
(recordn_value1,recordn_value2,......recordn_valuesn);
2、更新记录:
UPDATE tablename SET field1=value1,field2=value2,......fieldn=valuen [WHERECONDITION]
update emp set sal=4000 where ename='ducong';
同时更新多个表中的数据:
UPDATE t1,t2.....tn.field1=expr1,tn.fieldn=exprn [WHERE CONDITION];
例:
update emp a,dept b set a.sal=a.sal*b.deptno,b.deptname=a.ename where a.deptno=b.deptno
select * from emp;
select * from dept;
select ename,hiredate,sal,deptno from emp;

4、查询记录:
查询一张表的所有信息:
SELECT * FROM tablename [WHERE CONDITION]
例:
mysql> select * from emp where ename='zengqinghua';
查询部分字段:
mysql> select ename,sal from emp;
查询不重复的记录:
mysql> select distinct deptno from emp; ##distinct
根据条件查询:
mysql> select * from emp where ename='zengqinghua';
注:where后面的条件是一个字段的=比较,除了=外,还可以使用>.<.>=.<=.!=等比较运算符。
mysql> select * from emp where deptno=2 and ename='zengqinghua';
排序和限制:
SELECT * FROM tablename [WHERE CONDITION] [ORDER BY field1 [DESC|ASC], field [DESC|ASC],......field [DESC|ASC]]
用order by来指定关键字来排序:
mysql> select * from emp order by deptno;    ##表示从小到大的排序;
mysql> select * from emp order by -deptno;   ##表示从大到小的排序;
=
mysql> select * from emp order by deptno DESC;

mysql> select * from emp order by deptno , hiredate desc; 表示在相同的字段中,diredate字段降序排序。
用LIMNIT来限制显示的内容:
SELECT ......[LIMIT offset_start,row_count]
例:
mysql> select * from emp order by deptno , hiredate desc limit 2;
mysql> select * from emp order by deptno , hiredate desc limit 1,2; ##表示从第二条记录开始显示,显示三条。

聚合:
SELECT [field1,field2,......fieldn] fun_name
FROM tablename
[WHERE where_contition]
[GROUP BY field1,field2,......fieldn
[WITH ROLLUP]]
[HAVING where_contition]
计算表中一共有多少行:
mysql> select count(1) from emp;
统计各部门的人数:
mysql> select deptno,count(1) from emp group by deptno;
统计各部门的人数,和总人数:
mysql> select deptno,count(1) from emp group by deptno with rollup;
统计人数大于1人的部门:
mysql> select deptno,count(1) from emp group by deptno having count(1) >1;
统计公司所有员工的薪水总额、最高和最低薪水:
mysql> select sum(sal),max(sal),min(sal) from emp;
表连接:
mysql> select ename,deptname from emp,dept where emp.deptno=dept.deptno;
外部连接分为左连接和右连接:
左连接:包含所有的左边表中的记录甚至是右边表中没有和它匹配的记录。
右连接:包含所有的右边表中的记录甚至是左边表中没有和它匹配的记录。
左连接
mysql> select ename,deptname from emp left join dept on emp.deptno=dept.deptno;
右连接
mysql> select ename,deptname from dept right join emp on emp.deptno=dept.deptno;
子查询:
从emp表中查询出所有部门在dept表中的所有记录。
mysql> select * from emp where deptno in (select deptno from dept);
从dept表中查询出所有部门在emp表中的所有记录。
mysql> select * from dept where deptno in (select deptno from emp);
如果子查询记录数唯一,还可以用=代替in;
mysql> select * from dept where deptno = (select deptno from emp limit 1);
某些情况,子查询可以转化为表连接,例如:
mysql> select * from emp where deptno in (select deptno from dept);
转化为表连接后:
mysql> select emp.* from emp ,dept where emp.deptno=dept.deptno;
记录联合:
SELECT * FROM t1
UNION | UNION ALL
SELECT * FROM t2
......
UNION | UNION ALL
SELECT * FROM tn;
mysql> select deptno from emp union all select deptno from dept;
mysql> select deptno from emp union  select deptno from dept;  #会把重复的去掉。
===============================================================================
===============================================================================
DCL(Data Control Lanaguage) >>>
创建一个数据库用户oracle,具有对qinghua数据库中所有表SEELECT/INSERT权限:
mysql> grant select,insert on qinghua.* to identified by '123';
mysql> insert into emp values ('hello','2001-10-10','9999',2);
Query OK, 1 row affected (0.04 sec)
删除oracle的insert权限:
mysql> revoke insert on qinghua.* from ;
mysql> insert into emp values ('dd','2000-1-3','3333',2);
ERROR 1142 (42000): INSERT command denied to user for table
 'emp'

帮助的使用:
按照层次看帮助:
mysql> ? contents
You asked for help about help category: "Contents"
For more information, type 'help ', where is one of the following
categories:
   Account Management
   Administration
   Data Definition
   Data Manipulation
   Data Types
   Functions
   Functions and Modifiers for Use with GROUP BY
   Geographic Features
   Language Structure
   Storage Engines
   Stored Routines
   Table Maintenance
   Transactions
   Triggers
   User-Defined Functions
   Utility
查看MySQL中都支持哪些数据数型:
mysql> ? data types
You asked for help about help category: "Data Types"
For more information, type 'help ', where is one of the following
topics:
   AUTO_INCREMENT
   BIGINT
   BINARY
   BIT
   BLOB
   BLOB DATA TYPE
   BOOLEAN
   CHAR
   CHAR BYTE
   DATE
   DATETIME
   DEC
   DECIMAL
   DOUBLE
   DOUBLE PRECISION
   ENUM
   FLOAT
   INT
   INTEGER
   LONGBLOB
   LONGTEXT
   MEDIUMBLOB
   MEDIUMINT
   MEDIUMTEXT
   SET DATA TYPE
   SMALLINT
   TEXT
   TIME
   TIMESTAMP
   TINYBLOB
   TINYINT
   TINYTEXT
   VARBINARY
   VARCHAR
   YEAR DATA TYPE
查看int的具体介绍:
mysql> ? int
Name: 'INT'
Description:
INT[(M)] [UNSIGNED] [ZEROFILL]
A normal-size integer. The signed range is -2147483648 to 2147483647.
The unsigned range is 0 to 4294967295.

快速查阅帮助:
如想知道show能看些什么东西。
? show
如查看create table的语法:
? create table
常用的网络资源:
http://dev.mysql.com/downloads/
http://dev.mysql.com/doc/


===========================================================
===========================================================
数据库内容:
mysql> show tables;
+-------------------+
| Tables_in_qinghua |
+-------------------+
| dept              |
| emp               |
+-------------------+
2 rows in set (0.00 sec)
mysql> desc emp;
+----------+---------------+------+-----+---------+-------+
| Field    | Type          | Null | Key | Default | Extra |
+----------+---------------+------+-----+---------+-------+
| ename    | varchar(20)   | YES  |     | NULL    |       |
| hiredate | date          | YES  |     | NULL    |       |
| sal      | decimal(10,2) | YES  |     | NULL    |       |
| deptno   | int(2)        | YES  |     | NULL    |       |
+----------+---------------+------+-----+---------+-------+
4 rows in set (0.04 sec)
mysql> desc dept;
+----------+-------------+------+-----+---------+-------+
| Field    | Type        | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| deptno   | int(3)      | YES  |     | NULL    |       |
| deptname | varchar(10) | YES  |     | NULL    |       |
+----------+-------------+------+-----+---------+-------+
2 rows in set (0.04 sec)
mysql> select * from emp;
+-------------+------------+------------+--------+
| ename       | hiredate   | sal        | deptno |
+-------------+------------+------------+--------+
| zengqinghua | 2010-05-12 |      40.00 |      2 |
| ducong      | 2010-05-13 |      40.00 |      3 |
| linscora    | 2010-05-14 |      40.00 |      2 |
| redhat      | 1993-09-09 | 4000000.00 |      4 |
| cisco       | 1903-09-09 | 4000000.00 |   NULL |
| hello       | 2001-10-10 |    9999.00 |      2 |
+-------------+------------+------------+--------+
6 rows in set (0.00 sec)
mysql> select * from dept;
+--------+----------+
| deptno | deptname |
+--------+----------+
|      2 | tech     |
|      3 | hr       |
|      4 | sal      |
|      5 | fin      |
+--------+----------+
4 rows in set (0.01 sec)
 
阅读(838) | 评论(0) | 转发(0) |
0

上一篇:没有了

下一篇:RHEL5.4下安装Oracle11gR2之安装环境

给主人留下些什么吧!~~