mysql基本語句1
阿新 • • 發佈:2021-11-28
-- 操作資料庫 show databases; show create database dbname; -- 建立資料庫,建立前進行判斷,並制定字符集 create database if not exists DBname character set utf8; -- 修改 alter database DBname character set utf8; -- delte drop database db3; drop database if exists db3; -- 使用 -- 查詢當前使用的資料庫 select database(); use db1; -- 使用db1 -- 操作表 -- CRUD -- 查詢某個資料庫中所有表名稱 use db1; show tables; -- 查詢表結構 desc db1; -- 建立表 create table tableName(列名1 資料型別1, ..., ...); create table stu like student; -- 複製表 -- 資料型別 int ,double(m,n), date[yyyy-MM-dd],datetime[yyyy-MM-dd HH:mm:ss],timestamp[yyyy-MM-dd HH:mm:ss,不給這個欄位賦值,則自動使用系統時間賦值], varchar(20)[字串型別,最大20個字元,一個漢字一個字元] create table student( id int, name varchar(32), age int, score double(2,2), birthday date, create_time timestamp ); -- 刪除表 drop table if exists tableName; create table stu like student; -- 複製表 -- 修改表結構 -- 修改表名稱 alter table T1 rename to T2; -- T1 --> T2 -- 修改表字符集 show create table T2; alter table T2 character set utf8; -- 修改某列,新增列,刪除列 alter table TableName add sex varchar(2); alter table T2 change sex xingbie varchar(4); alter table T2 modify sex varchar(8); -- delete alter table 表名 drop 列名; -- DML 增刪改表中資料 -- 新增資料 insert into tablename(列名1,...,...) values(值1,值2,..,...); select * from tablename; -- 刪除資料 delete from 表名 where 條件; delete from stu where id=1; truncate table 表名; -- 修改資料 update 表名 set 列名1=值1, ...=... where 條件; -- DQL 查詢表中的記錄(重點) select 欄位名列表 from 表名列表 where 條件列表 group by 分組欄位 having 分組之後的條件 order by 排序 limit 分頁限定 -- 基礎查詢 -- 查詢姓名和年齡 select name,age from stu; -- 去除重複,結果集都一樣的才可以去除 select distinct address from stu ; -- 計算 select name,math+english from stu; -- 如果有null參與計算結果都為null select name 姓名, math + ifnull(english,0) as 總分 from stu; -- 條件查詢 select * from where 條件; between .. and .. like isnull and or not -- 20-30歲之間 select * from stu where age >=20 and age <=30; --