MySQL基础操作1
1.进入MySQL的两种方式:
(1).MySQL自带的控制台
直接输入密码
(2).命令提示符:
mysql -uroot -proot
然后再输入密码
1 MySQL常用指令 2 ------- 3 1.启动mysql 命令行 4 c:/>mysql -h hostname -u root -p root //连接远程服务器 5 c:/>mysql -u root -p root //连接localhost 6 c:/>mysql -uroot -proot //直接登录到本机 7 8 2.查看mysql的帮助 9 c:/>mysql --help 10 11 3.查询当前日期和时间 12 mysql>select current_date ; //伪劣 13 mysql>select now() ; //函数 14 4.显示所有数据库 15 mysql>-- 这是注释 16 mysql>show databases; 17 18 5.删除数据库 19 mysql>drop database myhive ; 20 21 6.创建数据库 22 mysql>create database mybase ; 23 24 7.使用指定的数据库 25 mysql>use mybase ; 26 27 8.显示所有表 28 mysql>show tables; 29 30 9.创建表 31 mysql>create table test(id int,name varchar(20),age int); 32 33 10.查看表结构 34 mysql>describe test ; // 35 mysql>desc test ; // 36 37 11.删除表 38 mysql>drop table test ; 39 40 12.查询表数据 41 mysql>select * from test ; //全字段 + 全表扫描 42 mysql>select id,name,age from test ; //投影查询 project 43 mysql>select id,name,age from test where id > 3 ; // 44 mysql>select id,name,age from test where id > 3 and id < 5; // 类似于 java && 45 mysql>select id,name,age from test where name = 'tom'; // 46 47 mysql>select id,name,age from test where name like 't%'; //模糊查询 48 mysql>select id,name,age from test where name not like 't%'; //模糊查询 49 mysql>select id,name,age from test where name not like 't\_%'; //使用转义符查询特殊字面量 50 51 mysql>select id,name,age from test where name is null ; //null查询 52 mysql>select id,name,age from test where name is not null ; //非null查询 53 54 mysql>select id,name,age from test order by id desc,name ; //降序排序 55 mysql>select id,name,age from test order by id asc ; //降序排序 56 57 mysql>select id,name,age from test limit 1,3 ; //从第二条 ,查3条 58 mysql>select id,name,age from test limit 1 ; //0,1 59 60 61 62 [聚集函数查询] 63 mysql>select count(*) from test ; //count,查询记录总数 64 mysql>select max(age) from test ; //最大值 65 mysql>select min(age) from test ; //最小值 66 mysql>select avg(age) from test ; //平均值 67 mysql>select sum(age) from test ; //求总和 68 69 70 71 72 73 13.插入记录 74 mysql>insert into test(id,name,age) values(1,'tom',23); 75 mysql>insert into test(id,name) values(4,'tomson'); 76 mysql>insert into test values(4,'tomson',13); 77 78 14.更新记录 79 mysql>update test set name='xxx' , age = 33 where id = 112 ; //更新id为112的记录 80 mysql>update test set name='xxx' , age = 33; //更新所有记录 81 82 83 15.删除记录 84 mysql>delete from test where id = 1 ; 85 86 16.使用mysql命令行执行sql脚本 87 mysql>source d:/java/findtest.sql 88 89 17. 90 91 CRUD 92 ------- 93 [create] 94 insert into table_name(field_name,...) values(value,...) ; 95 96 [retrieve] 97 select id,.. from table_name where id= xxx, ... ; 98 99 [update] 100 update table_name set id=xxx,... where id = xxx , .. ; 101 102 [delete] 103 delete from test where ... ;