2020-02-13

數(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)建關鍵字數(shù)據(jù)庫

create database datebasecharset utf8; -- 報錯

-- 使用反引號

create database `datebase`charset utf8;

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

create database 北京charset utf8;

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

set names gbk;

create database 北京charset utf8;

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

show database;

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

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

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

show databases like 'information\_%';

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

show create database mydb;

show create database `database`;-- 關鍵字需要使用反引號

-- 數(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結尾的表

show tables like '%s';

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

show create table student;

show create table student\g-- \g 等價于 ;

show create table student\G-- \G 將查到的結構旋轉90度變成縱向

-- 查看表結構

desc class;

describe class;

show columns from class;

-- 重命名表

rename table studentto my_student;

-- 修改表選項;字符集

alter table my_studentcharset = GBK;

-- 字段的操作:

-- 給學生表添加id,放到第一個位置

alter table my_student

add column id int

first ;

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

alter table my_studentmodify number char(10)after id;

-- 修改學生表中的gender字段改為sex

alter table my_studentchange gender sex varchar(10);

-- 刪除學生表中的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的學生的信息

-- 更新數(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 '學號:bc2017+0001 不能重復'-- 主鍵本身不能為空

)charset utf8;

-- 復合主鍵

create table my_pri2(

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

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

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

? ? -- 增加主鍵限制:學號和課程號應該是對應的,具有唯一性

? ? 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 '學號:唯一,允許為空',

? ? name varchar(20)not null

)charset utf8;

create table my_unique2(

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

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

?著作權歸作者所有,轉載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容