2020-02-13

數(shù)據(jù)庫的基本操作:

-- 連接認(rèn)證

mysql.exe -h localhost -P3306 -u root -p

-- 查看所有數(shù)據(jù)庫

show databases

-- 斷開連接

exit

quit

\q

-- 雙中劃線+空格:注釋 (單行注釋)驶鹉,也可以使用#號(hào)

-- 創(chuàng)建數(shù)據(jù)庫

create database mybdcharset utf8;

-- 創(chuàng)建關(guān)鍵字?jǐn)?shù)據(jù)庫

create database datebasecharset utf8; -- 報(bào)錯(cuò)

-- 使用反引號(hào)

create database `datebase`charset utf8;

-- 使用中文數(shù)據(jù)庫

create database 北京charset utf8;

-- 如果報(bào)錯(cuò)解決方案暴区,告訴服務(wù)器當(dāng)前中文的字符集是什么

set names gbk;

create database 北京charset utf8;

-- 查看數(shù)據(jù)庫

show database;

-- 查看指定部分的數(shù)據(jù)庫

-- 查看以information_開始的數(shù)據(jù)庫 (_需要被轉(zhuǎn)義 %匹配多個(gè)字符集 _匹配單個(gè)字符集)

show databases like 'information_%';-- 相當(dāng)于information%

show databases like 'information\_%';

-- 查看數(shù)據(jù)庫的創(chuàng)建語句

show create database mydb;

show create database `database`;-- 關(guān)鍵字需要使用反引號(hào)

-- 數(shù)據(jù)庫的修改 數(shù)據(jù)庫名字不可以修改 數(shù)據(jù)庫的修改僅限庫選項(xiàng)

-- 修改數(shù)據(jù)庫informationtest 的字符集

alter database informationtestcharacter GBK;

-- 刪除數(shù)據(jù)庫

drop database informationtest;

-- 新增數(shù)據(jù)表

create table if not exists student(

-- 顯示地將student表放到mydb數(shù)據(jù)庫下

name varchar(10),

gender varchar(10),

number varchar(10),

age int

)charset utf8;

-- 創(chuàng)建數(shù)據(jù)庫表

-- 進(jìn)入數(shù)據(jù)庫

use mydb;

-- 創(chuàng)建表

create table class(

name varchar(10),

room varchar(10)

)charset utf8;

-- 查看所有表

show tables;

-- 查看部分表(模糊查詢)

-- 查看以s結(jié)尾的表

show tables like '%s';

-- 查看表的創(chuàng)建語句

show create table student;

show create table student\g-- \g 等價(jià)于 ;

show create table student\G-- \G 將查到的結(jié)構(gòu)旋轉(zhuǎn)90度變成縱向

-- 查看表結(jié)構(gòu)

desc class;

describe class;

show columns from class;

-- 重命名表

rename table studentto my_student;

-- 修改表選項(xiàng)鸣驱;字符集

alter table my_studentcharset = GBK;

-- 字段的操作:

-- 給學(xué)生表添加id瘟判,放到第一個(gè)位置

alter table my_student

add column id int

first ;

-- 將學(xué)生表中的number學(xué)號(hào)字段變成固定長度,且放到第二位(id之后)

alter table my_studentmodify number char(10)after id;

-- 修改學(xué)生表中的gender字段改為sex

alter table my_studentchange gender sex varchar(10);

-- 刪除學(xué)生表中的age年齡字段

alter table my_studentdrop age;

-- 刪除數(shù)據(jù)表

drop table class;

-- jim bc20200001 male lily female

-- 數(shù)據(jù)的操作

-- 插入數(shù)據(jù)

insert into my_studentvalues (1,'jim','male','bc20200001'),

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? (2,'lily','female','bc20200001');

-- 插入數(shù)據(jù),指定字段列表

insert into my_student(number,gender,name,id)values ('bc2020','male','tom','3'),('bc2020','female','lucy','4');

-- 查看所有數(shù)據(jù)

