I'm making here a reference for sql statements that might come in handy since I usually surf for the proper ways to do each and everyone of it.
So here are some sql syntax that every programmer (and like) should know. I'll just go with the basic CRUD and sight some examples.
1) Creating a Database
CREATE DATABASE database_name
2) Drop Database
3) Creating Table
CREATE TABLE "table_name"( "column 1" "data_type_for_column_1", "column 2" "data_type_for_column_2", ... )
Sample:
a) Basic:
CREATE TABLE example ( id INT, data VARCHAR(100) );
b) With Auto Increment and Primary Key:
CREATE TABLE example_autoincrement ( id INT NOT NULL AUTO_INCREMENT PRIMARY KEY, data VARCHAR(100) );
c) With timestamp:
CREATE TABLE example_timestamp ( id INT NOT NULL AUTO_INCREMENT PRIMARY KEY, data VARCHAR(100), cur_timestamp TIMESTAMP(8) );
d) With default value:
CREATE TABLE example_default_now ( id INT NOT NULL PRIMARY KEY AUTO_INCREMENT, data VARCHAR(100), created TIMESTAMP DEFAULT NOW() );
4) Updating or Altering an existing Table
a) Adding a column
ALTER TABLE table_name ADD column_name datatype;
b) Dropping / Deleting a table column
ALTER TABLE table_name DROP COLUMN column_name;
c) Altering / Updating a table column
ALTER TABLE table_name ALTER COLUMN column_name datatype;
d) Updating Column Name
ALTER TABLE table_name CHANGE old_column_name new_column_name type size;
e) Alter Table Name
ALTER TABLE old_table_name RENAME TO new_table_name;
5) Select Items on Table
NOTE: Here, column_name(s) can be replaced to * if you want to return all columns
a) Basic:
SELECT column_name(s) FROM table_name
or
SELECT * FROM table_name
b) With Conditions:
SELECT column_name(s) FROM table_name WHERE column_name operator value
c) In specific order:
SELECT column_name(s) FROM table_name ORDER BY column_name(s) ASC|DESC
6) Insert item on Table
INSERT INTO table_name (column1, column2, column3,...) VALUES (value1, value2, value3,...)
7) Update item on Table
UPDATE table_name SET column1=value, column2=value2,... WHERE some_column=some_value
8) Delete item on Table
DELETE FROM table_name WHERE some_column=some_value
9) View all databases and/or tables
SHOW databases;
USE database_name; SHOW tables;
10) Describe a table
DESCRIBE table_name;
These are just the basic things that should be learn in handling mySql database. Happy coding! :)
Andami na pala talagang PL ngayon... but none of them ang naiintindihan ko. Or sadyang bata pa ako sa Programming Languages. :D
ReplyDeleteMay tanong ako. Isa lang ba ang PL na ginagamit mo or you can use multiple PL's?
I can use multiple, depende sa project and requirements.. at saka yan.. it's more on database and mostly gagamitin mo ito.. isa pa, that is the reason why I posted it here, so that I can see the summary without researching on other sites
ReplyDeleteWow, I learned a lot man! You seems so good in databasing... wish I can do that too. What programming lang are you focused on?
ReplyDeleteLol, hey "Typo" Error on "wish I can do that too." that is "Can't". Thanks.
ReplyDeletewell, I'm currently doing some actionscript related project lately (Flex Builder to be specific).. so for my focus, hmmm.. I don't think I have anything in particular.. i just do what the client/boss asks me to use..hahaha :p
ReplyDeletebut generally most PL I'm doing are scripting languages ( mostly used in web dev or RIA ).. I think I'm too flexible for a specific language (which I think is a good habit)..
All of the above commands are truly important in Database program. BTW what course did you take because I think your good in programming.
ReplyDeletethanks christian.. welcome to my blog!
ReplyDeleteI actually took up BS Computer Engineering, but I focused on software (technically programming). I saw your profile and learned that you're a BSIT student. Hope you can learn things from my blog.. :)
Good to know that there is still people like you that study hard even it is not your major (I'm not really sure). I also hope that I learn more on your blog. Nice to meet you then.
ReplyDeletethanks christian
ReplyDeletetest
ReplyDelete