mysql常用增删改查

来源:互联网 发布:个人域名注册流程 编辑:程序博客网 时间:2024/05/05 01:40

修改数据库的字符集
    mysql>use mydb
    mysql>alter database mydb character set utf8;
创建数据库指定数据库的字符集
    mysql>create database mydb character set utf8;

查看database的字符集!

show variables like 'collation_%';show variables like 'character_set_%';一、系统操作

1. 打开服务:net start mysql(mysql为配置时,可自定名称)  

2.关闭服务:net stop mysql  

3.从cmd 模式进入mysql  

(1).mysql -u 用户名 -p 回车>输入正确密码>进入欢迎  

(2).mysql -h IP(本机localhost) -u 用户名 -p 回车>输入正确密码>进入欢迎  

3.退出:exit/quit;

4.修改用户密码:mysqladmin -u 用户名 -p password 新密码

5、增加一个管理员帐户:grant all on *.* to user@localhost identified by "password";  

二、增删改查语句
  1. 显示数据表字段:describe 表名
  2. 当前库数据表结构:show tables; 
  3. ALTER TABLE [表名] ADD COLUMN [字段名] DATATYPE  
  4. ALTER TABLE [表名] ADD PRIMARY KEY ([字段名])   说明:更改表得的定义把某个栏位设为主键。  
  5. 添加:insert into 表名(id,name) values(0,'脖子');   或者:INSERT INTO [id,name...表名] VALUES('','' 企鹅",......顺序排列的数据)
  6. 删除:DELETE FROM [表名] WHERE ([条件]);              删除表中的列:alter table 表名 drop column 列名;  
  7. 修改:UPDATE [表名] SET [修改内容如name = 'Mary'  列名='新的值,非数字加单引号'] WHERE [条件如:id=3];
  8. 数据传入命令 load data local infile "[文件名]" into table [表名];  
  9. 分页查询:select *from 表名 limit 每页数量 offset 偏移量;  
  10. create table 表名(id int auto_increment primary key,name varchar(20)) DEFAULT CHARSET=gbk
  11. 添加主外键:alter table 外表名  add constraint FK_名称 foreign key(外列) references 主表名(主列)  

    如现有两表 主表tbl_order 子表tbl_orderdetail 现子表tbl_orderdetail的oid列引用了主表tbl_order的oid列  则命令如下:  

  alter table tbl_orderdetail  add constraint FK_oid foreign key(oid) references tbl_order(oid)  ;

查询时间:select now();  

查询当前用户:select user();  

查询数据库版本:select version();  

查询当前使用的数据库:select database();  

三、操作指令

1、删除student_course数据库中的students数据表:  

rm -f student_course/students.*  

2、备份数据库:(将数据库test备份)  

mysqldump -u root -p test>c:\test.txt  

备份表格:(备份test数据库下的mytable表格)  

mysqldump -u root -p test mytable>c:\test.txt  

将备份数据导入到数据库:(导回test数据库)  

mysql -u root -p test  

//

MYSQL数据库导入导出

导入:mysql -uroot -ptian test<test.sql
导出:mysqldump -uroot -ptian test>test.sql

其中 -uroot 表示用户名

   -ptian  表示密码

    test    表示数据库名(已存在的)

    test.sql  表示外部的脚本文件(文件名字、格式随便,例如:a.sql,a.abc......)

 

3、创建临时表:(建立临时表zengchao)  

create temporary table zengchao(name varchar(10)); 

4、复制表: create table table2 select * from table1;  

5、对表重新命名  alter table table1 rename as table2;  

6、修改列的类型

alter table table1 modify id int unsigned;//修改列id的类型为int unsigned  

alter table table1 change id sid int unsigned;//修改列id的名字为sid,而且把属性修改为int unsigned  

7、创建索引  alter table table1 add index ind_id (id);  

8、联合字符或者多个列(将列id与":"和列name和"="连接)  

select concat(id,':',name,':',age)  as 学生年龄 from students;  

9、增加一个用户test2密码为abc,让他只可以在localhost上登录,并可以对数据库mydb进行查询、插入、修改、删除的操作

grant select,insert,update,delete on mydb.* to test2@localhost identified by \"abc\";     如果希望该用户能够在任何机器上登陆mysql,则将localhost改为"%"。

0 0
原创粉丝点击