MySQL操作数据库基本操作

来源:互联网 发布:14岁谈恋爱软件 编辑:程序博客网 时间:2024/05/19 19:55

MySQL操作数据库基本操作

mysql -uroot -p     进入mysql
mysql -uhdw -p    进入hdw用户mysql
net stop wampmysqld    停止服务
net start wampmysqld    启动服务
mysqladmin -uroot -p shutdown   关闭mysqladmin
\c  取消所有命令    Esc  取消本行命令

(1)数据库操作
create database think character set utf8    创建数据库think,设置编码为utf8
show databases   查看数据库
use think   选择数据库think
drop database think  删除数据库think
grant all on wish.* to "hdw" @"localhost" identified by "hdw"  只允许hdw用户操作数据库wish,密码是hdw

C://mysqldump -uroot -p houdunwang > d:/houdunwang.sql     将数据导出d:/houdunwang.sql 
C://mysql -uroot -p houdunwang < d:/houdunwang.sql          将数据d:/houdunwang.sql 导入
mysql>source d:/houdunwang.sql            将数据d:/houdunwang.sql 导入

(2)对数据库中表的操作

create table hd_wish(id int unsigned not null primary key auto_increment, username varchar(20) not null default '',content
                                  varchar(255) not null default '',time int(10) not null default 0) engine myisam charset utf8;
创建表hd_wish(primary key 定义这个字段为主键,auto_increment 定义这个字段为自动增长,引擎为myisam,字符集为utf8

insert into hd_user set username='admin'   向表hd_user中插入数据(非标准版
insert into hd_user(username) values('admin',)   向表hd_user中插入数据(标准版

show tables  查看表名
desc hd_wish   查看表hd_wish结构
drop table hd_wish   删除表hd_wish
select *from hd_wish    查看表hd_wish中的所有数据
delete from hd_wish   将表hd_wish中的记录清空
delect from hd_wish where username="admin"  可用 WHERE 子句来选择所要删除的记录
select count(*) from hd_wish  统计表hd_wish中有多少条记录
select *from stu where sname like "李%"     模糊匹配
select if(sex "男生","女生") as stusex,sname from stu    as取别名(stusex)
select concat("姓名:",sname,"性别:",sex,"QQ",qq) as stuinfo from stu   字符串与字段进行连接
alter table stu add birday date              增加表stu字段
update stu set birday="1990/2/23" where id=1  修改表stu数据
select *from stu order by id desc limit 0,1    从第零个开始取1个
select distinct year(birday) as "出生年份" from stu    distinct去掉重复的部分,year(birday)只得到年份

0 0
原创粉丝点击