MySQL操作常用語句

2021-09-23 17:10:45 字數 2435 閱讀 8222

主表中的資料

dml-表資料的增刪改

準備:建立分類表,用以練習

create table category(

cid int primary key, (主鍵約束)

cname varchar(100)

);需求1:向category表中,新增一條資料 1,『手機數碼』

insert into category (cid,cname) values (1,『手機數碼』);

需求2:向table1表中,新增一條資料 2,『小明』,18,『2001-07-01』

insert into table1 (id,name,age,birthday) values (『2』,『小明』,18,『2001-07-01』);

需求3:向table1表中,新增一條資料 『3』,『小紅』 忽略age,birthday

insert into table1 (id,name) values (『3』,『小紅』);

需求4:向category表中,新增一條資料 2,『電子產品』

insert into category values (2,『電子產品』);

需求5:向category表中,新增一條資料 3 cname不填

insert into category (id) values (3);

insert into category values (3,null);

需求6:向category表中,一次性新增多條資料

4,鞋靴箱包 5,戶外用品 6,母嬰用品

insert into category values (4,『鞋靴箱包』),(5,『戶外用品』),(6,『母嬰用品』);

表資料的新增:

字段操作:

修改t1表,新增一列:remark ,字串,長度100

alter table t1 add remark varchar(100);

修改t1表,修改remark這一列長度為64

alter table t1 modify remark varchar(64);

修改t1表,修改remark為rek

alter table t1 change remark rek varchar(64);

修改t1表,刪除rek列

alter table t1 drop rek;

修改t1表,為table1表

rename table t1 to table1;

資料操作:

修改表資料

需求1:需要將table1中,所有人的年齡改為68歲,所有人的生日改為 「1941-01-01」

update table1 set age=68 ,birthday=『1941-01-01』;

需求2:修改category表中,id為6的資料,cname改為』生活用品』

update category set cname=『生活用品』 where cid=6;

注意:1、資料的型別必須和建表資料型別保持一致

多個設定之間 用,分隔

需求3:修改category表中,cname為生活用品 的資料,cid改為66

update category set cid=66 where cname=『生活用品』;

需求4:修改category表中,cid為66的資料,cid改為99

update category set cid=99 where cid=66;

表資料的查詢:

需求1:查詢product表中所有欄位及所有記錄

select pid,pname,price,category_name from product;#開發用,查詢效率高

select * from product;#學習用,查詢效率低

需求2:查詢product表中,所有的pname和pid

select pname,pid from product;

需求3:查詢product表中所有的』電腦辦公』記錄

select * from product where category_name=『電腦辦公』;

需求4:查詢product表中所有的pname和price。 所有**提公升1000

select pname,price+1000 from product;#(硬碟資料沒有被改變的)

表資料的刪除:

刪除表資料

需求1:table1表中,刪除id=1的資料

delete from table1 where id=『1』;

需求2:刪除table表中所有資料

delete from table1; #逐行刪除,僅刪除資料—dml

truncate table1; #直接刪除當前表結構,重新建立乙個一模一樣的新錶—ddl

mysql常用語句 MySQL常用語句

create table student id int primary key auto increment comment 學號 name varchar 200 comment 姓名 age int comment 年齡 comment 學生資訊 修改表注釋 alter table studen...

mysql操作常用語句

1 建立 刪除 使用info create database info 建立資料庫 drop database info 刪除資料庫info use info 使用資料庫 2 建立表 刪除表 插入資料 更新資料 查詢資料 刪除資料 建立student表 create table student id...

php mysql 常用語句 mysql常用語句

一 修改mysql使用者密碼 mysql h localhost u root p 命令列登入 update user set password password 123456 where user root 二 資料庫操作 show databases 顯示資料庫 create database ...