基本語句

1、連接認證基本語法
mysql.exe -hlocalhost -p3306 -uroot -proot
mysql -uroot -p
Enter password:****(root)
2、退出

建議方式:使用sql提供的指令

Exit;//exit帶分號
\q //quit縮寫
Quit
3、創(chuàng)建數(shù)據(jù)庫

基本語法:create database 數(shù)據(jù)庫名字[庫選項];
庫選項:數(shù)據(jù)庫的相關(guān)屬性
字符集:charset字符集,代表著當前數(shù)據(jù)庫下的所有表儲存的數(shù)據(jù)默認指定的字符集(如果當前不指定,那么采用DBMS默認的)
校對集:collate
Create database 數(shù)據(jù)庫名字 charset 字符集名稱;

create database mydatabase charset gbk;
4、顯示所有數(shù)據(jù)庫

基本語法:showdatabases like '匹配模式';
_:匹配當前位置單個字符
%:匹配指定位置多個字符
獲取以my開頭的全部數(shù)據(jù)庫:'my%';
獲取m開頭,后面第一個字母不確定,最后為database的數(shù)據(jù)庫:'m_database';
獲取以database結(jié)尾的數(shù)據(jù)庫:'%database';

顯示所有數(shù)據(jù)庫

show databases;
image.png

查看以my開頭的數(shù)據(jù)庫

show databases like 'my%';
image.png

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

show create database mydatabase;
image.png
5、修改數(shù)據(jù)庫

修改數(shù)據(jù)庫字符集(庫選項):字符集和校對集
基本語法:alter database 數(shù)據(jù)庫名字 charset 字符集;

alter database mydatabase charset gbk;
image.png

是否可以修改數(shù)據(jù)庫名字?mysql5.5之前是可以修改的,rename命令,但是5.5之后就不可以了

6、刪除數(shù)據(jù)庫

基本語法:drop database 數(shù)據(jù)庫名稱

drop database mydatabase;

image.png

刪除雖簡單,但是切記要做好安全操作,確保里面數(shù)據(jù)沒有問題了
刪除數(shù)據(jù)庫之后:對應(yīng)儲存數(shù)據(jù)的文件夾也會被刪除(opt文件也會被刪除)

7、創(chuàng)建數(shù)據(jù)表

基本語法:create table 表名 (字段名 字段類型 [字段屬性],字段名 字段類型 [字段屬性]...)[表選項]

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

create table  class(name varchar(10));

image.png

以上錯誤表明,表必須放在對應(yīng)的數(shù)據(jù)庫下,有倆種方式可以將表掛入到指定的數(shù)據(jù)庫下
1、在數(shù)據(jù)表名字前面加上數(shù)據(jù)庫名字,用"."連接即可:數(shù)據(jù)庫.數(shù)據(jù)表

將數(shù)據(jù)表掛到數(shù)據(jù)庫下

create table mydatabase2. class(name varchar(10));

image.png

2、在創(chuàng)建數(shù)據(jù)表之前先進入到某個具體的數(shù)據(jù)庫即可:use 數(shù)據(jù)庫名字
進入數(shù)據(jù)庫,創(chuàng)建表

use mydatabase2;
create table mydatabase2. class(name varchar(10));
image.png

表選項:與數(shù)據(jù)庫選項類似
Engine:存儲引擎,mysql提供的具體存儲數(shù)據(jù)的方式, 默認有一個innodb(5.5以前默認的是myisam)
Charset:字符集,只對當前自己表有效(級別比數(shù)據(jù)庫高)
Collate:校對集

使用表選項

create table student(name varchar(10))charset utf8;
image.png
8、復制已有表結(jié)構(gòu)

從已經(jīng)存在的表復制一份(只復制結(jié)構(gòu),如果表中有數(shù)據(jù)不復制)
基本語法:create table 新表名 like 表名;//只要使用數(shù)據(jù)庫.表名,就可以在任何數(shù)據(jù)庫下訪問其他 數(shù)據(jù)庫的表名

在test數(shù)據(jù)庫下創(chuàng)建一個與teacher一樣的表

use test;
create table teacher like mydatabase2.teacher; 
image.png
9、查看所有表

查看所有表

show tables;
image.png
10、匹配顯示表

基本語法:show tables like '匹配模式';

查看匹配數(shù)據(jù)表

show tables like 'c%';
image.png
11、顯示表結(jié)構(gòu)

本質(zhì)含義:顯示表中所包含的字段信息(名字,類型,屬性等)
Describe 表名
Desc 表名
show columns from 表名

顯示表結(jié)構(gòu)

describe class;
desc teacher;
show columns from student;
image.png
12、顯示表創(chuàng)建語句

查看數(shù)據(jù)表創(chuàng)建時的語句:此語句看到的結(jié)果已經(jīng)不是用戶之前輸入的
基本語法:show create table 表名;

show create table student;
image.png

mysql中有多種語句結(jié)束符
與\g所表示的效果是一樣的,都是字段在上橫排著,下面跟對應(yīng)的數(shù)據(jù),\G 字段在左側(cè)豎著,數(shù)據(jù)在右側(cè)豎著

show create table student\G
image.png
13、設(shè)置表屬性

表屬性指的就是表選項:engine,charest和collate
基本語法:alter table 表名 表選項 [=] 值

