当前位置:Gxlcms > 数据库问题 > mysql常用语句、命令(增删改查功能)

mysql常用语句、命令(增删改查功能)

时间:2021-07-01 10:21:17 帮助过:5人阅读

  • 当前库数据表结构:show tables; 
  • ALTER TABLE [表名] ADD COLUMN [字段名] DATATYPE  
  • ALTER TABLE [表名] ADD PRIMARY KEY ([字段名])   说明:更改表得的定义把某个栏位设为主键。  
  • 添加:INSERT INTO [id,name...表名] VALUES(‘‘,‘‘ 王乐",......顺序排列的数据);   或者:insert into 表名(id,name) values(0,‘尹当‘)
  • 删除:DELETE FROM [表名] WHERE ([条件]);              删除表中的列:alter table 表名 drop column 列名;  
  • 修改:UPDATE [表名] SET [修改内容如name = ‘Mary‘  列名=‘新的值,非数字加单引号] WHERE [条件如:id=3];
  • 数据传入命令 load data local infile "[文件名]" into table [表名];  
  • 分页查询:select *from 表名 limit 每页数量 offset 偏移量;  
  • create table 表名(id int auto_increment primary key,name varchar(20)) DEFAULT CHARSET=gbk
  • 添加主外键: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改为"%"。

    mysql常用语句、命令(增删改查功能)

    标签:tps   系统   alt   add   添加   _id   select   类型   body   

    人气教程排行