mysql 常用操作指令

mysqlnodejs 中實踐

項目地址

-- 顯示所有數據庫
show databases;
-- 創建數據庫
create database <db_name>
-- 顯示所有數據表
show tables;
-- 查看錶結構
desc <tb_name>;
-- 創建數據表
create table <tb_name> (
  id int unsigned not null primary key; -- 無符號整型、非空、主鍵  
  username varchar(45) not null; -- 字符串類型、非空
);
-- 新增
insert into <tb_name> (cols1,col2,...) values (val1,val2,...);
-- 示例:
insert into users
-> (username,`password`,createtime)
-> values
-> ('root','123',NOW());
-- 全查詢
select * from <tb_name>;
-- 查詢
select <col1>,<col2>,... from <tb_name>;
-- 示例:
select username,id from users;
-- 條件查詢:
select <col1>,<col2>,... from <tb_name> where <col1=val1>,...
-- 示例:
select username,id from users where username='root'
-- 多條件查詢
select <col1>,<col2>,... from <tb_name> where <col1=val1> and <col2=val2>
select <col1>,<col2>,... from <tb_name> where <col1=val1> or <col2=val2>
-- 模糊查詢
select <col1>,<col2>,... from <tb_name> where col1 like `%<val1>%`
-- 排序
select <col1>,<col2>,... from <tb_name> where <col1=val1> order by id;
-- 倒序
select <col1>,<col2>,... from <tb_name> where <col1=val1> order by id desc;
-- 新增一列
alter table <tb_name>
-> add column <col3> varchar(45) not null;
-- 修改字段值
update <tb_name> set <col1>=<val_new> where <col2>=<val2>;
-- 修改多個字段值
update <tb_name> set <col1>=<val_new1>,<col2>=<val_new2> where <col2>=<val2>;
-- 示例: 
update users set nickname='栗子🌰' where id=1;
-- 刪除
delete from <tb_name> where <col1>=<val1>;
-- 示例:
delete from users where id=3;
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章