select * from my_student;

-- 查看指定字段,指定條件的數(shù)據(jù)

select id,number,gender,name from my_student

where id=1; -- 查看滿足id為1的學(xué)生的信息

-- 更新數(shù)據(jù)

update my_studentset gender='female' where name='jim';

-- 刪除數(shù)據(jù)

delete from my_studentwhere gender='male';

-- 創(chuàng)建整形表

create table my_int(

int_1 tinyint,

? ?? int_2 smallint,

? ? ? int_3 int,

? ? ?? int_4 bigint

)charset utf8;

-- 插入數(shù)據(jù)

insert into my_int

values (100,100,100,100);-- 有效數(shù)據(jù)

insert into my_int

values ('a','b','199','f');-- 無效數(shù)據(jù) 類型限定

insert into my_int

values (255,1000,100000,100000);-- 錯(cuò)誤 超出范圍

-- 給表增加一個(gè)無符號(hào)類型

alter table my_intadd int_5 tinyint unsigned; -- 無符號(hào)類型

insert into my_int

values (127,1000,100000,100000,255);

-- 指定顯示寬度為1

alter table my_intadd int_6 tinyint(1)unsigned;

-- 插入數(shù)據(jù)庫

insert into my_int

values (127,0,0,0,255,255);

-- 顯示寬度為2,0填充

alter table my_intadd int_7 tinyint(2)zerofill;

-- 插入數(shù)據(jù)

insert into my_int

values (1,1,1,1,1,1,1);

insert into my_int

values (100,100,100,100,100,100,100);

-- 浮點(diǎn)型

-- 浮點(diǎn)數(shù)表

create table my_float(

f1 float,

? ? f2 float(10,2),-- 10位在精度范圍之外

? ? f3 float(6,2)-- 6為在精度范圍之內(nèi)

)charset utf8;

-- 插入數(shù)據(jù)

insert into my_float

values (1000.10,1000.10,1000.10);

insert into my_float

values (1234567890,12345678.90,1234.56);

insert into my_float

values (3e38,3.01e7,1234.56);

insert into my_float

values (999999999,99999999.99,999.99); -- 后兩個(gè)是最大值

-- 超出長度插入數(shù)據(jù)

insert into my_float

values (123456,1234.12345678,123.9876543);

insert into my_float

values (123456,1234.12,12345.56);

-- 創(chuàng)建定點(diǎn)數(shù)據(jù)表

create table my_decimal(

f1 float(10,2),

? ? d1 decimal(10,2)

)charset utf8;

-- 插入數(shù)據(jù)

insert into my_decimal

values (12345678.90,12345678.90);-- 有效數(shù)據(jù)

insert into my_decimal

values (1234.123456,1234.123456);-- 小數(shù)部分可以超出長度

-- 查看警告

show warnings ;

-- 插入數(shù)據(jù)

insert into my_decimal

values (99999999.99,99999999.99); -- 沒有問題

insert into my_decimal

values (99999999.99,99999999.999); -- 進(jìn)位超出范圍

-- 時(shí)間日期

-- datetime

-- 創(chuàng)建時(shí)間日期表

create table my_date(

d1 datetime,

? ? d2 date,

? ? d3 time,

? ? d4 timestamp,

? ? d5 year

)charset utf8;

-- 插入數(shù)據(jù)

insert into my_datevalues ('2020-02-12 10:29:30','2020-02-12','10:29:30','2020-02-12 10:29:30','2020');

-- 時(shí)間可以使用負(fù)數(shù)

insert into my_datevalues ('2020-02-12 10:29:30','2020-02-12','-10:29:30','2020-02-12 10:29:30','2020');

insert into my_datevalues ('2020-02-12 10:29:30','2020-02-12','-210:29:30','2020-02-12 10:29:30','2020');

insert into my_datevalues ('2020-02-12 10:29:30','2020-02-12','-2 10:29:30','2020-02-12 10:29:30','2020');

