數(shù)據(jù)庫

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

-- 連接認證

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

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

show databases

-- 斷開連接

exit

quit

\q

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

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

create database mybdcharset utf8;

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

create database datebasecharset utf8; -- 報錯

-- 使用反引號

create database `datebase`charset utf8;

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

create database 北京charset utf8;

-- 如果報錯解決方案浴栽,告訴服務(wù)器當前中文的字符集是什么

set names gbk;

create database 北京charset utf8;

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

show database;

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

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

show databases like 'information_%';-- 相當于information%

show databases like 'information\_%';

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

show create database mydb;

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

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

-- 修改數(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ù)庫表

-- 進入數(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 等價于 ;

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;

-- 修改表選項庄涡;字符集

alter table my_studentcharset = GBK;

-- 字段的操作:

-- 給學(xué)生表添加id雕拼,放到第一個位置

alter table my_student

add column id int

first ;

-- 將學(xué)生表中的number學(xué)號字段變成固定長度缕贡,且放到第二位(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);-- 錯誤 超出范圍

-- 給表增加一個無符號類型

alter table my_intadd int_5 tinyint unsigned; -- 無符號類型

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);

-- 浮點型

-- 浮點數(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); -- 后兩個是最大值

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

insert into my_float

values (123456,1234.12345678,123.9876543);

insert into my_float

values (123456,1234.12,12345.56);

-- 創(chuàng)建定點數(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); -- 進位超出范圍




-- 時間日期

-- datetime

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

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ù)

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ù)獲取時間戳

select unix_timestamp();

-- 字符串

-- 列屬性

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

create table my_class

(

name varchar(20)not null,

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

)charset utf8;

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

create table my_teacher(

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

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

)charset utf8;

-- 默認值

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é)號:bc2017+0001 不能重復(fù)'-- 主鍵本身不能為空

)charset utf8;

-- 復(fù)合主鍵

create table my_pri2(

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

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

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

? ? -- 增加主鍵限制:學(xué)號和課程號應(yīng)該是對應(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 ;-- 錯誤:主鍵理論上是單獨存在的

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

-- 唯一鍵

create table my_unique1(

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

? ? name varchar(20)not null

)charset utf8;

create table my_unique2(

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

? ? 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夢');

-- 刪除唯一鍵

alter table my_unique3drop index number;

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末赠堵,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子法褥,更是在濱河造成了極大的恐慌茫叭,老刑警劉巖,帶你破解...
    沈念sama閱讀 217,826評論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件半等,死亡現(xiàn)場離奇詭異揍愁,居然都是意外死亡呐萨,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,968評論 3 395
  • 文/潘曉璐 我一進店門莽囤,熙熙樓的掌柜王于貴愁眉苦臉地迎上來谬擦,“玉大人,你說我怎么就攤上這事朽缎〔以叮” “怎么了?”我有些...
    開封第一講書人閱讀 164,234評論 0 354
  • 文/不壞的土叔 我叫張陵话肖,是天一觀的道長北秽。 經(jīng)常有香客問我,道長最筒,這世上最難降的妖魔是什么贺氓? 我笑而不...
    開封第一講書人閱讀 58,562評論 1 293
  • 正文 為了忘掉前任,我火速辦了婚禮床蜘,結(jié)果婚禮上辙培,老公的妹妹穿的比我還像新娘。我一直安慰自己悄泥,他們只是感情好虏冻,可當我...
    茶點故事閱讀 67,611評論 6 392
  • 文/花漫 我一把揭開白布肤粱。 她就那樣靜靜地躺著弹囚,像睡著了一般。 火紅的嫁衣襯著肌膚如雪领曼。 梳的紋絲不亂的頭發(fā)上鸥鹉,一...
    開封第一講書人閱讀 51,482評論 1 302
  • 那天,我揣著相機與錄音庶骄,去河邊找鬼毁渗。 笑死,一個胖子當著我的面吹牛单刁,可吹牛的內(nèi)容都是我干的灸异。 我是一名探鬼主播,決...
    沈念sama閱讀 40,271評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼羔飞,長吁一口氣:“原來是場噩夢啊……” “哼肺樟!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起逻淌,我...
    開封第一講書人閱讀 39,166評論 0 276
  • 序言:老撾萬榮一對情侶失蹤么伯,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后卡儒,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體田柔,經(jīng)...
    沈念sama閱讀 45,608評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡俐巴,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,814評論 3 336
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了硬爆。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片欣舵。...
    茶點故事閱讀 39,926評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖摆屯,靈堂內(nèi)的尸體忽然破棺而出邻遏,到底是詐尸還是另有隱情,我是刑警寧澤虐骑,帶...
    沈念sama閱讀 35,644評論 5 346
  • 正文 年R本政府宣布准验,位于F島的核電站,受9級特大地震影響廷没,放射性物質(zhì)發(fā)生泄漏糊饱。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,249評論 3 329
  • 文/蒙蒙 一颠黎、第九天 我趴在偏房一處隱蔽的房頂上張望另锋。 院中可真熱鬧,春花似錦狭归、人聲如沸夭坪。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,866評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽室梅。三九已至,卻和暖如春疚宇,著一層夾襖步出監(jiān)牢的瞬間亡鼠,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,991評論 1 269
  • 我被黑心中介騙來泰國打工敷待, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留间涵,地道東北人。 一個月前我還...
    沈念sama閱讀 48,063評論 3 370
  • 正文 我出身青樓榜揖,卻偏偏與公主長得像勾哩,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子举哟,可洞房花燭夜當晚...
    茶點故事閱讀 44,871評論 2 354

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