mysql命令集合

修改mysql密码
alter user 用户名@”localhost” identified by "新密码";
mysql> alter user  root@”localhost” identified by "123qqq…A";
查看student表的实际创建指令
show create table 库.;
mysql> show create table mydb.student;
查看现有的库
mysql> show  databases;
切换到sys库
use 库;
mysql> use sys;
确认当前所在的库
mysql> select database();
新建名为newdb的库
create database 库;
mysql> create database newdb;
新建表格
create table 表名(字段名 类型(长度));
mysql> create table pwlist(name char(16) not null);
查看表结构
desc 库名.表名;
mysql> desc pwlist;
删除表
drop table 库名.表名;
mysql> drop table pwlist;
删除名为newdb的库
drop database 库名;
mysql> drop database newdb;
创建t2表时设置字段约束条件
create table t2(字段名 类型 约束条件);
mysql> create table  t2 ( class char(9),name char(10) not null,age tinyint not null default  19 , likes set("a","b","c","d") default "a,b" );
添加address字段
alter table 库名.表名 add 要添加的字段名 类型;
mysql> alter table tea6 add address varchar(48);
在tea6表的age列之后添加一个gender字段
alter table 库名.表名 add 要添加的字段名 类型 after 要在哪个列后面添加;
mysql> alter table tea6 add gender enum('boy','girl') after age;
将tea6表的gender字段改名为sex,并添加非空约束
alter table 库名.表名 change 原字段名 改字段名 类型 约束条件(可以不添加);
mysql> alter table tea6 change gender sex enum('boy','girl') not null;
删除tea6表中名为sex的字段
alter table 库名.表名 drop 要删除的字段
mysql> alter table tea6 drop sex;
创建tea4表,将其中的id、name作为索引字段
mysql> crente table tea4( id char(6) not null, name varchar(6) not null, age int(3) not null, gender enum('boy','girl') default 'boy', index(id),index(name) );
删除tea4表中名称为named的index索引字段
mysql> drop index name on tea4;
针对tea4表的age字段建立索引,名称为 nianling
mysql> create index nianling on tea4(age);
建表时设置PRIMARY KEY主键索引
mysql> CREATE TABLE db2.tea6( id int(4) AUTO_INCREMENT, name varchar(4) NOT NULL, age int(2) NOT NULL, PRIMARY KEY(id) );
删除现有表的PRIMARY KEY主键索引
alter table db2.biao01 drop primary key;
重新为tea6表指定主键字段,仍然使用id列
mysql> alter table tea6 add primary key(id);
建表时创建复合主键
mysql> create table  db2.t6( class char(7), name  char(15), pay enum("yes","no") default "no", primary key(class,name,pay) );
创建外键
mysql> create table yg( yg_id int primary key auto_increment, name char(16) )engine=innodb;
----------------------------------------------------------------------------------------------------------------------
mysql> create table gz( gz_id  int, name char(16) , gz float(7,2) , foreign key(gz_id) references yg(yg_id)  on update cascade on delete cascade )engine=innodb;
删除外键
mysql> alter table gz drop foreign key gz_ibfk_1;
使用now()查看当前的日期和时间
mysql> select now();
使用curdate()获得当前的日期
mysql> select curdate();
使用curtime()获得当前的时间
mysql> select curtime();
分别获取当前日期时间中的年份、月份、日
mysql> select year(now()) , month(now()) , day(now());
导入数据
mysql> load data infile "目录名/文件名" into table 库名.表名 fields terminated by "分隔符" lines terminated by "\n" ;
mysql> load data infile "/myload/passwd" into table db3.user fields terminated by ":" lines terminated by "\n" ;
导出数据
mysql> elect  * from  库名.表名  into outfile "/目录名/文件名";
mysql> elect  * from  db3.user  into outfile "/myload/user1.txt";
插入部分字段的值
mysql> insert into 表名(字段名) values(字段值);
mysql> insert into stu_info(name,age) values('Jerry',27);
将stu_info表中所有性别为“boy”的记录的age设置为20
mysql> update 表名 set 字段名=字段值 where 字段名=字段值;
mysql> update stu_info set age=10 where gender='boy';
删除stu_info表中年龄小于18的记录
mysql> delete from 表名 where 字段名 < 字段值;
mysql> delete from stu_info where age < 18;
计算stu_info表中各的平均年龄、最大年龄、最小年龄
mysql> select avg(字段名),max(字段名),min(字段名) from 表名;
mysql> select avg(age),max(age),min(age) from stu_info;
计算stu_info表中男的个数
mysql> select count(字段名) from 表名 where 字段名=字段值;
mysql> select count(gender) from stu_info WHERE gender='boy';
列出stu_info表中年龄为21岁的记录
mysql> select * from 表名 where 字段名=比较值;
mysql> select * from stu_info where age=21;
列出stu_info表中年龄超过21岁的记录
mysql> select * from 表名 where 字段名>比较值;
mysql> select * from stu_info where age>21;
列出stu_info表中年龄超小于21岁的记录
mysql> select * from 表名 where 字段名<比较值;
mysql> select * from stu_info where age<21;
列出stu_info表中年龄超小于或等于21岁的记录
mysql> select * from 表名 where 字段名<=比较值;
mysql> select * from stu_info where age<=21;
列出stu_info表中年龄小于23岁的女学员记录
mysql> select * from 表名 where 字段名 < 比较值 and 字段名=比较值;
mysql> select * from stu_info where age < 23 and gender='girl';
列出stu_info表中姓名以“J”开头的学员记录
mysql> select * from 表名 where 字段名 like 匹配字符;
mysql> select * from stu_info where name like 'J%';
用正则的方式列出stu_info表中姓名以“J”开头且以“y”结尾的学员记录
mysql> select * from stu_info where name regexp '^J.*y$';
列出stu_info表的所有记录,按年龄排序(升序)
mysql> select * from 表名 order by 字段名;
mysql> select * from stu_info order by age;
列出stu_info表的所有记录,按年龄排序(降序)
mysql> select * from stu_info order by age desc;
查询stu_info表的所有记录,只列出前3条
mysql> SELECT * FROM stu_info LIMIT 3;
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值