基本语法

基本语法

对数据库的操作

查看所有的数据库

1
show databases;

创建数据库

1
create database dbname;

删除数据库

1
drop database dbname;

对数据表的基本操作

创建表

1
2
3
4
create table (
columnname type ... ;
....
)

删除表

1
drop table tablename;

查看表的结构

1
desc tablename;

查看表的创建语法

1
show create table tablename;

查看所有表

1
show tables;

表结构

添加列

1
alter table tb_name  add [column]  name type ;

删除列

1
alter table tb_name drop [column] name;

添加索引

1
alter table tb_name add index (name(length) ads|desc);

更改列的类型

1
alter table tb_name modify column name type;

更改表名

1
alter table tb_name rename newatblename;

添加主键

1
alter table tb_name add primary key (name);

删除主键

1
alter table tb_name drop primary key

添加外键

1
alter table tb_name add foreign key(name) references othertbanme(key);

其他

创建视图

1
create view  v_name  as select ...;

创建存储函数

1
2
3
4
5
6
7
create function fname(name char(5)) 
returns char(10)
declare cursorname cursor for select_staement;
declare name int|varchar;
begin
return select "hello";
end

创建存储过程

1
2
3
4
create procedure panme(in name int)
begin
sql ;
end

创建触发器

1
2
3
4
5
6
create trigger tname 
after|before insert|update|delete
on tablename
begin
sql
end

用户

创建用户

1
create user "username"@"host" IDENTIFIED by "password";

删除用户

1
drop user "username"@"host";

权限

用户授权

1
grant all on *.* to "username"@"host" with grnat option;

撤销用户权限

1
revoke all on *.* from "username"@"host";