sql的基本语句

来源:互联网 发布:身份证复制软件3.7 编辑:程序博客网 时间:2024/05/29 08:00
对于数据库的操作:
1. 数据库的链接:
mysql -u用户名 -p密码
mysql -uroot -p8******
2. 退出数据库的三种方式:
exit / quit /ctrl+d
3. 显示数据库的版本:
select version();
4. 显示时间:
select now();
5. 查看所有的数据库:
show databases;
6. 使用数据库:
use 数据库名;
7. 查看当前使用的数据库
select database();
8.创建数据库:
create database 数据库名 charset=utf8;
9.删除数据库:
drop database 数据库名;
10. 查看当前使用的数据库:
select database()

数据表的操作:
1. 查看当前数据库中所有的表。
show tables;
2. 查看表的结构:
desc 表名;
3. 删除表:
drop table 表名;
4. 查看表的创建语句:
show create table 表名;
5. 修改表----删除字段
alter table 表明 drop 列名;
alter table students drop birthday ;
6. 修改表----修改字段,不重命名,只是改变列的数据类型
alter table 表名 modify 列名 类型以及约束
alter table students modify birth date not null ;
7. 修改表----修改字段,重新命名
alter table 表名 change 原名 新名 类型以及约束
alter table students change birthday birth datetime not null ;
8. 添加字段。
alter table 表名 add 列名 类型;
alter table studets add birthday datetime;
9. 创建表的基本用法:
--------auto_increment 表示自动增长
---------not null 表示不能为空
----------primary key 表示主键
----------default 默认值
-----------create table 数据表的名字
create table classes (
id int unsigned not null auto_increment primary key,
name varchar(30) not null);
create table students(
id int unsigned not null auto_increment primary key,
name varchar(30) not null,
age tinyint unsigned default 0,
high decimal(5,2) ,
gender enum("男","女","中性","保密") default "保密",
class_id int unsigned) ;
10. 查询classes中的所有数据。
select * from classes ;
11. 查询指定的列:
select 列1,列2 from 表名;
12. 查询指定的记录:
select gender as "性别" ,name as "姓名" from students;
13. 物理删除:
delete from students where id=11 or id=8;
14. 逻辑删除。
------用一个字段来表示,这条信息是否已经不能再使用。
------给students表添加一个is_delete 字段bit 类型
alter table students add is_delete bit(1) default 0;
update students set is_delete=1 where id=6;
select * from students where is_delete=0;
15. 修改表:
update students set age=28 ; ---所有记录的字段全部都要改
update students set age=29 where name="tom";
update students set age=18 ,high =180 where id=9;
16. 插入信息。
----插入一个学生的信息
insert into students values(0,"刘备",19,180.5,"男","2001-09-08");
---枚举中下标的使用
insert into students values(default,"曹操",20,201.3,1,"1990-09-07");
---部分插入
insert into students (name,age) values ("孙权",20);
---多行插入
insert into students (name,age) values ("诸葛亮",30),("周瑜",50);
insert into students values(0,"漳卅",19,180.7,"男",‘2001-9-8“),(0,"漳 卅",19,180.7,"男",‘2001-9-8“);
原创粉丝点击