標籤:建立 插入資料 nod 指定 int 建立資料庫 用戶端串連 基本 劃線
mysql mariadb
用戶端串連
mysql -uroot -p;
用戶端退出
exit 或 \q
顯示所有資料庫
show databases;
show schemas;
建立資料庫
create database db1
charset utf8;
刪除資料庫
drop database db1;
drop database if exists db1;
查看錶
show tables;
desc tb1;-------查看tb1的表結構
show create table tb1\G; -- 查看建表語句
表的增刪改查
建立
create table tb1(
id int primary key auto_increment,
name varchar(20) not null,
num int not null unique,
xid int,
foreign key(xid) references tb2(id),
)engine=innodb charset=utf8;
修改
alter table tb1 add gender char(1) after name;
alter table tb1 modify num int null;
alter table tb1 modify id int;
alter table tb1 modify id int auto_increment;
alter table tb1 drop primary key;
alter table tb1 drop foreign key(外鍵約束名);
alter table tb1 drop index 約束名;
alter table tb1 modify id int;
刪除表
drop table if exists tb1;
約束
主健 非空 唯一 外健 檢查
預設值
num int default 1;
mysql 的一個sql_mod變數
linux 中安裝mysql,sql_mod變數預設是空值,表示運行在‘不嚴格’模式,非空欄位會插入,
字元段超長會被截斷
。。。。。。。。。。。。。。。。。。
可以修改這個變數使mysql運行在strict 模式
-- 查看 sql_mode 變數的值
show variables like ‘sql_mode‘;
-- 修改
set global sql_mode=‘STRICT_TRANS_TABLES‘;
------退出,重新進入mysql,再查看變數
show variables like ‘sql_mode‘;
* * structured query language
結構化查詢語言 (SQL)
* sql標準文法
*各資料庫廠商都有自己的擴充文法
*)mysql 擴充
*)oracle plsql
*)sql server t-sql
*sql分類
*)DDL -----定義語言,建庫建表修改表
*)DML -----資料操作語言,增刪改
*)DQL ----- 資料查詢語言,select
插入資料insert
*insert into tb1 values(5,‘abc‘);
全部欄位按欄位順序插入值
* * insert into tb1(gender, name) values(‘M‘, ‘張三‘);
向指定的欄位插入值
** * insert into tb1(gender, name)
values(‘M‘, ‘張三‘),
(‘F‘, ‘李四‘),
(‘M‘, ‘王五‘);
向表中一次插入多條資料(非標準sql)
* insert into tb1 select * from tb2
insert into tb1(name, gender) select name, gender from tb2
向tb1插入tb2表中的資料
修改資料update
===========================================================
*)update tb1 set name=‘abc‘,age=23,gender=null
where id=43;
刪除資料delete
===========================================================
*)delete from tb1 where .........
查詢資料
=========================================================
* where 子句
=等於
<>不等於
>大於
<小於
>=大於等於
<=小於等於
between 小值 and 大值範圍
in指定幾個固定取值
like 模糊查詢通常只查字串
%-匹配0 到多個任一字元
_-匹配單個任一字元
escape ‘\‘ : 指定轉移運算子
\_普通底線
\%普通%字元
is null
not
------------------------------
not between and
not in
is not null
and
or
mysql 資料庫基本命令語句