CREATE TABLE students( id INTERGER PRIMARY KEY, name TEXT NOT NULL, age INTGER, grade TEXT );
1 2 3 4
如果表不存在就创建,存在就不管 CREATE TABLE if net exists userInfo( \ username text not null, \ passwd text not null);";
展现创建表的模式
1
.schema students
主键
主键是表中唯一标识每一行的字段
1
id INTERGER PRIMARY KEY
插入数据
1
INSERT INTO students(name,age,grade) VALUES('alice',23,'A');
查询数据
1 2 3 4 5 6 7 8 9 10 11 12 13
查看所有内容 SELECT * from students; ps: *是通配符 只看name SELECT name from students; 看name和age SELECT name,age from students; 想看年龄大于20 SELECT name,age from students where age>20; 想看年龄大于20且成绩为A SELECT * from students where age>20 and grade='A'; 想看年龄在20到30区间 SELECT * from students where age between 20 and 30;
更新数据(修改数据)
1
UPDATE students SET age='24' where name ='xxx';
删除数据
1
DELETE FROM students where name='xxx';
SQL函数
1 2 3 4 5 6
SELECT COUNT(*) FROM students; 判断表里是否存在zhangsan,返回值为zhangsan的数量 SELECT COUNT(*) FROM students where username='zhangsan'; SELECT COUNT(*) FROM userInfo where username='zhangsan' and passwd='123'; SELECT COUNT(1) FROM userInfo where username='zhangsan' and passwd='123'; ps:效果一样,但count(1)效率远大于count(*)