alter table student charset gbk;

image.png

注:如果數(shù)據(jù)庫已經(jīng)確定了,里面有很多數(shù)據(jù)了,不要輕易修改表選項()字符集影響不大

修改表結(jié)構(gòu)

14、修改表名

基本語法:rename table 舊表名 to 新表名
數(shù)據(jù)庫中數(shù)據(jù)表通常有前綴,取數(shù)據(jù)庫的前倆個字母加上下劃線

rename table student to my_student;
image.png
15、修改表選項

基本語法:alter table 表名 表選項 [=] 新值

alter table student charset gbk;
16、新增字段

基本語法:alter table 表名 add [column] 新字段名 列類型 [列屬性] [位置first/after字段名]

給學生表添加一個age字段

alter table my_student add column age int;
image.png

字段位置:字段想要存放的位置
First:在某某之前(最前面),第一個 字段
After:字段名:放在某個具體的字段之后(默認的)

添加字段:放在第一個字段

alter table my_student add id int first
image.png
17、修改字段名

基本語法:alter table 表名 change 舊字段名 新字段名 字段類型 [列屬性] [新位置]

修改字段名

alter  table my_student change age nj int;
image.png
18、修改字段類型(屬性)

基本語法:alter table 表名 modify 字段名 新類型 [新屬性] [新位置]

修改字段類型

alter table my_student modify name varchar(20);
image.png
19、刪除字段

基本語法:alter table 表名 drop 字段名

刪除字段

alter table my_student drop nj;
image.png
刪除表結(jié)構(gòu)

基本語法:drop table 表名,[表名2....]可以同時刪除多個數(shù)據(jù)表

刪除表名

drop table class;
image.png

批量刪除表名

drop table  teacher,my_student;
image.png

一、數(shù)據(jù)基礎(chǔ)操作-插入操作

本質(zhì)含義:將數(shù)據(jù)以sql的形式存儲到指定的數(shù)據(jù)表(字段)里面
基本語法:Insert into 表名 [(字段列表)] value(對應(yīng)字段列表),向表中所有字段插入數(shù)據(jù)

insert into my_student (name,age) values("jake",30);

image.png

注:后面(values中)對應(yīng)的值列表只需要與前面的字段列表相對應(yīng)即可(不一定與表結(jié)構(gòu)完全一致)

insert into my_student (age,name) values(49,"tom");

image.png

注:字段列表并不一定非要有所有的表中字段

insert into my_student (name) values("han");

image.png

基本語法:向表中所有字段插入數(shù)據(jù) //值列表必須與字段列表一致
Insert into 表名 value (對應(yīng)表結(jié)構(gòu))

insert into my_student values("lilei",28);
image.png

二、數(shù)據(jù)基礎(chǔ)操作-查詢操作

1、查詢表中全部數(shù)據(jù)

基本語法:select * from 表名;//表示匹配所有的字段*

獲取所有數(shù)據(jù)

select * from my_teacher;
image.png
2、查詢表中部分字段

基本語法:select 字段列表 from 表名 //字段列表使用“,”隔開

獲取指定字段

select name from my_teacher;
image.png
3、簡單條件查詢數(shù)據(jù)

基本語法:select 字段列表 / from 表名 where 字段名 = 值 //mysql中沒有==符號*

獲取年齡為30歲人的名字

select name from my_teacher where age = 30;
image.png

三、數(shù)據(jù)基礎(chǔ)操作-刪除操作

基本語法:delect from 表名 [where 條件];//如果沒有where條件,意味著系統(tǒng)會自動刪除該表所有數(shù)據(jù)(慎用)

刪除年齡為30歲的老師

delete from my_teacher where age = 30;
image.png

四、數(shù)據(jù)基礎(chǔ)操作-更新操作

更新:將數(shù)據(jù)進行修改(通常是修改部分字段數(shù)據(jù))
基本語法:updata 表名 set 字段名 = 新值 [where 條件];如果沒有where條件,那么所有的表中對應(yīng)的那個字段都會被修改成統(tǒng)一值

更新年齡Han

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

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

  • ORA-00001: 違反唯一約束條件 (.) 錯誤說明:當在唯一索引所對應(yīng)的列上鍵入重復值時,會觸發(fā)此異常。 O...
    我想起個好名字閱讀 5,967評論 0 9
  • /* 創(chuàng)建數(shù)據(jù)庫 create database 數(shù)據(jù)庫名; */ CREATE DATABASE myb...
    隨心者隨心行閱讀 216評論 0 0
  • MySQL數(shù)據(jù)庫 課程目標:1.如何使用MySQL數(shù)據(jù)庫,主要是講解基本的語法2.如何設(shè)計數(shù)據(jù)庫? 第一章 數(shù)據(jù)庫...
    我愛開發(fā)閱讀 1,403評論 1 4
  • 1.DDL 對數(shù)據(jù)庫內(nèi)部的對象進行創(chuàng)建、刪除、修改等操作的語言,DDL語句更多的是由數(shù)據(jù)庫管理員(DBA)使用,開...
    鄭宋君閱讀 505評論 0 0
  • 文/段代洪 牛皮紙,kraftpaper。堅韌、耐水。半漂的牛皮紙,呈淡褐色,舊時光的顏色。 ...
    段代洪的心靈驛站閱讀 992評論 1 5

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