MySQL数据库https接口_第三章 mysql 数据库接口程序以及SQL语句操作

mysql  数据库接口程序以及SQL语句操作

用于管理数据库:

命令接口自带命令

DDL:数据定义语言(create drop )

DCL: 数据控制语言(grant revoke)

DML: 数据操作语言(update delete insert)

一 . mysql接口程序:

① mysql -uroot -poldboy123 -e "show variables like '%server_id%'"

② mysql>

1. 接口自带的功能

① \h 或help 或 ?

help contents;

② \G

mysql> select user,host,password from mysql.user\G;

③\T  或 tee  记录操作日志

④ \c   等于linux ctrl +c

⑤ \s 或 status

⑥ \. 或 source    执行外部SQL脚本:二进制日志截取、备份出来的SQL脚本

2.查看mysql 命令帮助

mysql>help contents;

You askedforhelp about help category: "Contents"For more information, type 'help ', where is one ofthe following

categories:

Account Management

Administration

Compound Statements

Data Definition

Data Manipulation

Data Types

Functions

Functionsand Modifiers for Use with GROUP BYGeographic Features

Help Metadata

Language Structure

Plugins

Procedures

Storage EnginesTableMaintenance

TransactionsUser-Defined Functions

Utility

mysql>help data Definition;

You askedforhelp about help category: "Data Definition"For more information, type 'help ', where is one ofthe following

topics:ALTER DATABASE

ALTEREVENTALTER FUNCTION

ALTER LOGFILE GROUP

ALTER PROCEDURE

ALTERSERVERALTER TABLE

ALTERTABLESPACEALTER VIEW

CONSTRAINT

CREATE DATABASE

CREATEEVENTCREATE FUNCTION

CREATE INDEX

CREATE LOGFILE GROUP

CREATE PROCEDURE

CREATESERVERCREATE TABLE

CREATETABLESPACECREATE TRIGGER

CREATE VIEW

DROP DATABASE

DROPEVENTDROP FUNCTION

DROP INDEX

DROP PROCEDURE

DROPSERVERDROP TABLE

DROPTABLESPACEDROP TRIGGER

DROP VIEWRENAMETABLE

TRUNCATE TABLE

二 服务端命令

1 SQL:结构化的查询语言,mysql接口程序只负责接收SQL,传送给SQL层

2 SQL种类

DDL:数据库(对象)定义语言 (create drop  alter )

DCL:数据库控制语言(grant revoke)

DML:数据(行)操作语言(update delete insert)

DQL: 数据查询语言(show、select)

三 DDL语句:数据定义语言 (create drop )

DDL 操作对象分为        库   表

1 库 的定义

定义了: ① 库名   ②库的基本属性

2. 定义库 定义字符集

show databases;create database test CHARACTER SETutf8 ;create database kitty_server DEFAULT CHARACTER SETutf8mb4 COLLATE utf8mb4_general_ci;

showcreate databasellf;drop databasellf;

helpcreate database;

字符集: CHARACTER SET [=] charset_name

排序规则: COLLATE [=] collation_name

mysql> show variables like 'character_set%';+--------------------------+------------------------------------+

| Variable_name | Value |

+--------------------------+------------------------------------+

| character_set_client | utf8 |

| character_set_connection | utf8 |

| character_set_database | utf8mb4 |

| character_set_filesystem | binary |

| character_set_results | utf8 |

| character_set_server | utf8 |

| character_set_system | utf8 |

| character_sets_dir | /data/mysql-5.6.42/share/charsets/ |

+--------------------------+------------------------------------+

字符集:

① 服务器字符集

控制的是,存到mysql中时,字符集控制

② 客户端字符集

控制的是用户的输入及显示

③ 系统字符集

控制的是系统相关的显示,和一些依赖于操作系统的应用

3  修改库的字符集

ALTER DATABASE [db_name] CHARACTER SETcharset_name COLLATE collation_namealter databasellf charset utf8mb4 COLLATE utf8mb4_general_ci;show create database llf;

4 创建表 (create)

create table t1 (id int ,name varchar(20));

表数据:数据行

表属性(元数据):表名、列名字、列定义(数据类型、约束、特殊列属性)、表的索引信息

4-1 复制表

--复制表结构+记录

create table t2_new select * fromt2;--复制表结构

create table t3_new select * from t3 where 1 =2;create table t3_new like t3;

5 删除表以及表结构 (drop)

drop table t1;

6  修改表 (alter)

--修改表名

alter tabletable_1 rename t2_new;

renametable t1 tot1_new;--删除字段i

ALTER TABLE table_1 DROPi;--添加字段 i

ALTER TABLE table_1 ADD i INT;--添加字段 i 设定位第一列

ALTER TABLE table_1 ADD i INTFIRST;--添加字段 设定位于c个字段之后

ALTER TABLE table_1 ADD i INTafter c;--修改字段类型

ALTER TABLE table_1 MODIFY c CHAR(10);--change 修改字段名

ALTER TABLE table_1 CHANGE c c_new CHAR(10);--change也可以 修改字段类型

ALTER TABLE table_1 CHANGE c b varchar(10);--删除列

alter table table_1 dropc;--修改默认值为100

ALTER TABLE table_1 ALTER i SET DEFAULT 1000;--删除默认值

ALTER TABLE table_1 ALTER i DROP DEFAULT;--修改 id为主键

ALTER TABLE table_1 modify id int(11) not null primary keyauto_increment;--增加约束 (针对已有的主键增加 auto_increment)

alter table table_1 modify id int(11) not null primary keyauto_increment;--修改 id 自动增长

alter table table_1 modify id int(11) not nullauto_increment;--对存在的表增加复合主键

alter table table_1 add primary key(host_ip, port);--增加主键

alter table table_1 modify name varchar(10) not null primary key;--增加主键和自动增长

alter table table_1 modify id int not null primary keyauto_increment;--删除自增约束

alter table table_1 modify id int(11) not null;--删除主键

alter table table_1 drop primary key;

四 DML语句: 数据操作语言 (insert update delete)

1. insert

insert into t1 values (2,'li4'),(3,'wang5'),(4,'ma6');insert into t1(name) values ('xyz');

2. update

----会更新表中所有行的name字段,比较危险。

update t1 set name='zhang33';update t1 set name='zhang55' where id=1;

3. delete

--删除表中所有行,比较危险。一行一行删除表中数据。

delete fromt1 ;delete from t1 where id=2;

DDL  删除表  truncate

truncate table t1; ---在物理上删除表数据,速度比较快。

五  DQL语句:数据查询语言(show、select)

1. show

show tables;

showcreate tablet1;

showcreate database llf;

2. 单表查询

3 多表查询

六 DCL语句: 数据控制语言(grant revoke)

1.grant

grant all on ysl.* to test@'10.0.0.%' identified by '123456';--权限 权限范围 用户 范文 密码

grant SELECT,INSERT, UPDATE, DELETE, CREATE, DROP on testdb.* to zabbix@'10.0.0.%';--创建用户并授权

grant all on *.* to root@'10.0.0.%' identified by '123456';

2 revoke

mysql> revoke create,drop on *.* from ysl@'%';

Query OK,0 rows affected (0.00sec)

mysql> revoke all on *.* from ysl@'%';

Query OK,0 rows affected (0.00 sec)

七 数据类型

八 完整性约束

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值