-- year可以使用2位或者4位

insert into my_datevalues ('2020-02-12 10:29:30','2020-02-12','10:29:30','2020-02-12 10:29:30','70');

-- timestamp: 修改記錄

update my_dateset d3='2020-01-12 20:13:30' where d5=2020;

-- 使用函數(shù)獲取時(shí)間戳

select unix_timestamp();

-- 字符串

-- 列屬性

-- 創(chuàng)建班級(jí)表

create table my_class

(

name varchar(20)not null,

? ? room varchar(20)null -- 不寫默認(rèn)就是允許為空

)charset utf8;

-- 創(chuàng)建一個(gè)表

create table my_teacher(

name varchar(20)not null comment '姓名',

? ? money decimal(10,2)not null comment '工資'

)charset utf8;

-- 默認(rèn)值

create table my_default(

name varchar(20)not null ,

? ? age tinyint unsigned default 0,

? ? gender enum('男','女','保密')default '男'

)charset utf8;

-- 插入數(shù)據(jù)

insert into my_default(name)values ('小明');

insert into my_defaultvalues ('男閨蜜',18,default);

-- 增加主鍵

create table my_pril(

name varchar(20)not null comment '姓名',

? ? number char(10)primary key comment '學(xué)號(hào):bc2017+0001 不能重復(fù)'-- 主鍵本身不能為空

)charset utf8;

-- 復(fù)合主鍵

create table my_pri2(

number char(10)comment '學(xué)號(hào):bc2017+0001',

? ? course char(10)comment '課程代碼:bc2589+0001',

? ? score tinyint unsigned default 60 comment '成績:',

? ? -- 增加主鍵限制:學(xué)號(hào)和課程號(hào)應(yīng)該是對(duì)應(yīng)的补鼻,具有唯一性

? ? primary key (number,course)

)charset utf8;

-- 追加主鍵

create table my_pri3(

course char(10)not null comment '課程代碼:bc2589+0001',

? ? name varchar(10)not null comment '課程名字'

)charset utf8;

-- 追加主鍵的方法

alter table my_pri3modify course char(10)primary key comment '課程代碼:bc2589+0001';

alter table my_pri3add primary key (course);

-- 向pril、2表插入數(shù)據(jù)

insert into my_prilvalues ('古天樂','bc20200001'),('蔡老師','bc20200002');

insert into my_pri2values ('bc20200001','bc20200001',90),('bc20200001','bc20200002',85),('bc20200002','bc20200001',92);

-- 主鍵沖突

insert into my_prilvalues ('劉濤','bc20200002'); -- 不可以主鍵沖突

insert into my_pri2values ('bc20200001','bc25890001',100);-- 不可以恬汁,沖突

-- 刪除主鍵

alter table my_pri3drop primary key ;

-- 自增長

create table my_auto(

id int primary key auto_increment comment '自增長',

? ? name varchar(10)not null

)charset utf8;

-- 觸發(fā)自增長

insert into my_auto(name)values ('鄧麗君');

insert into my_autovalues (null,'成龍');

insert into my_autovalues (default,'吳綺莉');

-- 指定數(shù)據(jù)

insert into my_autovalues (6,'黃曉明');

insert into my_autovalues (null,'楊穎');

-- 修改自增長的值

alter table my_autoauto_increment=4;-- 向下修改(改写夜稀)不生效

alter table my_autoauto_increment=10; -- 向上修改(改大)

-- 查看自增長變量

show variables like 'auto_increment%';

-- 修改自增長步長

set auto_increment_increment =5; -- 一次自增5

-- 插入記錄:使用自增長

insert into my_autovalues (null,'楊紫');

insert into my_autovalues (null,'張一山');

-- 刪除自增長

alter table my_automodify id int primary key ;-- 錯(cuò)誤:主鍵理論上是單獨(dú)存在的

alter table my_automodify id int;-- 有主鍵的時(shí)候,不能加主鍵

-- 唯一鍵

