須要信息:表名,表字段名,表字段的定義數據庫
create table table_name 列定義 選項;測試
create table table_name like old_table_name; --like:包括舊錶的結構+信息+索引spa
create table table_name select * from old_table_name; --包括舊錶的結構+信息code
create table studyt1 ( id int(20) unsigned auto_increment not null, name varchar(20) not null, jobdate date, primary key (id) ) engine=innodb default charset=utf8; show create table studyt1;
show tables;orm
show tables from db_name;blog
show tables like '關鍵字';排序
show talbes form db_name like '關鍵字';索引
show tables; show tables from test; show tables like 'bl%'; show tables from test like 'bl%';
select * from study01; select * from test.study01; select id01,id02 from test.study01 limit 2;
查數據庫是不是自動commitrem
show variables like '%autocommit%';
測試表和數據it
create table study11 (id int(3),name varchar(12),sex varchar(6)); create table study12 (id int(3),name varchar(12),age int(5)); insert into study11 (id,name,sex) values (1,'study01','男'), (2,'study02','男'), (3,'study03','女'), (4,'study04','女'), (5,'study05','女'); insert into study12 (id,name,age) values (1,'study01',20), (2,'study02',21), (3,'study03',18), (4,'study04',19), (5,'study05',28);
語法:insert into table_name (表字段) values (值列表);
/*方法1*/ insert into study11 (id,name,sex) values (6,'study06','男');/*方法2*/ insert into study11 values (7,'study07','男'); /*方法3*/ insert into study11 set id=8,name='study06',sex='男'; /*方法4*/ insert into study1 values (1,'study01',now(),20);/*方法5*/ insert into study1 values (2,'study02',default,20); /*方法6*/ create table study13 select * from study11;truncate table study13; insert into study13 select * from study11;
語法:delete from 表名 [where 條件] [order by] [limit row_count];
delete from study13 where id=1; delete from study13 limit 2;
語法:update 表名 set 列名=值 where 條件;
update study13 set name='study11',sex='女' where id=1;
語法:select 字段/表名 from 表名/視圖名 where 查詢條件;
查詢條件:
1)where 條件
2)group by 分組
3)having 分組後再聚合
4)limit 限制多少行顯示
5)order by [asc|desc] 排序,升|降
4.4.一、列鏈接
select name,concat(name,'-',sex) as '姓名+性別' from study11;
4.4.二、別名 as/也能夠省略
select a.* from study11 a,study12 b where a.name=b.name;
4.4.三、虛擬表dual
select now() from dual;
4.4.四、SQL語句註釋方式
1)語句前註釋:#
#select now() from dual;
2)語句後註釋:--
select now() from dual; --查當前系統時間
3)多行註釋:/**/
/*select now() from dual;*/
4.4.五、經常使用的運算符
=:等於
>:大於
<:小於
>=:大於等於
<=:小於等於
<>:不等於
!=:不等於
is null:爲null
is not null:不爲null
[not]like:模糊查詢
[not]between and:在什麼範圍內
[not]in:在什麼範圍值內
select * from study12 where age=20; select * from study12 where age<>20; select * from study12 where age>20; select * from study12 where age>=20; select * from study12 where age<20; select * from study12 where age<=20; select * from study12 where age between 18 and 20; select * from study12 where age not between 18 and 20; select * from study12 where age>=18 and age<=20; select * from study12 where age>=18 && age<=20; select * from study12 where age in (18,19,28); select * from study12 where age not in (18,19,28); select * from study12 where name like 'study%'; select * from study12 where name not like '%005%';
4.4.六、邏輯運算
非:not
與:and &&
或:or
異或:xor
select null is not not null,null is null; select null<=>null,10<=>null;
4.4.七、組合
select * from study12 where name='study01' and age=20; select * from study12 where name='study01' or age=21;
4.4.八、like
select * from study12 where name like 'study%';
4.4.九、查詢分組與排序
group by 分組
group by 列 {asc升序|desc降序},{with rollup} 組內聚合計算
select left(name,4),group_concat(name) name from study12 group by left(name,4);
4.4.十、limit
限制返回的行數
select * from study12 order by age desc limit 1;
4.4.十一、distinct
去除重複記錄
select distinct left(name,4) name from study12;
4.4.十二、union
無重並集:把多個結果組合並去重,再以第1列的結果進行升序排序。
select name from study11 union select name from study12;
4.4.1三、union all
有重並集:把多個結果組合不去重
select name from study11 union all select name from study12;
4.4.1四、for update
會鎖表(生產環境不要輕易用)
select * from study11 for update;