MySQL中常用的sql語句(增刪改查)

2021-10-19 08:39:57 字數 1847 閱讀 2653

存放資料: create table tb_student();

增: insert into tb_student value(『餘』,20,『文理』,『13891716424』,『計算機』);

eg:create table tb_student(name char(32),age int,c_name varchar(32),

-> phonenum varchar(32),major varchar(32));

增:insert into tb_student(name) value (『大校』);

批量增:insert into tb_student value(『劉倩』,21,『文理』,『13891716424』,『計算 機』),

-> (『妙仔』,20,『高新』,『13891716424』,『電氣』);

創主鍵create table tb_學生(id int primary key auto_increment,

-> name varchar(32) not null,

-> gender varchar(2) not null default 『男』);

增加值insert into tb_學生(id,name,gender) value(『aaa』,『張三』,『女』);

關係表create table r_student1_subject(id int auto_increment prinement primary key,sid int,cid int,score int);

刪全部刪:delete from tb_student;

有事物過程

全部刪:truncate tb_student;

無事物過程

drop

直接講表結構也刪了

有條件刪:delete from tb_student where name=『餘』;

*等於號可以改變,為刪除的條件要滿足的東西

改改:update tb_student set age = 19; 改年齡

有條件改:update tb_student set age = 20 where name = 『妙仔』 ; 改名字為妙仔的年齡

該多個字段:update tb_student set age = 20 ,phonenum = 『13891716424』,major = 』 機械』 where name = 『妙仔』;

查查找是盡量不要再select後跟 * ,因為 * 會遮蔽優化器,寫 * 叫通表查詢

查詢表:show tables;

查: select * from tb_student;

迪卡爾機

select * from tb_student1 , tb_subject;

select * from tb_student1 , tb_subject,r_student1_subject;

查出來u所有的的結果,且滿足兩個id的相等才是結果

select * from tb_student1 , tb_subject,r_student1_subject,where tb_student1.id = r_student_subject.sid and tb_subject.id = r_student_subject.cid;

查出來想要的而結果,篩選後的結果,用重新命名查(as可省略),稱表別名,

select a.name 『姓名』, b.name 『課程』 from tb_student1 a,tb_subject b,r_student_subject c where a.id = c.id and b.id = c.id;

字段查:select name from tb_student where age = 20; 查詢年齡是20的名字

多表聯查

MySQL中常用的SQL語句

create database 庫名 charset 指定字符集 其中有兩個常用字符集分別是utf8和utf8mb4,utf8mb4支援標稱符號的儲存 刪除乙個庫 drop database 庫名 修改庫的字符集 alter database 庫名 charset 要指定的新的字符集 檢視乙個資料庫...

mysql中常用的語句 mysql中常用的語句整理

mysql中常用的語句 1 建立帶自增長的主鍵的表 drop table if exists user login create table user login user id int unsigned not null auto increment,user name varchar 50 de...

Mybatis中常用的SQL語句

1.baseresultmap 2.sql id,name,password 3.確切的select select from login test name 4.模糊的select select from login test name like 5.批量的select 可用於資料庫表的批量匯出 s...