create table my_unique1(

number char(10)unique comment '學(xué)號(hào):唯一,允許為空',

? ? name varchar(20)not null

)charset utf8;

create table my_unique2(

number char(10)not null comment '學(xué)號(hào)',

? ? name varchar(20)not null ,

? ? -- 增加唯一鍵

? ? unique key (number)

)charset utf8;

create table my_unique3(

id int primary key auto_increment,

? ? number char(10)not null ,

? ? name varchar(20)not null

)charset utf8;

-- 追加唯一鍵

alter table my_unique3add unique key(number);

-- 插入數(shù)據(jù)

insert into my_unique1values (null,'大熊'),('bc20200001','tom'),(null,'小靜');

insert into my_unique1values ('bc20200001','哆啦a夢(mèng)');

-- 刪除唯一鍵

alter table my_unique3drop index number;

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子废麻,更是在濱河造成了極大的恐慌,老刑警劉巖模庐,帶你破解...
    沈念sama閱讀 217,826評(píng)論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件烛愧,死亡現(xiàn)場離奇詭異,居然都是意外死亡掂碱,警方通過查閱死者的電腦和手機(jī)怜姿,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,968評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來疼燥,“玉大人沧卢,你說我怎么就攤上這事∽碚撸” “怎么了但狭?”我有些...
    開封第一講書人閱讀 164,234評(píng)論 0 354
  • 文/不壞的土叔 我叫張陵,是天一觀的道長撬即。 經(jīng)常有香客問我立磁,道長,這世上最難降的妖魔是什么剥槐? 我笑而不...
    開封第一講書人閱讀 58,562評(píng)論 1 293
  • 正文 為了忘掉前任唱歧,我火速辦了婚禮,結(jié)果婚禮上粒竖,老公的妹妹穿的比我還像新娘颅崩。我一直安慰自己绍刮,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,611評(píng)論 6 392
  • 文/花漫 我一把揭開白布挨摸。 她就那樣靜靜地躺著,像睡著了一般岁歉。 火紅的嫁衣襯著肌膚如雪得运。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,482評(píng)論 1 302
  • 那天锅移,我揣著相機(jī)與錄音熔掺,去河邊找鬼。 笑死非剃,一個(gè)胖子當(dāng)著我的面吹牛置逻,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播备绽,決...
    沈念sama閱讀 40,271評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼券坞,長吁一口氣:“原來是場噩夢(mèng)啊……” “哼!你這毒婦竟也來了肺素?” 一聲冷哼從身側(cè)響起恨锚,我...
    開封第一講書人閱讀 39,166評(píng)論 0 276
  • 序言:老撾萬榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎倍靡,沒想到半個(gè)月后猴伶,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,608評(píng)論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡塌西,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,814評(píng)論 3 336
  • 正文 我和宋清朗相戀三年他挎,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片捡需。...
    茶點(diǎn)故事閱讀 39,926評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡办桨,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出栖忠,到底是詐尸還是另有隱情崔挖,我是刑警寧澤,帶...
    沈念sama閱讀 35,644評(píng)論 5 346
  • 正文 年R本政府宣布庵寞,位于F島的核電站狸相,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏捐川。R本人自食惡果不足惜脓鹃,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,249評(píng)論 3 329
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望古沥。 院中可真熱鬧瘸右,春花似錦娇跟、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,866評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至龄章,卻和暖如春吃谣,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背做裙。 一陣腳步聲響...
    開封第一講書人閱讀 32,991評(píng)論 1 269
  • 我被黑心中介騙來泰國打工岗憋, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人锚贱。 一個(gè)月前我還...
    沈念sama閱讀 48,063評(píng)論 3 370
  • 正文 我出身青樓仔戈,卻偏偏與公主長得像,于是被迫代替她去往敵國和親拧廊。 傳聞我的和親對(duì)象是個(gè)殘疾皇子监徘,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,871評(píng)論 2 354

推薦閱讀更多精彩內(nèi)容