1.创建数据库
create database mydata;
2.连接数据库
只有连接了才能在这个数据库下创建表(不然不知道建哪里去了)
use mydata;
3.创建表
create table person(
number int(11),
name varchar(255),
birthday DATE);
也可以
create table if not exists person (
number int(11),
name varchar(255),
birthday DATE);
可能版本不同有的人这里用尖括号也可以···
良好的习惯是建表的时候同时设置主键
比如要把number
设置成主键可以:
number int(11) primary key,
或者添加一句
primary key(number)
如果还需要外键语法如下:
foreign key (number) references 其他表名(number)
创建表时输入错误不能直接修改,所以可以写成sql script用文件名做参数执行。
没有分号!!!!
. 后紧跟着全路径 路径千万别写错不然会出现
Failed to open file ‘file_sourse’, error: 2的提示
从对比两张图可以发现mysql可以执行.txt文件也可以执行.sql文件。因为我重复创建表了所以出现了warning 的提示。
这里要注意如果要创建.sql文件不能直接改txt文件的后缀名。
当然建表也可以按别的表的样子复制,语法如下
CREATE TABLE new_tbl LIKE orig_tbl;
创建表后也可以删除表,语句如下:
drop table person;//删除表
truncate table person;//清空表
delete from person where name='susu';//删除元组
查询数据库
扫描二维码关注公众号,回复:
4886932 查看本文章
show databases;
查询表
show tables;
展示表项
describe 表名; //desc 表名;
插入数据
insert into 表名 values(表项1,表项2···);
等插入后提交commit;
有的引擎是自动提交可以不写这句话,查询组动提交的办法show variables like "%autocommit";
分页展示(展示表按某表项倒排在第一条之后展示两条)
select * from 表名 order by 某表项 dese limit 1,2;
设置自动递增表
create table article(
id int primary key auto_increment,
title varchar(255)
);
插入
insert into article values(null,‘a’);
insert into article values(null,‘b’);
也可以给某几项插入
insert into article(title) values('e');