数据库之常用sql语句

#查看当前登录的用户
select user();
#创建数据库
create database 库名;
#查看有哪些数据库
show databases
#查看已建库完整语句
show create database 库;
#创建用户,使之可以管理数据库。
方法一、
create user yh@'localhost' identified by '123456';
grant all on yh.* to yh@'localhost';
方法二、
grant all on yh.* to yh@'localhost' identified by 123456;
#查看创建的用户拥有哪些权限。
show grants for yh@'localhost';
#查看当前数据库里有哪些用户。
select user,host from mysql.user;
#进入数据库
use yh
#查看当前所在的数据库。
select database();
#创建一张表test,字段id和name varchar(16)。
create table test( id int(4) not null , name varchar(16) not null);
#查看建表结构及表结构的SQL语句。
desc test;
show full columns from test;
#查看表内有多少条数据
select count(*) from user;
#插入一条数据
insert into test(id,name) values(1,'yh');
select * from test;
#再批量插入2行数据
insert into test(id,name) values(2,'yy'),(3,'hh');
select * from test;
#查询名字为yh的记录。
select * from test where name='yh';
#把数据id等于1的名字yh更改为yyhh。
update test set name='yyhh' where id=1;
select * from test;
#在字段name前插入age字段,类型tinyint(2)。
alter table test add age tinyint(2) after id;
desc test;
#删除test表中的所有数据
delete  from test;
truncate test;
#删除表test
show tables ;
drop table test;
#在字段name后插入手机号字段(shouji),类型char(11)。
alter table test add shouji char(11) after name;
desc test;
#所有字段上插入2条记录(自行设定数据)
insert into test(id,name,shouji) values(1,'aige','1322222222'),(2,'yh','1878888888');
insert into test(id,name,shouji) values(3,'jh','135555555');
select * from test;
#查询手机号以187开头的,名字为yh的记录(提前插入)。
select * from test  where shouji like '187%' and name like 'yh';
#收回yh用户的select权限
revoke select on yh.* from yh@'localhost';
#删除yh用户。
drop user yh@'localhost';
#删除oldboy数据库。
drop database yh;
posted @ 2018-06-01 13:49  杨灏  阅读(339)  评论(0)    收藏  举报