欢迎投稿

今日深度:

MySQL(MariaDB)常用SQL语句详解,mysqlmariadb

MySQL(MariaDB)常用SQL语句详解,mysqlmariadb


DDL(Data Definition Language)数据定义语言

这些语句定义了不同的数据段、
数据库、表、列、索引等数据库对象的定义。常用的语句关键字主要包括 create、drop、alter
等。

数据库管理

--删除数据库
drop database if exits bookDB;
--创建数据库
create database bookDB;
--修改数据库
alter database bookDB charset=utf8;

表的管理(主要是各种约束的管理)

--表的创建
create table bookInfo(
    book_id int auto_increment unique,              --自增长,唯一约束
    author_id int,
    book_name varchar(10) not null,                 --非空约束
    book_price decimal(10,2) check (book_price>0),  --检查约束(mysql中不支持检查约束,但是加上并不报错)
    book_shelf bit default 0,                       --默认约束
    primary key (book_id),                          --主键
    key fk_author (author_id),                      --外键详细写法
    constraint fk_author foreign key (author_id) references authorInfo(author_id)                                       
    --foreign key (author_id) references authorInfo (author_id)
    --主外键的建立也可以直接在字段上面添加,这种写法是为了方便管理
);
-- 拿到数据创建一个表格
create table bookInfo as select * from book_table;
--创建临时表
create temporary table if not exists book_table(....);
--删除表
drop table bookInfo;

--重命名
alter table bookInfo rename [to] book_info;

--修改表(列的管理)
--添加列
alter table bookInfo 
    add column book_press varchar(20);                  --column关键字可以省略
alter table bookInfo 
    add book_press varchar(20) after book_price;        --指定位置
alter table bookInfo 
    add (book_press varchar(20),book_date datetime);    --批量添加

--修改列类型
alter table bookInfo
    modify book_press varchar(200);
--修改列名(同时也可修改列的类型)
alter table bookInof
    change book_press bookPress varchar(25);

--删除列
alter table bookInfo
    drop column book_press;

--修改表(约束的管理)
--使用modify关键字可以更改数据类型,使用change关键字可以更改列名和数据类型
--添加约束
alter table bookInfo 
    add primary key (book_id);                          --添加主键
alter table bookInfo
    add modify book_id int primary key;                 --使用modify关键字           
alter table bookInfo
    add constraint fk_author foreign key (author_id) 
        references authorInfo(author_id)                --添加外键
alter table bookInfo
    add constraint unique (book_id);                    --添加唯一约束
alter table bookInfo
    modify book_shelf int default 0;                    --添加默认约束

 --删除约束
alter table bookInfo
    modify book_shelf int;                              --删除默认约束
alter table bookInfo
    change book_id book_id int;                         --去除auto_increment
alter table bookInfo
    drop primary key;                                   --删除主键(先删除自增长)
alter table bookInfo
    drop foreign key (fk_author);                       --删除外键

--设置自增长值
alter table bookInfo auto_increment=13;
--设置表的字节编码
alter table bookInfo character set='utf8';

DML(Data Manipulation Language)数据操作语言

主要用于添加、删除、更新和查
询数据库记录,并检查数据完整性,常用的语句关键字主要包括 insert、delete、udpate 和
select 等。

--插入数据
insert into bookInfo values(0,2,'head frist java',49.8,0);
--把一个表的书籍插入到另一个表中
insert into bookInfo select * from bookInfo;
--删除数据
delete from bookInfo;
--修改数据
update bookInfo set book_name='head first C#',book_press='南方出版社' where book_id=1;
--查询数据
select * from bookInfo where book_id=1;
--查询数据的各种条件的排序
select * from bookInfo 
    [where 条件]
        [group by 列名]
            [having 条件] 
                [order by 列名 desc|asc]
                    [limit 数量] 

DCL(Data Control Language)数据控制语言

用于控制不同数据段直接的许可和
访问级别的语句。这些语句定义了数据库、表、字段、用户的访问权限和安全级别。主要的
语句关键字包括 grant、revoke 等。

用户管理:

-- 查看用户
select current_user(), user();
select * from mysql.user;

--创建用户
-- 特别需要注意,在 MySQL 中,账号由两部分组成:
-- 1. user
-- 2. host
-- 即使 user 相同,只要 host 不同,也会被认为是不同账号。
-- 这样可以非常方便对来自不同 ip 地址的访问进行精细的权限控制。
-- 默认情况下,创建的用户 host 为 '%',这是一个匹配符,跟模糊查询里的意思一样,表示匹配所有
create user user_name identified by '密码';             -- 所有连接
create user user_name@'127.0.0.1' identified by '密码';   -- 本地连接
create user user_name@'192.168.%' identified by '密码';   -- 192.168 网段的连接


--修改用户密码
set password for '用户名'@'服务地址' = password('新密码');
--删除用户
drop user user_name;


-- 增加用户
insert into mysql.user(host, user, password) values (xx, yy, zz);

-- 修改密码
update mysql.user set password=password('新密码') where user='user_name' and host='%';

-- 修改权限
update mysql.user set event_priv='Y' where user='user_name' and host='%';

-- 注意,使用 sql 语句修改用户跟权限之后,需要手动刷新权限表
flush privileges;

权限管理

--授予权限
grant all on *.* to user_name@'127.0.0.1';        -- 将所有数据库上的所有权利都授予通过本机连接的用户!
grant all privileges on database_name.* to user_name@'%'; -- 将数据库  上的所有权利都授予所有连接的 用户!
grant select on database_name.table_name to user_name@'%';  -- 将数据库上的 表数据表的访问权限开放给所有用户。

--查看用户权限
show grants for user_name@'%';

-- 授权的相对完整语法为:
grant all/alter/create/drop/select/update/delete
  on *.* -- db.*/db.table
  to 'user'@'host'
  identified by '密码'
  with max_user_connections 2
       max_connections_pser_hour 5;

www.htsjk.Com true http://www.htsjk.com/mariadb/36581.html NewsArticle MySQL(MariaDB)常用SQL语句详解,mysqlmariadb DDL(Data Definition Language)数据定义语言 这些语句定义了不同的数据段、 数据库、表、列、索引等数据库对象的定义。常用的语句关键字主要包括...
相关文章
    暂无相关文章
评论暂时关闭