哈嘍兄弟們,中秋閑著沒事,整理了一些資料庫的基本操作,分享給大家,希望對大家有所幫助~

一、SQL陳述句 (mysql 資料庫中的語言)
show databases;查看資料庫 use "database_ name" ;進入資料庫 show tables; 查看當前資料庫中有哪些表 select * from "table_ name";查詢資料表中的所有內容 describe "table_ name"; 查看表結構 desc "table_ name";







類比excel表格




類比excel表格


簡寫
filed 欄位名

二、DDL
1.DDL陳述句
用于創建資料庫物件(庫、表、索引等)
(1)創建新的資料庫
create database 資料庫名; # Python學習交流群 279199867
(2)創建新的表
create table 表名(欄位1 資料型別,欄位2 資料型別[, …] [, primary key (主鍵名)]);
主鍵一般選擇能代表唯一性的欄位不允許取空值(NULL) ,一個表只能有一個主鍵,
create database 資料庫名 use 資料庫名 create table 表名 (id int not null, name char(10) not null, score decimal (5,2) ,passwd char (48) defalt' ',primary key (id)) ; desc 表名 not null 不允許為空值 default ' ' 默認值為空 primary key : 主鍵一般選擇沒有重復并且不為空值的欄位
例子
create table 表名 (id int(10) not null primary key, name varchar(40) ,age int(3)); create table food (id int(3) , name varchar (40) ,money decimal (3,1) ,primary key (id));





2.洗掉資料庫和表
洗掉指定的資料表
drop 洗掉表內容(資料)和表結構 use 資料庫名 drop table 表名 drop table [資料庫名.] 表名;
如不用use進入庫中,則需加上資料庫名
洗掉指定的資料庫
drop database 資料庫名;









三、DML
管理表中的資料記錄
insert: 插入新資料 update: 更新原有資料 delete: 洗掉不需要的資料
1.insert插入新資料
格式:
insert into 表名(欄位1,欄位2[,...]) values (欄位1的值,欄位2的值,...);
例子:
insert into 表名 (id,name,score,passwd) values (1,'自定義',70.5,passwd('123456')) ;
passwd(‘123456’) :查詢資料記錄時,密碼字串以加密形式顯示:若不使用passwd(), 查詢時以明文顯示,
密碼復雜性驗證
insert into 表名 values(2,'自定義',90.5, 654321) ; select * from 表名 ; 查詢表的資料記錄
insert插入表資料
在此之前需要進行查看desc table_ name; 來查看表結構(有哪些欄位,有無主鍵,主鍵是哪個欄位,type,是否允許為空,是否有默認值)
使用insert into table_ name進行插入,是根據查看到的表結構來判斷,可以怎么寫



2.update更新原有資料
修改、更新資料表中的資料記錄
格式:
update 表名 set 欄位名1=欄位值1[,欄位名2=欄位值2] [where 條件運算式];
例子:
update 表名 set passwd=PASSWORD('') where name='自定義'; update 表名 set name= '自定義' , passwd='' where id=3;



3.delete: 洗掉不需要的資料(表內容)
在資料表中洗掉指定的資料記錄(行)
格式:.
delete from 表名 [where 條件運算式];
例子:
delete from 表名 where id=4;

四、DQL查詢資料記錄
select
格式:
seleect 欄位名1,欄位名2[,...] from 表名[where 條件運算式];
例子:
seleect * from 表名; seleect id, name from 表名; seleect id, name, score from 表名 where id=2; select name from 表名\G 以串列方式豎向顯示 select * from info limit 2; 只顯示頭3行 select * from info limit 2,3; 顯示第3行后的前3行





類比excel表格

四、DCL
1.alter 修改表名和表結構(表結構)
alter table 舊表名 rename 新表名; 擴展表結構(增加欄位) alter table 表名 add address varchar(50) default '地址不詳' ; default ' 地址不詳':表示此欄位設定默認值為地址不詳,可與not null配合使用 alter table 表名 add address varchar(50) not null default '地址不詳' ; 修改欄位(列)名,添加唯一鍵(唯一性約束) alter table 表名 change 舊列名 新列名 資料型別 [unique key] ; unique key:唯一鍵(特性:唯一, 但可以為空,空值只允許出現一次) primary key (主鍵) :唯一且非空 alter table 表名 change name user_ name varchar(10) unique key; change可修改欄位名、資料型別、約束等所有項, 洗掉欄位 格式: alter table 表名 drop 欄位名;
兄弟們,今天的分享就到這里結束了,下次見~
如果本文對你有所幫助的話,記得點贊收藏呀~
最后推薦一套Python教程:Python實戰100例
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/507268.html
標籤:其他
