1. 程式人生 > 實用技巧 >MySQL資料庫常用命令

MySQL資料庫常用命令

基本的SQL語句有select、insert、update、delete、create、drop、grant、revoke等,具體使用方式見表:

庫操作

建立資料庫:create database shujuku;
建立帶字符集的資料庫:create database mydb2 CHARACTER SET=UTF8;
建立帶校驗的資料庫:create database mydb3 CHARACTER SET=UTF8 COLLATE utf8_general_ci;
顯示資料庫:show databases;
刪除資料庫:drop database shujuku;
修改資料庫編碼:alter databaese shujuku character set gb2312;

表操作

建立資料庫表(建立一個表名為:employee,該表中含有id、name、sex、birthday、job欄位);

create table employee
(
id int,
name varchar(40),
sex char(4),
birthday date,
job varchar(40),
);

表中增加image欄位:alter table employee add image blob;
修改job值,使其長度為60(原長度為1000):alter table employee modify job varchar(60);
刪除sex列:alter table employee drop sex;

表名改為user(原名為employee):rename table employee to user;
修改表的字符集為utf_8:alter table user character set utf8;
列name修改為 username:alter table change columm name username varchar(100);
刪除表:drop table user;

增刪改查例項

準備表:

create table employee
(
id int,
name varchar(40),
sex varchar(4),
birthday date,
entry_date date,
salary decimal(8,2),
resume text
);

插入表資料:

insert into employee(id ,name,sex,birthday,entry_date,salary,resume) values(1,'zhangsan','male','1999-08-22','2020-08-22,'1000','i am a developer');

指定某些列插入資料:insert into employee(id) values(6);
插入漢字:insert into employee(id,name) values(6,‘張三’);

修改表資料

將所有員工薪水修改為5000元:update employee set salary=5000;

將姓名為’zs’的員工薪水修改為3000元:update employee set salary = 3000 where name=‘zhangsan’;

將姓名為’aaa’的員工薪水修改為4000元,job改為ccc:update employee set salary = 4000,job=‘ccc’ where name=‘張三’;

將wu的薪水在原有基礎上增加1000元:update employee set salary = salary+1000 where name=‘張三’;

刪除表資料

刪除表中名稱為“zs”的記錄:delete from employee where job=‘ccc’;
刪除表中所有記錄:delete from employee;
使用truncate刪除表中記錄:truncate table employee;

查詢表資料

查詢表中所有學生的資訊:select id,name,chinese,english,math from student;
查詢表中所有學生的姓名和對應的英語成績:select name,english from student;
查詢姓名為wu的學生成績:select * from student where name=‘張三’;
查詢英語成績大於90分的同學:select * from student where english>‘90’;
查詢英語分數在 80-90之間的同學:select * from student where english>=80 and english=<90;

常見MySQL語句命令

進入mysql 命令列:mysql -uroot -p;
檢視所有資料庫:show databases;
建立資料庫:create database niu charset utf8;
刪除資料庫:drop database niu;
選擇資料庫:use databases;
檢視所有表:show tables;
檢視建立資料庫的語句:show create database databasename;
檢視建立表的語句:show create table tablename;
查看錶結構:desc tablenmae;

常見MySQL欄位含義

自增長:auto_increment
非空:not null
預設值:default
唯一:unique
指定字符集:charset
主鍵:primary key

詳見:https://www.shujukuba.com/mysql/99.html