第三章 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 asked for help about help category: "Contents"
For more information, type 'help <item>', where <item> is one of the following
categories:
   Account Management
   Administration
   Compound Statements
   Data Definition
   Data Manipulation
   Data Types
   Functions
   Functions and Modifiers for Use with GROUP BY
   Geographic Features
   Help Metadata
   Language Structure
   Plugins
   Procedures
   Storage Engines
   Table Maintenance
   Transactions
   User-Defined Functions
   Utility

 

 

mysql> help data Definition;
You asked for help about help category: "Data Definition"
For more information, type 'help <item>', where <item> is one of the following
topics:
   ALTER DATABASE
   ALTER EVENT
   ALTER FUNCTION
   ALTER LOGFILE GROUP
   ALTER PROCEDURE
   ALTER SERVER
   ALTER TABLE
   ALTER TABLESPACE
   ALTER VIEW
   CONSTRAINT
   CREATE DATABASE
   CREATE EVENT
   CREATE FUNCTION
   CREATE INDEX
   CREATE LOGFILE GROUP
   CREATE PROCEDURE
   CREATE SERVER
   CREATE TABLE
   CREATE TABLESPACE
   CREATE TRIGGER
   CREATE VIEW
   DROP DATABASE
   DROP EVENT
   DROP FUNCTION
   DROP INDEX
   DROP PROCEDURE
   DROP SERVER
   DROP TABLE
   DROP TABLESPACE
   DROP TRIGGER
   DROP VIEW
   RENAME TABLE
   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 SET utf8 ;
create database kitty_server DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;
                
show create database llf;
drop database llf;
help  create 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 SET  charset_name COLLATE collation_name
alter database llf 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 * from t2;

-- 復制表結構
create table t3_new   select * from t3 where 1 =2;
create table t3_new  like t3;

 

 

 

5 刪除表以及表結構 (drop)

drop table t1;

 

 

6  修改表 (alter)

-- 修改表名
alter table  table_1  rename  t2_new;
rename table t1  to t1_new;

-- 刪除字段i
ALTER TABLE table_1  DROP i;   

-- 添加字段 i
ALTER TABLE table_1 ADD i INT;

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

-- 添加字段 設定位於c個字段之后
ALTER TABLE table_1 ADD i INT after 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 drop c;

-- 修改默認值為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 key   auto_increment;

-- 增加約束 (針對已有的主鍵增加 auto_increment)
alter table table_1 modify id int(11) not null   primary key  auto_increment;

-- 修改 id 自動增長
alter table table_1 modify id int(11) not null auto_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 key auto_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 from t1 ; 
delete from t1   where  id=2;

 

DDL  刪除表  truncate

truncate table t1;  ---在物理上刪除表數據,速度比較快。

 

 

  

五  DQL語句:數據查詢語言(show、select)

1. show

show tables;
show create table t1;
show create database llf;

 

 

2. 單表查詢

https://www.cnblogs.com/augustyang/p/11079174.html

 

3 多表查詢

https://www.cnblogs.com/augustyang/p/11456792.html

 

 

六 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.00 sec)
mysql> revoke all on *.* from  ysl@'%';
Query OK, 0 rows affected (0.00 sec)

 


 

 

七 數據類型

https://www.cnblogs.com/augustyang/p/11079102.html

 

八 完整性約束

https://www.cnblogs.com/augustyang/p/11079165.html

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM