mysqladmin -u root -p123456 password 518 //设定用户密码 如果root没密码,则-p123456一项可以省略。
mysql –u root -p518 //登陆mysql服务器,-p与518不能空格,如果空格了,
//还是会提示输入密码,一般安全的密码输入方式为:mysql –u root -p(不输入密码) ,
//回车键之后会提示输入密码
exit/quit //退出mysql
//退出mysql后,可以使用向上方向键和向下方向键来翻滚历史命令,还可使用F7来查看历史命令
show databases; //查看服务器内的数据库
show tables;//显示所有的表
drop table customer;//删除customer表
drop database home;//删除home数据库
mysql -u root -p123456
create database home;//创建home数据库
grant all privileges on home.* to user@localhost identified by '123456';
//创建user用户,密码123456,并赋予user对home数据库的权限
//user用户只能对home数据库进行操作。
mysql -u user -p123456 //使用user用户登录数据库
use home;//使用home数据库
select database();//查看现在使用的数据库
create table customer(id CHAR(5) PRIMARY KEY,name VARCHAR(20),birth DATE,sex
CHAR(1) DEFAULT '0') CHARSET=utf8;
//创建customer表
//创建表的格式为create table 表名 (域名 数据类型 列选项[],....);
desc customer;//显示表的结构
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id | char(5) | NO | PRI | NULL | |
| name | varchar(20) | YES | | NULL | |
| birth | date | YES | | NULL | |
| sex | char(1) | YES | | 0 | |
+-------+-------------+------+-----+---------+-------+
一:数据库的插入
//插入之前执行set names gbk;否则插入不了中文
insert into customer(id,name,birth,sex) values('N0001','杜意意','1975-04-18',0);
insert into customer(id,name,birth,sex) values('N0002','李玉',NULL,1);
insert into customer(id,name,birth,sex) values('N0003','李加',NULL,0);
insert into customer(id,name,birth,sex) values('N0004','小小','1980-11-23',1);
//插入数据,如果插入的数据涵盖了所有的列,则(id,name,birth,sex)可以省略。
//当值1为字符串、日期的情况下。必须用单引号''括起来;
//比如(id,name,birth,sex)和('N0004','小小','1980-11-23',1)必须是一一对应的关系
insert into customer(id,name,birth,sex) values('N0005','桂枝枝',NULL,1);
select id,name,birth,sex from customer;
//显示表中的数据,如果要显示所有的列时,直接select * from customer;
+-------+--------+------------+------+
| id | name | birth | sex |
+-------+--------+------------+------+
| N0001 | 杜意意 | 1975-04-18 | 0 |
| N0002 | 李玉 | NULL | 1 |
| N0003 | 李加 | NULL | 0 |
| N0004 | 小小 | 1980-11-23 | 1 |
| N0005 | 桂枝枝 | NULL | 1 |
+-------+--------+------------+------+
二:更新已经存在的记录
update customer set name='李玉枝',birth='1980-09-09' where id='N0002';
//将id ='N0002'的那条记录的姓名更新为[李玉枝],生日更新为[1980-09-09]
//如果省略where,则代表对所有的记录进行更新
三:删除记录
delete from customer where id = 'N0005';
//将id='N0005'那条记录进行删除
//如果省略where,则代表删除表中所有的所有数据;
//但是,在删除表中所有数据时,有一个速度更快的命令truncate,
//如truncate table customer;//删除整个customer表数据
四:数据检索(内容最多)
1、检索指定列名
select id,name from customer;
2、条件检索
select name,birth from customer where birth>='1980/01/01';
比较运算符
+------------+------------------+--------------------------------+
| 运算符 | 说明 | 举例 |
+------------+------------------+--------------------------------+
| = | 相等 | name ='小小' |
| > | 大于 | birth >'1980-01-01' |
| < | 大于 | birth >'1980-01-01' |
| >= | 大于等于 | birth >'1980-01-01' |
| <= | 小于等于 | birth >'1980-01-01' |
| <> | 不相等 | name <>'小小' |
|is[NOT]NULL | 为 NULL | name is NULL |
|[NOT]LIKE | 匹配 | name LIKE '小%' |
|[NOT]BETWEEN| 包含在指定范围内 | price BETWEEN 3000 AND 4000 |
|[NOT]IN | 包含在指定值内 | id IN ('N0001','N0002','N0003')|
+------------+------------------+--------------------------------+
3、模糊检索
select name from customer where name LIKE '李%';
+---------+
| name |
+---------+
| 李玉枝 |
| 李加 |
+---------+
%代表0个以上的字符。
[李%]表示以[李]开头的所有字符串,[%李]表示以[李]为结尾的所有字符串,
[%李%]表示所有含有[李]的字符串。
_(下划线)代表一个字符,如果是[李_]的情况下,[李加]是符合条件的,而[李玉枝]不符合条件。
4、NULL条件
检索NULL时,不能使用= NULL 运算符,要使用is NULL
select name,birth from customer where birth is NULL;
+------+-------+
| name | birth |
+------+-------+
| 李加 | NULL |
+------+-------+
5、多个条件表达式的组合
select name,birth,sex from customer where (birth<='1976-01-01' OR birth>='1980-01-01')
AND sex='1';
+--------+------------+------+
| name | birth | sex |
+--------+------------+------+
| 李玉枝 | 1980-09-09 | 1 |
| 小小 | 1980-11-23 | 1 |
+--------+------------+------+
运算符的优先顺序为 NOT->AND->OR,故(birth<='1976-01-01' OR birth>='1980-01-01')需要括号
6、排序检索结果
使用ORDER BY语句进行排序。下面按照性别升序,且按照生日降序对检索结果进行排序。
select name,birth,sex from customer ORDER BY sex ASC,birth DESC;
+--------+------------+------+
| name | birth | sex |
+--------+------------+------+
| 杜意意 | 1975-04-18 | 0 |
| 李加 | NULL | 0 |
| 小小 | 1980-11-23 | 1 |
| 李玉枝 | 1980-09-09 | 1 |
+--------+------------+------+
ORDEY BY跟多个列名时,列名之间以逗号隔开;
ASC表示升序,DESC表示降序,排序方式被省略时看作ASC。
7、从开始位置依次往后取出指定件数-LIMIT
比如LIMIT 0,2 表示取出id =N0001,id =N0002 两条记录
LIMIT 开始位置,件数
开始位置默认从0开始的,所以LIMIT 2 也表示取出id =N0001,id =N0002 两条记录
原则上LIMIT语句要与ORDER BY一起使用,而且ORDER BY要在LIMIT之前。如果没有明确指定排序方式,数据会按照随机的
顺序取出。
select name,birth from customer ORDER BY birth DESC LIMIT 0,2;
+--------+------------+
| name | birth |
+--------+------------+
| 小小 | 1980-11-23 |
| 李玉枝 | 1980-09-09 |
+--------+------------+
8、数据分组
使用GROUP BY语句,这语句通常与统计函数一起使用
主要统计函数
+------------+---------------+
| 函数 | 说明 |
+------------+---------------+
| AVG(列名) | 平均值 |
| COUNT(列名)| 件数 |
| MAX(列名) | 最大值 |
| MIN(列名) | 最小值 |
| SUM(列名) | 合计值 |
+------------+---------------+
select sex,count(id) from customer group by sex;
+------+-----------+
| sex | count(id) |
+------+-----------+
| 0 | 2 |
| 1 | 2 |
+------+-----------+
count函数是统计非NULL的记录件数,所以在统计件数时不要选择含有NULL值的列
9、列的别名
使用AS语句指定别名
select sex,count(id) AS cnt from customer group by sex;
+------+-----+
| sex | cnt |
+------+-----+
| 0 | 2 |
| 1 | 2 |
+------+-----+
多个表的连接
order_basic表
+--------+------------+-------+---------+
| oid | odate | uid | address |
+--------+------------+-------+---------+
| D00001 | 2010-07-24 | u0001 | 上海 |
| D00002 | 2010-07-24 | u0002 | 上海 |
| D00003 | 2010-07-24 | u0001 | 上海 |
| D00004 | 2010-07-24 | u0001 | 上海 |
| D00010 | 2010-07-26 | u0099 | NULL |
+--------+------------+-------+---------+
user表
+-------+--------+-------------------+--------+
| uid | zip | address | name |
+-------+--------+-------------------+--------+
| u0001 | 210021 | 上海市古北区112弄 | 李小明 |
| u0002 | 210041 | 上海市黄浦区123弄 | 牛二 |
| u0003 | 210001 | 上海市徐汇区 | 王五 |
+-------+--------+-------------------+--------+
1、内连接(取出左右两表能一一对应的数据)
select order_basic.oid,user.name from order_basic
INNER JOIN user ON order_basic.uid = user.uid;
+--------+--------+
| oid | name |
+--------+--------+
| D00001 | 李小明 |
| D00002 | 牛二 |
| D00003 | 李小明 |
| D00004 | 李小明 |
+--------+--------+
2、左外连接(以左表为基准,左表的数据全部显示)
select order_basic.oid,user.name from order_basic
LEFT OUTER JOIN user ON order_basic.uid = user.uid;
+--------+--------+
| oid | name |
+--------+--------+
| D00001 | 李小明 |
| D00002 | 牛二 |
| D00003 | 李小明 |
| D00004 | 李小明 |
| D00010 | NULL |
+--------+--------+
3、右外连接(以右表为基准,右表的数据全部显示)
select order_basic.oid,user.name from order_basic
RIGHT OUTER JOIN user ON order_basic.uid = user.uid;
+--------+--------+
| oid | name |
+--------+--------+
| D00001 | 李小明 |
| D00003 | 李小明 |
| D00004 | 李小明 |
| D00002 | 牛二 |
| NULL | 王五 |
+--------+--------+
导出home数据库
C:\Windows\System32> mysqldump -u root -p123456 home > home.sql //导出的文件名要与数据库名一样
生成的home.sql在C:\Windows\System32中
导入home数据库
mysql -u root -p123456
// create database home; //数据库名字要与导入的文件名一样
// use home;
source C:/Users/John/Desktop/home.sql; //导入home.sql数据库的所有表,会自动建立home数据库
home.sql文件下载地址为:http://download.csdn.net/detail/bladeandmaster88/9700364
单行注释以--开头,多行注释用/* */
参考资料 mysql高效编程一书
转载请注明原文地址: https://ju.6miu.com/read-965395.html