sql语句

 auto_increment   自动增长  整形才能做自动增长

NOT NULL    不能为空

DEFAULT  0   默认值为0

PRIMARY KEY    定义主键

ALTER    TABLE  修改表

ADD[COLUMN]  增加列

AFTER           在什么之后

CHANGE [COLUMN]   

修改表中列名和数据类型

alter  table  tb2

change column  tb2_id  tb_id  int;

ALTER[COLUMN]  修改和删除默认值;

set   关键词

MODIFY   修改数据类型,修改一个类型

DROP[column]    删除表中列

RENAME[TO]    为表重命名

RENAME   TABLE    旧表名   to   新表名

DROP   TABLE   删除表

show  columns from  mysql_test.customers;   查看表列 (显示表的结1

创建双普通索引     方法1;

create   index  index_cust

on   mysql_test.customers(cust_name,sex);

创建升序索引         方法2;add增加    asc升序

alter table  mysql_test.class

add  index  index_stu_name(class_name(3)  asc);

查看索引

show   index  from  mysql_test.class;

删除索引

drop   index  index_stu_name   mysql_test.class;

插入语句     方法1

insert  into   mysql_test.customers

values('xiaoqh',2022,'P','shenzhen','longhua');

插入语句       方法2

insert    into  mysql_test.customers

set   cust_name='chenhq',cust_address='qinghu';

删除语句    

DELETE  FROM  mysql_test.customers

WHERE  cust_name=' zenghr';

更新语句

update   mysql_test.customers

set  cust_address='wuhan'

where  cust_name='zenghr';

查询语句

select *  from   customers;   查询表所有内容

-----------------------------------------------------------

select   cust_name,cust_city

from   mysql_test.customers;              查询姓名和城市的信息

----------------------------------------------------------

查询换别名

SELECT  cust_name   as xiangming , cust_city as  dizhi

FROM   mysql_test.customers;

交叉连接&笛卡尔积

select * from  tb1,tb2;    方法1

select * from tb1 cross join tb2;    方法2

两表相连&内连接          必须有相同外码

select *

from    tb1   inner  join  tb2

on  tb1.tb_id=tb2.tb2_id;

where子句&条件查询

select * from  tb2

where  tb2_id=3;

范围查询

select *  from  customers

where cust_id between 2  and 3;

枚举查询

select * from customers

where cust_id  in(1,2);

查询谁的cust_address为null

select  cust_name from  customers

where cust_address is null;

查看相同地址的人数

select cust_name,sex,count(*)  as  'renshu'

from  customers

group  by cust_address,sex;

查询降序和升序   desc&asc

select cust_id,sex 

from customers

order by  cust_id desc,sex desc;

限制语句 LIMIT

select cust_id,cust_name

from customers

order by   cust_id

limit   2,4;

三表连查

mysql> select b_name,bo_jdate

    -> from book,borrow,readers

    -> where b_name='yuwen' and readers.re_id=borrow.r_id and borrow.b_id=book.b_id;

最高,最低,平均值  聚合函数 group by

mysql> select b_press,max(b_price),min(b_price),avg(b_price)

    -> from book

    -> group by b_press;

你可能感兴趣的:(sql语句)