數(shù)據(jù)庫mysql(一)

數(shù)據(jù)庫學(xué)習(xí)(mysql)

一.數(shù)據(jù)庫的操作(DDL)

1. create database if not exists db1 character set utf8;

創(chuàng)建數(shù)據(jù)庫db1,(如果不存在則創(chuàng)建,并指定字符集為utf-8)注意寫的時(shí)候是utf8,而不是utf-8

show databases;

查詢所有的數(shù)據(jù)庫

2.查詢數(shù)據(jù)庫創(chuàng)建語句

show create database db1;

3.修改數(shù)據(jù)庫字符集(修改數(shù)據(jù)庫的字符集為gbk)

alter database db1 character set gbk;

4.刪除數(shù)據(jù)庫(危險(xiǎn)操作)

drop database db1

drop database if exists db1 防止報(bào)錯(cuò),先判斷,后刪除

5.使用數(shù)據(jù)庫

use db1;

6.查詢正在使用數(shù)據(jù)庫的名稱

select database();

二.數(shù)據(jù)表的操作(DDL:數(shù)據(jù)定義語言)

1. 查詢數(shù)據(jù)表

show tables;

2.查詢表結(jié)構(gòu)

desc db1;

3.創(chuàng)建表

create table 表名(

列名1? 數(shù)據(jù)類型1,

列名2? 數(shù)據(jù)類型2

數(shù)據(jù)類型:int(整數(shù)),long(小數(shù)),date(yyyy-MM-dd),datetime(yyyy-mm-dd HH-mm-ss),timestamp(時(shí)間戳類型)varchar(字符串)

datetime與timestamp區(qū)別:timestamp如果不給這個(gè)字段賦值或者賦值為null則默認(rèn)使用當(dāng)前系統(tǒng)時(shí)間默認(rèn)賦值;

創(chuàng)建表實(shí)例:

create table Student(

id int,

name varchar(32),

age int,

score double(4,1),? --表示最多4位,小數(shù)點(diǎn)后取一位,如:100.0

birthday date,? ? ? ? --出生日期不用到時(shí)分秒,使用date

insert_time timestamp? --創(chuàng)建時(shí)間

);

4.刪除表

drop table student;

drop table if exists student;? 判斷是否存在

5.復(fù)制表

create table student2 like student;

6.修改表

修改表的名稱:alter table? student rename? to? student2;? 修改student的表明位student2

查看表的字符集:show create table student;

修改表的字符集:alter table student character set utf8;

添加一列:alter table student add 列名 數(shù)據(jù)類型;

修改列名:

第一種方式:alter table student change? name name2 varchar(10); 修改列名與數(shù)據(jù)類型

第二種方式:alter table student modify name varchar(10); 只修改數(shù)據(jù)類型

刪除列名:alter table student drop name2;

三.數(shù)據(jù)表的增刪改操作(DML:數(shù)據(jù)操縱語言)

1. 增加數(shù)據(jù)

insert into 表名(列名1,列名2)? values(值1,值2);

實(shí)例:insert into student(age,name) values(20."張三");

注意事項(xiàng):

1.列名與值一一對應(yīng)

2.如果給所有值添加,可以不寫列名

3.除了數(shù)據(jù)類型,其他字符類型需要使用引號(hào)(單引號(hào)或則雙引號(hào))

2.刪除數(shù)據(jù)

delete from student where id=1;

注意:

1.如果不加條件,則刪除所有數(shù)據(jù)

2.truncate 刪除,效率更高,推薦使用,原理是先drop table,再創(chuàng)建一張空表,delete則是一條? 一條數(shù)據(jù)刪除,但是delete支持回滾,更加安全

3.修改數(shù)據(jù)

update student set name = '李四' where id = 2;

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

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

  • 一、數(shù)據(jù)庫系統(tǒng) 數(shù)據(jù)庫DataBase【DB】,指的是長期保存到計(jì)算機(jī)上的數(shù)據(jù),按照一定順序組織,可以被各種用戶或...
    王梓懿_1fbc閱讀 666評論 0 0
  • 一、數(shù)據(jù)庫系統(tǒng) 數(shù)據(jù)庫DataBase【DB】,指的是長期保存到計(jì)算機(jī)上的數(shù)據(jù),按照一定順序組織,可以被各種用戶或...
    fly5閱讀 441評論 0 0
  • Ubuntu下安裝mysqlapt updatesudo apt-get install mysql-server...
    恬恬i阿萌妹O_o閱讀 355評論 0 0
  • 一、數(shù)據(jù)庫系統(tǒng) 數(shù)據(jù)庫DataBase【DB】,指的是長期保存到計(jì)算機(jī)上的數(shù)據(jù),按照一定順序組織,可以被各種用戶或...
    慕楊_閱讀 3,949評論 0 2
  • 一、數(shù)據(jù)庫系統(tǒng) 數(shù)據(jù)庫DataBase【DB】,指的是長期保存到計(jì)算機(jī)上的數(shù)據(jù),按照一定順序組織,可以被各種用戶或...
    EndEvent閱讀 2,085評論 2 3

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