mysql的基本语句

1. 登录:在cmd中,编写mysql -uroot -proot,前提是把MySql配置到环境变量中。

show databases;显示所有数据库

user databasename;使用数据库

show tables;显示数据库中所有的表名

create database DataBaseName;    创建数据库
drop databases 数据库名         删除数据库
drop table 表名                 删除表

2.创建表
create table tableName(
     columnName    dataType,
       ……………..
columnName    dataType
primary key(‘ID’);设置ID为主键

eg:

CREATE TABLE kongzi (
    id integer NOT NULL AUTO_INCREMENT,
    name CHAR(30) NOT NULL,
PRIMARY KEY (id)
);

 

3.对数据库的基本操作:

增:insert into tableName(表属性) values(value,value,value……);                  向tableName中插入信息
查:select * from tableName;                 查看当前表中的所有数据
删delete from tableName where 条件;             删除数据库中的数据
改update tableName set 条件;                 修改数据库中的数据

改表名:alter table tableName rename [to] new_tableName;

如需在表中添加列,请使用下列语法:

ALTER TABLE table_name  ADD column_name datatype


要删除表中的列,请使用下列语法:

ALTER TABLE table_name  DROP COLUMN column_name

4.sql升序和降序排列

升序:select * from table1 order by age asc;

降序:select * from table1 order by age desc;

5.数字统计和平均值和总数

表的行数:select count(*) from table1;

工资的平均值:select avg(income) from table1;

工资的总数:select sum(income) from table1;

6.分组统计平均工资:group by

    a.不同部门的平均工资:select dept as 部门,avg(income) as 平均工资 from table1 group by dept;

    b.where不可以包括聚合函数,用having。

       select dept as 部门,avg(income) as 平均工资 from table1 group by dept having age(income)>4000;

7.连接查询

    a.笛卡尔积连接查询:select a.name,a.age,a.dept,b.name,b.dept from table1 a,table2 b;  (table1*table2随机的)

    b.等值连接:select position ,table1.name,table2.dept from table1 ,table2
where table1.dept_Id=table2.dept_Id; (注意数据库表的设计。特别是dept_Id,要有等值列

    c.非等值连接使用(between……and)和自链接不作介绍。

8.  Mysql_colse(); 结束当前登录的数据库。

    

原文地址:https://www.cnblogs.com/a892647300/p/2656986.html