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)
七 數據類型
八 完整性約束
總結
以上是生活随笔為你收集整理的MySQL数据库https接口_第三章 mysql 数据库接口程序以及SQL语句操作的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: centos7 mysql读写监控_Ce
- 下一篇: mysql binlog 二进制_二进制