mysql modify多个字段 mysql修改多个字段_mysql modify多个字段


1,连接mysql:


mysql


2,创建数据库:


create database 数据库名;


3,创建表:#前提是先进入数据库


use 数据库名;
create table 表名(列名1 varchar(),列名2 varchar(),...);


4,删除数据库:


drop database 数据库名;


5,删除表:


drop table 表名;


6,删除表数据:


delete from 表名;
truncate 表名;


7,显示全部数据库:


show databases;


8,显示某数据库中的表:


show tables;


9,快速查找某字段所在的表:


use information_schema;
select table_name from columns where column_name='字段名';


information_schema这张数据表保存了MySQL服务器所有数据库的信息,如数据库名,数据库的表,表栏的数据类型与访问权限等

10,显示表的详细信息:


(1):describe 表名;
(2):desc 表名;


11,显示当前MySQL版本和当前日期:


(1):select version(),current_date;
(2):select version(),now();


12,重命名表:


(1):alter table 旧表名 rename 新表名;
(2):rename table 旧表名 to 新表名;


13,复制表:


(1):create table 新建表 as select*from 被复制的表名;
(2):create table 表名1(select * from 表名2);#这条语句也可以将一个表的查询结果插入到一个新创建的表中


14, 将不同表中相同列中查询的数据展示出来(不包括重复数据):


select  列名 from 表名 union select 列名 from 表名 order by 列名;


将不同表中相同列中查询的数据展示出来(包括重复数据):


select 列名 from 表名 union all select 列名 from 表名 order by 列名;


15, 查询表中所有字段/列:


select * from 表名;


查询多个字段/列:


select 列名1,列名2,... from 表名;


16,将多个字符串连接成一个字符串,如果有任何一个参数为null,则返回值为null:


select concat(str1, str2,...) from 表名;


17,查询表前n行:


select * from 表名  limit 0, n;


18, 按倒序查找:


select * from 表名 order by 列名 desc;


按顺序查找:


select * from 表名 order by 列名 asc;


19, 增加表一个字段/列:


alter table 表名 add 列名 varchar();


增加表多个字段/列:


alter table 表名 add 列名1 varchar(255),add 列名2 varchar();


删除表一个字段/列:


alter table 表名字 drop column 列名;


删除表多个字段/列:


alter table 表名字 drop column 列名1,drop 列名2,drop 列名3;


20,使表某字段输入统一值:


update 表名 set 列名=统一值;


增加某字段/列并设置默认值:


alter table 表名 add 列名 varchar() default 默认值;


21,修改字段属性:


alter table 表名 modify column 列名 varchar() not null;


22,查询表的行数:


select count(*) from 表名字;


23,在表中插入一条数据:


insert into 表名 (列名1, 列名2,...) values (值1,值2,...,);


在表中批量插入数据:


insert into 表名 (列名1,列名2,...) values(...,...,...), (...,...,...), (...,...,...);


24,修改表中某行数据:


update 表名 set 列名1='',列名2='' where 列名3='';


25,直接创建索引:


create index 索引名 on 表名字(字段名/列名);


以修改表结构的方式添加索引:


alter table 表名字 add index 索引名(字段名/列名);


直接创建唯一索引:


create unque index 索引名 on 表名字(字段名/列名);


以修改表结构的方式添加唯一索引:


alter table 表名 add unque index 索引名(字段名/列名);


26,查看索引:


(1):show index from 表名;
(2):show keys from 表名;


27,删除索引:


drop index 索引名字 on 表名;


28,创建用户:


create user'用户名'@'localhost' identified by '密码';


授权用户:


grant all on test.*to'用户名'@'localhost';


刷新权限:


flush privileges;


取消授权:


revoke all on test.* from '用户名'@'localhost';


删除用户:


drop user'用户名'@'localhost';


29,开启事务:


set autocommit=0;


操作回滚:


rollback;


提交事务:


commit;